Repository navigation
Cherry pick #117421 to 26.3: Fix SQL injection through identifiers when inserting into a SQLite table - #121835
Merged
robot-clickhouse-ci-1 merged 10 commits intoSep 24, 2026
Merged
Conversation
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 #104217, and uses it at both sites. #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]>
Both were single comments spanning several lines. Each is now one line under 140 characters that keeps the non-obvious fact and drops what the surrounding code already states. Comment-only change: stripping comment lines from this file at the parent commit and at this one leaves byte-identical text, and the test still passes. Co-Authored-By: Claude Opus 5 <[email protected]>
WriteBufferFromString resizes the string to the bytes written in its destructor, and the buffer was still alive at the return statement, so the result was correct only because the compiler elides the named return value. Scoping the buffer matches backQuote, backQuoteIfNeed and backQuoteMySQL in the same file.
${USER_FILES_PATH}/.. is the server's own data directory, and no other stateless
test writes there. ${CLICKHOUSE_TMP} gives the property the test needs, a path
outside the user_files confinement, without writing to the data directory.
Both directions re-measured at the new path: the test passes with the fix and
fails on unpatched master, where the injected ATTACH DATABASE still reads the
canary and creates a marker outside user_files. A1 still reports
PATH_ACCESS_DENIED, so the control that proves the path is out of confinement is
unchanged.
Resolves conflicts in src/Common/quoteString.{h,cpp} and src/IO/WriteHelpers.h
against 711862b ("Fix SQL injection through identifiers on the PostgreSQL
read path"), which added writeDoubleQuotedStringPostgreSQL and
doubleQuoteStringPostgreSQL at the same positions in those files. Their bodies
are byte-identical to the SQLite helpers added here: both implement the SQL
standard delimited identifier, where a " is escaped by doubling it and every
other byte, backslash included, is literal.
Resolved as a union, keeping the SQLite helpers self-contained instead of
delegating to the PostgreSQL ones. This PR is labelled pr-must-backport and the
PostgreSQL change is not, and writeDoubleQuotedStringPostgreSQL is absent from
26.3 through 26.8, so a SQLite sink calling it would stop compiling as soon as
this is cherry-picked into a release branch.
Both sides are preserved exactly. The diff of this branch against master is
still the same 6 files at +129/-2, no PostgreSQL call site is touched, and no
caller of the pre-existing doubleQuoteString or writeDoubleQuotedString is
re-pointed.
…aping Fix SQL injection through identifiers when inserting into a SQLite table
…rypick/26.3/117421 # Conflicts: # src/Common/quoteString.cpp # src/Common/quoteString.h # src/IO/WriteHelpers.h
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 #117421
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.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
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