Repository navigation
Escape container literals with the target dialect when pushing down to PostgreSQL(26.3) - #125704
Merged
Merged
Conversation
…o PostgreSQL `FieldVisitorToStringPostgreSQL` overrode only the scalar `String` case. Container literals (`Array`/`Tuple`/`Map`/`Object`) fell through to the embedded `FieldVisitorToString`, which escapes nested strings with the regular ClickHouse rules, so a string inside a container was emitted with `\'` instead of `''`. An `IN` list of two or more elements is pushed down as a `Tuple`, so its elements took that path. Under PostgreSQL's default `standard_conforming_strings`, `\'` does not escape the quote, so a `'` in a filter value terminated the literal. Give the visitor explicit `Array`/`Tuple`/`Map`/`Object` overloads that recurse into itself, so the target dialect is kept all the way down. The formatting mirrors `FieldVisitorToString`, including the explicit `tuple(...)` form for single-element tuples. Backport of the container-escaping part of #104217. The upstream commit refactors the dialect visitors into a CRTP base and depends on earlier commits of that pull request, so it is not cherry-picked here; this is the equivalent minimal change for this branch.
Contributor
|
Workflow [BackportPR], commit [b92ad60] Summary: ✅ |
…forming_strings=off `writeQuotedStringPostgreSQL` doubles the quote but leaves backslashes literal. That is safe only while the server keeps `standard_conforming_strings` on. With the setting off, PostgreSQL honours backslash escapes in ordinary literals, so a value holding a backslash directly before a quote is emitted as `'a\''b'`: the backslash escapes the first of the doubled quotes and the second one terminates the literal, putting the remainder of the value outside it. `writeQuotedStringPostgreSQLLossless` emits the `E'...'` form when the value needs it, doubling the backslash as well, so the value is read back identically on either setting. The helper already exists on this branch and is already used by the metadata query paths. Adds the adjacent-backslash-and-quote vector at the top level and through a tuple; both fail with the plain quoter. Found by a pre-push AI review of the previous commit.
1 task
tuanpach
approved these changes
Oct 14, 2026
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.
Where this fix comes from. Both changes are ports of existing upstream commits from
#104217 — "Fix SQLite WHERE predicate pushdown for
strings with special characters":
6f2d55e6abeae44a79cedawriteQuotedStringPostgreSQLLossless, which commit 2 switches to, is from #111516.Which
Fieldtypes are affected, measured by reading the pushed-down query out of theReadFromPostgreSQL: Query:trace with the valuea'b. A type is only exploitable if it both reachesthe formatter (passes type analysis and
isCompatible) and has a text form that can carry auser-controlled quote:
FieldtypeString, scalar'a''b''a''b'offcase is defect 2Tuple, theINlist'a\'b''a''b'Map, inside aTuple[('k', 'a\'b')][('k', 'a''b')]Object, inside aTuple'{"k": "a\'b"}''{"k": "a''b"}'Array, inside aTupleUnsupported types for INArray, top levelisCompatible(Field::Types::Array)Map, top levelObject, top level'{"k": "a''b"}'String, takes the safe pathAggregateFunctionStateDataIllegal types of argumentsCustomTypeFieldFromASTImpl, built solely bySETparsing; its comparison operators all throwObjectwas not in the original report. It does appear injectable when nested, but not through anObject-specific path: on the pushdown path the value is converted to aStringbefore the literalformatter sees it, so
operator() (const Object &)is never reached. Verified by experiment — revertingthat overload to delegate to the plain visitor leaves the end-to-end injection test passing, while
reverting the container recursion in
formatContainer(applyVisitor(derived(), *it)) makes it fail.So both
Objectrows above are the same mechanism as a plain string: what made the nested caseinjectable is the lost dialect in the container recursion, i.e. the
Tupledefect.Mapis different again — it serializes to ClickHouse syntax[('k', …)], and PostgreSQL raisessyntax error at or near "["before reaching the attacker-controlled quote, so a\'in the traceproves only that dialect recursion was lost, not that injection is possible. Upstream commit
ae44a79cedadraws the same line: itsfieldContainsArrayOrMapcheck rejects nestedArrayandMapprecisely because their
[...]form is invalid for an external database.The fix still covers
MapandArrayfor symmetry and depth, not because they are exploitable. Onthis branch what blocks
Arrayis the general type system, not a pushdown-specific guard.A new
Fieldtype would silently fall into the catch-all template, with no compile-time or run-timesignal; that silent degradation is the original shape of this defect.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed escaping of string literals when a predicate is pushed down to PostgreSQL.
Version info
26.3.51.7