Repository navigation
Fix security issue to prevent SQL injection in postgresql and sqlite table functions - #74144
Merged
Merged
Conversation
Contributor
|
This is an automated comment for commit 7fb5c9d with description of existing statuses. It's updated for the latest CI running ✅ Click here to open a full report in a separate page Successful checks
|
pamarcos
marked this pull request as ready for review
January 15, 2025 16:34
kssenii
reviewed
Jan 17, 2025
kssenii
approved these changes
Jan 17, 2025
robot-clickhouse
added a commit
that referenced
this pull request
Jan 20, 2025
…in postgresql and sqlite table functions
robot-clickhouse
added a commit
that referenced
this pull request
Jan 20, 2025
… in postgresql and sqlite table functions
robot-clickhouse
added a commit
that referenced
this pull request
Jan 20, 2025
… in postgresql and sqlite table functions
robot-clickhouse
added a commit
that referenced
this pull request
Jan 20, 2025
… in postgresql and sqlite table functions
robot-clickhouse-ci-2
added a commit
that referenced
this pull request
Jan 20, 2025
Backport #74144 to 24.12: Fix security issue to prevent SQL injection in postgresql and sqlite table functions
pamarcos
added a commit
that referenced
this pull request
Jan 21, 2025
Backport #74144 to 24.11: Fix security issue to prevent SQL injection in postgresql and sqlite table functions
pamarcos
added a commit
that referenced
this pull request
Jan 21, 2025
Backport #74144 to 24.10: Fix security issue to prevent SQL injection in postgresql and sqlite table functions
pamarcos
added a commit
that referenced
this pull request
Jan 21, 2025
Backport #74144 to 24.8: Fix security issue to prevent SQL injection in postgresql and sqlite table functions
pamarcos
added a commit
that referenced
this pull request
Jan 21, 2025
Backport #74144 to 24.3: Fix security issue to prevent SQL injection in postgresql and sqlite table functions
This was referenced May 4, 2026
alexey-milovidov
pushed a commit
that referenced
this pull request
May 17, 2026
`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]>
pull Bot
pushed a commit
to stnxo2023/ClickHouse
that referenced
this pull request
Sep 23, 2026
SQLiteSink::consume builds a single statement string and passes it to sqlite3_exec, which executes every ;-separated statement in it. Two user-chosen identifiers were interpolated with backslash escaping: src/Storages/StorageSQLite.cpp:187 doubleQuoteString(remote_table_name) src/Storages/StorageSQLite.cpp:194 quoteString(it->name) Both resolve to writeAnyEscapedString with escape_quote_with_quote = false, which escapes the quote character with a backslash. SQLite has no backslash escape at all: inside a quoted identifier a backslash is ordinary data and doubling the quote is the only escape, so the first raw double quote in a name terminates the identifier and the remainder is tokenized as SQL. A trailing -- comments out the sink's own " (columns) VALUES ...; tail, so the whole string parses cleanly. Both sites are reachable from ordinary SQL: the remote table name is the second engine argument and the column names are the ClickHouse table's own. When the columns are listed explicitly the engine skips reading the remote structure, so the injected name never has to name a real SQLite table. An injected ATTACH DATABASE opens a second file on the same connection and is never seen by validateSQLiteDatabasePath, which validates only the path handed to sqlite3_open, so the user_files restriction is bypassed for reads and writes alike. This adds writeDoubleQuotedStringSQLite / doubleQuoteStringSQLite, the identifier counterpart of the writeQuotedStringSQLite added for the read path in ClickHouse#104217, and uses it at both sites. ClickHouse#74144 fixed the same class in the schema path and deliberately chose correct escaping over validation, because a legitimate table or column name containing a quote has to keep working. :194 is a behaviour fix as well as hardening: quoteString emitted a single-quoted string literal where an identifier belongs, so a column name containing a double quote produced invalid SQL. The same two lines also make a table whose name contains a backslash writable, which it previously was not. A NUL inside an identifier is deliberately not rejected. sqlbuf.str().c_str() truncates the statement at the NUL and every double quote before it has already been doubled, so the identifier is necessarily unterminated and SQLite rejects the statement with "unrecognized token". It always fails loudly, and the statement's meaning can never be silently truncated. Two residuals are left alone. The read path quotes identifiers through IdentifierQuotingStyle::DoubleQuotes with the same backslash escaping, so a table whose name contains a double quote or a backslash still cannot be read; that enum is shared with other engines and dictionary sources, and it crosses no privilege boundary because SQLiteSource prepares a single statement. The VALUES payload escapes control characters with backslashes, so a tab does not round-trip. Test: 05055_sqlite_insert_identifier_escaping. It fails on master and passes with this change; 5 of its 6 arms differ between the two binaries. Co-Authored-By: Claude Opus 5 <[email protected]>
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.
Fix security issue to prevent SQL injection in postgresql and sqlite table functions
At first I thought we should sanitize and validate the parameters to check whether the database and table names comply with PostgreSQL convention. However, turns out we can fix this in an easier way: properly escaping single quotes for PostgreSQL and SQLite.
Closes https://github.com/ClickHouse/clickhouse-private/issues/21742
Closes #74610
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix security issue to prevent SQL injection in postgresql and sqlite table functions
Documentation entry for user-facing changes
CI Settings (Only check the boxes if you know what you are doing)
All builds in Builds_1 and Builds_2 stages are always mandatory
and will run independently of the checks below: