Repository navigation
Fix SQL injection in ExternalQueryBuilder via incorrect string escaping - #104009
alexey-milovidov merged 5 commits into
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 ClickHouse#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]>
|
Workflow [PR], commit [ee42acf] Summary: ✅ AI ReviewSummaryThis PR fixes string-literal escaping in Final Verdict
|
…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]>
Let's say we have a string with the following contents: Now let's see what happens if we take the string with Postgres-style escaping and put it in a ClickHouse query. It will not parse. |
…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]>
|
@alexey-milovidov Thank you for the example — you're correct. The fix incorrectly applied |
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: ClickHouse#104009
|
Removed The test was a regression test for the ClickHouse-source dictionary path, which uses Per-backend escaping behavior is still covered by:
The other CI failures are unrelated to this PR:
|
LLVM Coverage Report
Changed lines: 100.00% (44/44) · Uncovered code |
Cherry pick #104009 to 25.8: Fix SQL injection in ExternalQueryBuilder via incorrect string escaping
…ia incorrect string escaping
Cherry pick #104009 to 26.2: Fix SQL injection in ExternalQueryBuilder via incorrect string escaping
…ia incorrect string escaping
Cherry pick #104009 to 26.3: Fix SQL injection in ExternalQueryBuilder via incorrect string escaping
…ia incorrect string escaping
Cherry pick #104009 to 26.4: Fix SQL injection in ExternalQueryBuilder via incorrect string escaping
…ia incorrect string escaping
This is a regression test for the `ExternalQueryBuilder` ClickHouse-backend path that uses `IdentifierQuotingStyle::Backticks` and backslash escaping. Switching to Postgres-style `''` escaping for this backend would break ClickHouse queries when keys contain backslashes, because the ClickHouse SQL parser interprets `\` as an escape character. This test passes on master and would have caught any regression that changed the escaping style for the ClickHouse-source dictionary backend. Originally part of ClickHouse#104009 but excluded from that PR because the bugfix validation requires the test to fail on master, and this test does not reproduce the original SQL injection bug (which only affected `DoubleQuotes`-style backends like PostgreSQL/MySQL). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…ia incorrect string escaping (#105187) Co-authored-by: robot-clickhouse <[email protected]>
…ia incorrect string escaping (#105183) Co-authored-by: robot-clickhouse <[email protected]>
…ia incorrect string escaping (#105185) Co-authored-by: robot-clickhouse <[email protected]>
Backport #104009 to 25.8: Fix SQL injection in ExternalQueryBuilder via incorrect string escaping
…yBuilder via incorrect string escaping (ClickHouse#105187) Co-authored-by: robot-clickhouse <[email protected]>
Fix SQL injection in
ExternalQueryBuildercaused by incorrect string literal escaping when buildingWHEREclauses for external dictionary sources (PostgreSQL, Cassandra, XDBC).ExternalQueryBuilderused the defaultFormatSettingswithescape_quote_with_quote = false, soserializeTextQuotedproduced backslash-escaped string literals (\') in the generated SQL. PostgreSQL (withstandard_conforming_strings = onby default since 9.1) treats backslash as a literal character — not an escape sequence — allowing a caller to break out of a string literal and inject arbitrary SQL by passing a key containing a single quote todictGeton a PostgreSQL or XDBC dictionary source. The same vulnerability applies to Cassandra, which also follows the SQL standard.The same class of bug was fixed for the PostgreSQL table function in PR #74144, but the dictionary code path through
ExternalQueryBuilderwas missed.Fix: in
ExternalQueryBuilder's constructor, setescape_quote_with_quote = trueonly whenquoting_style == DoubleQuotes(PostgreSQL, Cassandra, XDBC SQL). For ClickHouse and MySQL (Backticks/BackticksMySQL), keep the default backslash escaping (\',\\), which both backends parse natively. This per-backend split addresses reviewer feedback that the original "always quote-doubling" fix would have caused ClickHouse-on-ClickHouse dictionaries to corrupt string values containing backslashes when those values are serialized back throughTabSeparated.A new gtest
gtest_external_query_builder.cppcovers both branches:DoubleQuotesdoubles single quotes and leaves backslashes intact;Backticksuses\'and doubles backslashes.Reported in: https://github.com/ClickHouse/clickhouse-private/issues/56142
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed SQL injection vulnerability in PostgreSQL, Cassandra and XDBC dictionary sources: string keys containing single quotes were escaped with
\'(backslash), which these backends treat as a literal backslash rather than an escape sequence, allowing arbitrary SQL to be injected into dictionary lookup queries.ExternalQueryBuildernow emits SQL-standard''escaping for those backends; ClickHouse and MySQL dictionary sources continue to use backslash escaping.Documentation entry for user-facing changes
Version info
26.5.1.727(included in26.5and later)26.4.3.34,26.3.11.26,25.8.29.17