Skip to content

Escape container literals with the target dialect when pushing down to PostgreSQL(26.3) - #125704

Merged
tiandiwonder merged 2 commits into
26.3from
sec-65381-pg-container-escaping-26.3
Oct 14, 2026
Merged

tiandiwonder merged 2 commits into
26.3from
sec-65381-pg-container-escaping-26.3

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

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"
:

This PR Upstream commit Upstream subject
commit 1 6f2d55e6abe Address review: escape nested literals with the target dialect recursively
commit 2 ae44a79ceda Address review: make PostgreSQL pushdown literals round-trip control characters

writeQuotedStringPostgreSQLLossless, which commit 2 switches to, is from #111516.

Which Field types are affected, measured by reading the pushed-down query out of the
ReadFromPostgreSQL: Query: trace with the value a'b. A type is only exploitable if it both reaches
the formatter (passes type analysis and isCompatible) and has a text form that can carry a
user-controlled quote:

Field type Reaches the formatter Before After Verdict
String, scalar yes 'a''b' 'a''b' safe on the default setting; the off case is defect 2
Tuple, the IN list yes 'a\'b' 'a''b' was injectable
Map, inside a Tuple yes [('k', 'a\'b')] [('k', 'a''b')] dialect lost, but not exploitable — see below
Object, inside a Tuple yes '{"k": "a\'b"}' '{"k": "a''b"}' was injectable, but via the container recursion — see below
Array, inside a Tuple no — — rejected by type analysis: Unsupported types for IN
Array, top level no predicate dropped — rejected by isCompatible (Field::Types::Array)
Map, top level no predicate dropped — —
Object, top level yes '{"k": "a''b"}' same folded to a String, takes the safe path
AggregateFunctionStateData no — — rejected by type analysis: Illegal types of arguments
CustomType no — — only FieldFromASTImpl, built solely by SET parsing; its comparison operators all throw

Object was not in the original report. It does appear injectable when nested, but not through an
Object-specific path
: on the pushdown path the value is converted to a String before the literal
formatter sees it, so operator() (const Object &) is never reached. Verified by experiment — reverting
that 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 Object rows above are the same mechanism as a plain string: what made the nested case
injectable is the lost dialect in the container recursion, i.e. the Tuple defect.

Map is different again — it serializes to ClickHouse syntax [('k', …)], and PostgreSQL raises
syntax error at or near "[" before reaching the attacker-controlled quote, so a \' in the trace
proves only that dialect recursion was lost, not that injection is possible. Upstream commit
ae44a79ceda draws the same line: its fieldContainsArrayOrMap check rejects nested Array and Map
precisely because their [...] form is invalid for an external database.

The fix still covers Map and Array for symmetry and depth, not because they are exploitable. On
this branch what blocks Array is the general type system, not a pushdown-specific guard.

A new Field type would silently fall into the catch-all template, with no compile-time or run-time
signal; that silent degradation is the original shape of this defect.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

  • Merged into: 26.3.51.7

…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.
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
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.
@clickhouse-gh clickhouse-gh Bot added the comp-postgresql PostgreSQL-specific integration (table engine/function, logical replication bridge). label Oct 13, 2026
@tiandiwonder tiandiwonder changed the title Escape container literals with the target dialect when pushing down to PostgreSQL Bacckport 26.3: Escape container literals with the target dialect when pushing down to PostgreSQL Oct 14, 2026
@tiandiwonder tiandiwonder self-assigned this Oct 14, 2026
@tiandiwonder
tiandiwonder merged commit c36b897 into 26.3 Oct 14, 2026
36 checks passed
@tiandiwonder
tiandiwonder deleted the sec-65381-pg-container-escaping-26.3 branch October 14, 2026 02:03
@tiandiwonder tiandiwonder changed the title Bacckport 26.3: Escape container literals with the target dialect when pushing down to PostgreSQL Backport #104217 to 26.3: Escape container literals with the target dialect when pushing down to PostgreSQL Oct 14, 2026
@tiandiwonder tiandiwonder changed the title Backport #104217 to 26.3: Escape container literals with the target dialect when pushing down to PostgreSQL Escape container literals with the target dialect when pushing down to PostgreSQL to 26.3 Oct 14, 2026
@tiandiwonder tiandiwonder changed the title Escape container literals with the target dialect when pushing down to PostgreSQL to 26.3 Escape container literals with the target dialect when pushing down to PostgreSQL(26.3) Oct 14, 2026
@tiandiwonder tiandiwonder removed their assignment Oct 14, 2026
@tuanpach tuanpach self-assigned this Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-postgresql PostgreSQL-specific integration (table engine/function, logical replication bridge).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants