Repository navigation
Cherry pick #104009 to 26.2: Fix SQL injection in ExternalQueryBuilder via incorrect string escaping - #105182
Merged
robot-ch-test-poll1 merged 7 commits intoMay 17, 2026
Conversation
`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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job