Skip to content

Cherry pick #117421 to 26.8: Fix SQL injection through identifiers when inserting into a SQLite table - #121837

Merged
robot-clickhouse-ci-1 merged 10 commits into
backport/26.8/117421from
cherrypick/26.8/117421
Sep 24, 2026
Merged

robot-clickhouse-ci-1 merged 10 commits into
backport/26.8/117421from
cherrypick/26.8/117421

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 9 commits August 31, 2026 19:07
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
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Sep 23, 2026
…rypick/26.8/117421

# Conflicts:
#	src/Common/quoteString.cpp
#	src/Common/quoteString.h
#	src/IO/WriteHelpers.h
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 merged commit 1512c64 into backport/26.8/117421 Sep 24, 2026
1 check passed
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 deleted the cherrypick/26.8/117421 branch September 24, 2026 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants