Repository navigation
Conversation
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260625-113800 |
|
cc @vitlibar @yakov-olkhovskiy — could you review this? It fixes SQL injection in the MySQL-wire replacement queries ( |
|
Workflow [PR], commit [da02224] Summary: ❌
AI ReviewSummary
Final Verdict
|
7ec66d7 to
6d9415a
Compare
|
Fixed in 6d9415a. So |
6d9415a to
c15d4b5
Compare
|
Both sanitized parsers now accept one optional trailing Added pymysql regression cases in |
c15d4b5 to
4b6aa2b
Compare
|
Both addressed in
Regression cases added in |
The MySQL handler rewrites a few client commands into ClickHouse SQL by concatenating the raw client-supplied suffix into the replacement query without parsing or escaping it. An authenticated MySQL-wire client could inject SQL: - showTableStatusReplacementQuery concatenated the SHOW TABLE STATUS LIKE argument after "WHERE name LIKE ", so "" UNION SELECT ... FROM system.users read arbitrary tables instead of the intended system.tables-only lookup. - setSettingReplacementQuery concatenated the SET value tail, so "SET SQL_SELECT_LIMIT=1, max_threads=42" set unrelated settings. Both now parse the client argument (a single string literal for the LIKE pattern; "= <literal|DEFAULT>" for the setting value) and re-serialize it with quoteString()/FieldVisitorToString instead of concatenating raw input. The injection runs under the caller's own grants, so there is no privilege escalation, but it bypasses the intended read scope. checkShouldReplaceQuery is only a byte-prefix check, so the setting name in setSettingReplacementQuery is now checked for a word boundary before the sanitized replacement is produced. Otherwise a longer variable that merely starts with a mapped name (e.g. "SET SQL_SELECT_LIMITED=1" vs the mapped "SET SQL_SELECT_LIMIT") would be translated and silently reset the unrelated "limit" setting; it now passes through untranslated and errors as an unknown setting. The sanitized parsers accept one optional trailing ';' followed by end-of-stream. executeQuery treats ';' as end-of-query, so a benign programmatic client may send "SHOW TABLE STATUS LIKE 'one';" or "SET SQL_SELECT_LIMIT=2;" (the mysql CLI strips the terminator, but other clients pass it through). Requiring a strict end-of-stream broke those benign inputs. A second statement after the ';' still leaves the parser before end-of-stream and is rejected, so the injection protection is intact. setSettingReplacementQuery no longer has a state-changing fallback on a malformed value. The previous "reset the mapped setting to DEFAULT" fallback silently changed the caller's session setting (and reported success) when the tail was not exactly "= <literal|DEFAULT>" with an optional single ';' (for example "SET SQL_SELECT_LIMIT=1, max_threads=42" parses the "1", stops at the comma, and reset "limit"). It now throws a syntax error in that case, so the session setting is never changed as a side effect of a rejected input and the raw tail is never executed. killConnectionIdReplacementQuery used the regex "^[0-9]+$", which rejected a benign single-statement terminator: "KILL QUERY 12;" left the suffix "12;", failed the match, and fell through to the raw "KILL QUERY 12;" that ClickHouse does not support. The connection id is now captured with "^\s*([0-9]+)\s*;?\s*$" (one optional trailing ';' and surrounding whitespace) and only the captured digits are substituted, so multi-digit ids and a trailing terminator work while any non-numeric tail or a second statement still fails the match. The dispatcher matches the registered keys "KILL QUERY" and "SHOW TABLE STATUS LIKE" without their trailing separator, but the helpers slice the suffix at prefix.length() which includes the space. A malformed command would skip the byte at the separator position and be coerced into a valid command: "KILL QUERY;12" and "KILL QUERYx12" reached the digit regex as "12" (cancelling query id 12), and "SHOW TABLE STATUS LIKEx'a'" became a lookup of 'a'. Both helpers now require the byte at the separator position to be whitespace before slicing, so a missing or non-space separator is rejected (KILL QUERY passes through and errors; SHOW TABLE STATUS matches nothing). Added a regression test (test_mysql_replacement_query_injection) that fails on the previous behavior and passes with this fix, covering the SQL_SELECT_LIMITED boundary case, the trailing-semicolon terminator for SHOW TABLE STATUS / SET / KILL QUERY, the rejection of an injected SET tail (without changing "limit" or "max_threads"), the rejection of a second statement after ';', and the missing-separator cases for KILL QUERY and SHOW TABLE STATUS LIKE. Co-Authored-By: Claude Opus 4.8 <[email protected]>
4b6aa2b to
cef6b4b
Compare
|
Fixed in cef6b4b. Both |
CI finish ledger — cef6b4bEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260626-010000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 66/69 (95.65%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code |
CI finish ledger — da02224CI fully finished (Finish Workflow pass). The
Session id: cron:our-pr-ci-monitor:20260703-053000 |
d758283
…acement-query-injection Fix SQL injection in MySQL wire protocol replacement queries
Cherry pick #108474 to 26.6: Fix SQL injection in MySQL wire protocol replacement queries
Cherry pick #108474 to 26.3: Fix SQL injection in MySQL wire protocol replacement queries
Cherry pick #108474 to 26.3: Fix SQL injection in MySQL wire protocol replacement queries
Cherry pick #108474 to 26.6: Fix SQL injection in MySQL wire protocol replacement queries
Backport #108474 to 26.6: Fix SQL injection in MySQL wire protocol replacement queries
Backport #108474 to 26.3: Fix SQL injection in MySQL wire protocol replacement queries
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed SQL injection in the MySQL wire protocol: the
SHOW TABLE STATUS LIKEargument and theSET <mysql_setting>value sent over the MySQL interface (port 9004) are now parsed and re-quoted instead of being concatenated verbatim into the rewritten query. Also fixedKILL QUERY <id>over the MySQL interface silently ignoring multi-digit connection ids.Description
MySQLHandlerrewrites a few MySQL-wire client commands into ClickHouse SQL. The rewrites concatenated the raw client-supplied suffix into the generated query with no parsing or escaping, so an authenticated MySQL-wire client could inject SQL. The injection runs under the caller's own grants (no privilege escalation), but it bypasses the intended read scope of these helpers.showTableStatusReplacementQueryappended theSHOW TABLE STATUS LIKEargument directly after... WHERE name LIKE. This let a client inject an arbitrary tail and read any table instead of the intendedsystem.tables-only lookup, e.g.:returned rows from
system.users. The argument is now parsed as a single string literal and re-quoted withquoteString(); anything that is not exactly one string literal matches nothing.setSettingReplacementQueryconcatenated theSETvalue tail, soSET SQL_SELECT_LIMIT=1, max_threads=42becameSET limit=1, max_threads=42and set unrelated settings. The value is now parsed (= <literal|DEFAULT>) and re-serialized; once theSET <mysql_setting>prefix matches, the handler always emits a sanitized replacement and never executes the raw client text.killConnectionIdReplacementQueryused^[0-9]withRE2::FullMatch, which required the whole id to match a single digit, so every multi-digit connection id (e.g.KILL QUERY 12) was silently not translated. Tightened to^[0-9]+$, which accepts multi-digit ids and still rejects any non-numeric tail.A regression test (
tests/integration/test_mysql_protocol/test.py::test_mysql_replacement_query_injection) is added: it fails on the previous behavior (leakssystem.users) and passes with this fix, while the benignSHOW TABLE STATUS LIKE,SET, andKILL QUERYpaths keep working.Version info
26.7.1.580(included in26.7and later)26.6.8.6,26.3.33.93