Repository navigation
Substitute query parameters in row policy filters and reject unsubstituted Identifier parameters - #122688
Conversation
ASTCreateRowPolicyQuery keeps its filters outside `children`, and
ReplaceQueryParameterVisitor had no case for them, so CREATE / ALTER ROW
POLICY neither substituted nor rejected a query parameter in a USING
clause and stored the placeholder. A stored `{db:Identifier}.keys` then
read `keys` from the reader's current database, a value parameter made
every read of the table fail with UNKNOWN_QUERY_PARAMETER, and an unset
parameter was accepted.
Visit the filters explicitly in ReplaceQueryParameterVisitor, writing a
replaced top-level parameter back into `filters`, and in
QueryParameterVisitor, so an HTTP handler whose query is CREATE / ALTER
ROW POLICY also discovers and binds the filter parameters.
The filters stay out of `children` on purpose: a filter is evaluated when
a table is read, in the reading query's context. Registering them as
children exposes a stored filter to the generic DDL rewriters, e.g.
AddDefaultDatabaseVisitor::visitDDL of ON CLUSTER, which replaces
`currentDatabase()` with the initiator's database, and the create-time
normalizers of executeQuery (construction settings, UNION / EXCEPT modes,
WITH propagation).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…builder
Text parsed outside executeQuery never meets ReplaceQueryParameterVisitor:
row policy filters stored by an older version or defined in users.xml,
additional_table_filters, additional_result_filter,
parallel_replicas_custom_key. An `{x:Identifier}` there reached
QueryTreeBuilder, which dropped the parameter and built a name with an
empty part. Resolution then read a table from the current database or
failed with a misleading error, and any error message that converted the
node back to AST hit the `!part.empty()` assertion of ASTIdentifier, so
debug and sanitizer builds aborted (Stress test (amd_msan), STID 2508-3010).
Throw UNKNOWN_QUERY_PARAMETER, like the builder already does for an
unsubstituted value parameter, wherever it turns an identifier that a
parameter-capable parser produces into an analyzer name.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) and my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20260928-134400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260928-124800 |
|
Workflow [PR], commit [2a51350] Summary: ✅
AI ReviewSummaryThis PR substitutes query parameters in row-policy filters and masking-policy expressions, discovers them for HTTP handlers, and rejects unsubstituted Final VerdictStatus: ✅ Approve. Both added regression tests passed in the PR's Fast test report. No further changes are required by this review. |
ASTCreateMaskingPolicyQuery keeps its UPDATE assignments and WHERE
condition outside `children`, like the row policy filters, so
ReplaceQueryParameterVisitor and QueryParameterVisitor skipped them: a
query parameter there reached the interpreter unsubstituted
(`UPDATE k = {v:UInt64}` with `param_v` set), and an HTTP handler whose
query is CREATE MASKING POLICY did not bind it from the request.
Visit both members explicitly in both visitors, as for the row policy
filters. Masking policies are executable only in ClickHouse Cloud, so the
tests check what this build can observe: an unset parameter in UPDATE or
WHERE now fails with UNKNOWN_QUERY_PARAMETER instead of
SUPPORT_IS_DISABLED, and a handler binds the parameter (a value that does
not parse fails with BAD_QUERY_PARAMETER).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Commit See the job log for details. |
|
The same unsubstituted placeholder aborted two more
I checked this route with the
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The load-time check `checkAccessEntityHasNoQueryParameters` uses
`analyzeReceiveQueryParams`, which now also visits row policy filters and
masking policy expressions. A row policy stored by an older version with an
unsubstituted parameter in its filter (`USING k = {x:UInt64}`) was then
rejected while loading the access storage:
- on a normal restart every query of every user failed with
INCORRECT_ACCESS_ENTITY_DEFINITION, because the row policy cache reads all
policies;
- on a rebuild of the lists (`need_rebuild_lists.mark`, SYSTEM RELOAD USERS)
the policy was skipped and dropped from the lists, so the rows it hid
became readable.
Skip policy definitions in that check, as before: such a policy loads again
and reading its table fails with UNKNOWN_QUERY_PARAMETER.
Found by the AI review of ClickHouse#122688 after the merge with ClickHouse#109696.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
|
@groeneai at this point you can review where query parameters are not substituted in the remaining statements |
| EXPECT_THROW(deserializeAccessEntity("ATTACH ROLE r1; ATTACH GRANT SELECT ON *.* TO {g:Identifier};"), Exception); | ||
| } | ||
|
|
||
| TEST(AccessEntityIO, LoadsPolicyWithQueryParameterInExpression) |
|
The 26.10 CI needs this, although the issue happens in 26.3. Backporting to 26.8 for now, later to other release branches if needed. |
Backport #122688 to 26.10: Substitute query parameters in row policy filters and reject unsubstituted `Identifier` parameters
Kept only the delta of #122688: `checkAccessEntityHasNoQueryParameters` (from "Support query parameters in user and role identifiers") and `getColumnAliasNames` are not on 26.8, so the hunk editing the former is dropped and only `throwIfQueryParameter` is added to `QueryTreeBuilder.cpp`. #122688 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Kept only the delta of #122688: `checkAccessEntityHasNoQueryParameters` (from "Support query parameters in user and role identifiers") and `getColumnAliasNames` are not on 26.9, so the hunk editing the former is dropped and only `throwIfQueryParameter` is added to `QueryTreeBuilder.cpp`. #122688 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ilters and reject unsubstituted `Identifier` parameters
…ilters and reject unsubstituted `Identifier` parameters
Backport #122688 to 26.9: Substitute query parameters in row policy filters and reject unsubstituted `Identifier` parameters
Backport #122688 to 26.8: Substitute query parameters in row policy filters and reject unsubstituted `Identifier` parameters
`ReplaceQueryParameterVisitor` substitutes `{ts:DateTime64(3, 'UTC')}` as
`_CAST(<DecimalField>, 'DateTime64(3, \'UTC\')')`. `ASTLiteral` prints a
`DecimalField` as a quoted decimal string, so once the substituted query is
formatted and parsed again (a row policy filter, a column DEFAULT, a
Replicated mutation, ON CLUSTER DDL) the value becomes
`_CAST('20240101', 'DateTime64(3, \'UTC\')')`: a cast from String, which parses
date-time text. With `cast_string_to_date_time_mode = 'best_effort'` an
8-digit number is read as YYYYMMDD, so a row policy `ts <= {ts:DateTime64(...)}`
with a value before 1973-03-03 lets through rows it must hide, a negative value
fails CANNOT_PARSE_DATETIME, and a `Time64` value (decimal seconds are not
`Time64` text) fails CANNOT_PARSE_TEXT on every read. Since ClickHouse#122688 row policy
filters are stored with the parameters substituted, so this is reachable on
26.8+.
Build the substituted AST for any type containing DateTime64 / Time64 with
`columnConstantToExactLiteralAST`, the helper added by ClickHouse#95055 for exactly this
serialization problem in `ConstantNode::toASTImpl` and `ReadFromRemote`: it
writes `_CAST(_CAST('<ticks>', 'DecimalNN(S)'), '<type>')` for every leaf,
also inside Array / Tuple / Map / Nullable, with a carrier wide enough for
DateTime64(9). The parameter visitor runs on the AST before any query tree
exists, so it was the one serialization site that helper did not cover.
Casting from date-time text instead (adding the types to `needCastFromString`)
was rejected: local date-time text is ambiguous across DST overlaps, measured
one hour off for '1730615400.5' as DateTime64(1, 'America/New_York') even in
the query itself. Plain Decimal parameters already round-trip and are left
unchanged, as are Variant / Dynamic / Object and custom-serialization types,
which keep the cast-from-String path.
Already stored broken policies and defaults are not repaired; re-run the
ALTER with the parameter.
Closes ClickHouse#127330
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix query parameters in the filter of
CREATE ROW POLICYandALTER ROW POLICYbeing stored unsubstituted: a{db:Identifier}table qualifier was resolved in the current database of the query reading the table, and a value parameter made every read of the table fail. The parameters are now substituted when the policy is created, and anIdentifierquery parameter that reaches a filter unsubstituted (for example inadditional_table_filters) fails withUNKNOWN_QUERY_PARAMETER.Description
On 26.8,
CREATE ROW POLICY p ON t USING k IN (SELECT k FROM {db:Identifier}.keys)withparam_dbset stores theplaceholder, so each reader's current database decides which
keysfilters the rows, andALTER ROW POLICY p ON t USING k > {v:UInt64}makes every read oftfail withUNKNOWN_QUERY_PARAMETER.Found by
Stress test (amd_msan): the AST fuzzer put{fuzz_param_0:Identifier}into the row policy of05257_memory_prewhere_global_in_sets_built_in_place, and the next read aborted withLogical error: '!part.empty()'(STID 2508-3010): report.Two defects:
ASTCreateRowPolicyQuerykeeps its filters outsidechildren, andReplaceQueryParameterVisitorhad no case forthem. They are now visited explicitly there and in
QueryParameterVisitor(parameter discovery of HTTP handlers).They stay out of
childrenon purpose: a filter is evaluated later, in the reading query's context, and the genericDDL walkers would rewrite it (
ON CLUSTERreplacescurrentDatabase()with the initiator'sdatabase).
QueryTreeBuilderdropped the parameter of an unsubstituted{x:Identifier}and built an identifier with an emptypart. Text that never meets the visitor (stored policies,
additional_table_filters,additional_result_filter)reached it, and converting the node back to AST aborted debug builds. It now throws
UNKNOWN_QUERY_PARAMETER, as italready did for value parameters.
CREATE MASKING POLICYkeeps itsUPDATEandWHEREexpressions outsidechildrentoo, and both visitors now visitthem. It runs only in ClickHouse Cloud: the tests check the error for an unset parameter and the handler binding.
A policy stored by an older version with a parameter in its filter still loads (the load-time check of access entities does not look into policy expressions) and fails on read with
UNKNOWN_QUERY_PARAMETER; recreate it with the parameter set.Tests:
05291(policies, filter settings) and05292(HTTP handler binding) fail on master.Workflow [PR]
Sync PR [sync-upstream/pr/122688]
Version info
26.11.1.505-master(included in26.11and later)26.10.2.76,26.9.25.13,26.8.34.11