Skip to content

Cherry pick #104009 to 25.8: Fix SQL injection in ExternalQueryBuilder via incorrect string escaping - #105180

Merged
robot-ch-test-poll1 merged 7 commits into
backport/25.8/104009from
cherrypick/25.8/104009
May 17, 2026
Merged

robot-ch-test-poll1 merged 7 commits into
backport/25.8/104009from
cherrypick/25.8/104009

Conversation

@robot-ch-test-poll1

Copy link
Copy Markdown
Contributor

Original pull-request #104009

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

tiandiwonder and others added 7 commits May 4, 2026 12:35
`ExternalQueryBuilder` used the default `FormatSettings` with
`escape_quote_with_quote = false`, causing `serializeTextQuoted` to
produce backslash-escaped string literals (`\'`) in the generated SQL
sent to external databases.

PostgreSQL (with `standard_conforming_strings = on` by default since
9.1) treats backslash as a literal character, not an escape sequence.
This allowed a caller to break out of a string literal and inject
arbitrary SQL by passing a string key containing a single quote through
`dictGet` on a PostgreSQL or XDBC dictionary source.

The same class of bug was fixed for the PostgreSQL table function in
PR #74144, but the dictionary code path via `ExternalQueryBuilder` was
missed.

Fix: initialize `format_settings` with `escape_quote_with_quote = true`
so that all backends using `ExternalQueryBuilder` (PostgreSQL, MySQL,
XDBC, ClickHouse, Cassandra) consistently use the SQL-standard `''`
quoting style. MySQL and ClickHouse accept both styles, so there is no
regression risk for those backends.

Reported in: ClickHouse/clickhouse-private#56142

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…aping

MySQL was never affected by the backslash-vs-quote-doubling escaping bug
fixed in the previous commit: MySQL correctly handles \' by default.
There is no regression scenario to protect against, so the test only
adds maintenance burden without value.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…g per-backend

Use SQL-standard `''` quoting only for backends with `IdentifierQuotingStyle::DoubleQuotes`
(PostgreSQL, XDBC-PostgreSQL). ClickHouse and MySQL use backslash escaping by default,
and their SQL parsers would misinterpret `''`-escaped strings containing backslashes:
`\b` in a `''`-escaped literal is parsed by ClickHouse as a backspace character,
and a lone `\` produces an unterminated string literal (parse error).

Add regression test for ClickHouse dictionary source with backslash and quote keys.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Add coverage and a comment cleanup for the per-backend escape mode
chosen in `ExternalQueryBuilder` after reviewer feedback (PostgreSQL,
Cassandra, XDBC SQL go through SQL-standard `''` quoting while
ClickHouse and MySQL keep backslash escaping):

  - New unit test `gtest_external_query_builder.cpp` exercises both
    branches at the generated-SQL level via `composeLoadKeysQuery`:
    `DoubleQuotes` doubles single quotes and leaves backslashes
    unchanged; `Backticks` uses `\'` and `\\`.

  - New MySQL integration test in `test_dictionaries_mysql/test.py`
    verifies that a MySQL dictionary source correctly handles keys
    containing single quotes and backslashes under backslash escaping.

  - Restate the comment in `ExternalQueryBuilder.cpp` in plain ASCII
    and switch to an explicit `if` so the opt-in nature of
    `escape_quote_with_quote` for SQL-standard quoting is obvious.

No behavior change beyond the per-backend split already addressed in
the previous reviewer-feedback commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The new stateless test 04141_dict_clickhouse_source_backslash_key was a
regression test for the ClickHouse-source dictionary path, which uses
`IdentifierQuotingStyle::Backticks` and is unaffected by the original
SQL injection bug. As a result, the test passed on master, causing the
"Bugfix validation (functional tests)" check to fail with "Failed to
reproduce the bug".

Per-backend escaping behavior is still covered by:
- `gtest_external_query_builder.cpp` (unit-level coverage of both
  `DoubleQuotes` and `Backticks` quoting styles, including backslash
  and single-quote handling)
- `test_dictionaries_postgresql.py::test_postgres_dict_complex_key_with_single_quote`
  (end-to-end PostgreSQL coverage that actually catches the original
  bug on master)
- `test_dictionaries_mysql.py::test_mysql_dict_complex_key_with_special_chars`
  (end-to-end MySQL coverage of backslash escaping)

CI report: #104009
…cape

Fix SQL injection in ExternalQueryBuilder via incorrect string escaping
@robot-ch-test-poll1 robot-ch-test-poll1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels May 17, 2026
@robot-ch-test-poll1
robot-ch-test-poll1 merged commit 3fd694a into backport/25.8/104009 May 17, 2026
@robot-ch-test-poll1
robot-ch-test-poll1 deleted the cherrypick/25.8/104009 branch May 17, 2026 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants