Skip to content

Cherry pick #118063 to 26.7: Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key - #119088

Merged
robot-ch-test-poll2 merged 25 commits into
backport/26.7/118063from
cherrypick/26.7/118063
Sep 11, 2026
Merged

robot-ch-test-poll2 merged 25 commits into
backport/26.7/118063from
cherrypick/26.7/118063

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #118063

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

novikd and others added 23 commits September 4, 2026 11:19
Column-level SELECT grants are not consulted for the columns referenced by
`additional_table_filters`, `parallel_replicas_custom_key`, and for the
underlying table of an `INVOKER` view, so a user with SELECT on only a
subset of a table's columns can still learn about denied columns by passing
a filter expression that references them. This test pins the expected
(fixed) behaviour across both the old and new analyzer, and is RED against
the current unfixed binary.

Related: ClickHouse/clickhouse-private#69185
… filters in the planner

Filters coming from the `additional_table_filters` and `parallel_replicas_custom_key`
settings are supplied by the user, but they were resolved after the planner had already
checked `SELECT` on the columns of the query, so they could read columns the user has no
grant for. Split the filter resolution out of `buildFilterInfo` into `buildFilterQueryTree`
and, for the settings-provided filters only, collect the table columns the resolved filter
reads and run the same column-aware `SELECT` check the query columns get. Row policies are
admin-defined and stay unchecked.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…em from DEFINER view context

`InterpreterSelectQuery` used to apply the filters coming from `additional_table_filters` and
`parallel_replicas_custom_key` without any column-level `SELECT` check, so a user could probe
columns they are not granted. The filter AST is now analyzed as `SELECT <filter> FROM db.table`
and passed through `checkAccessRightsForSelect` next to the main query check, reusing the
`SELECT ... FROM` AST builder extracted from `generateFilterActions` as `makeSelectFromTable`.
For `SQL SECURITY DEFINER`/`NONE` bodies the check would pass as the definer, so
`additional_table_filters` is stripped from the overridden context instead.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Three fixes from the review of the test:

- Case 1 compared `secret_token` against the literal `'T'` instead of the
  secret value in `$T`.
- `run` merged stderr into the captured output, so a server warning printed
  by the client on a successful query leaked into the reference. Observed:
  the legacy-analyzer custom-key cases log `Parallel replicas with custom key
  will not be used ...` when the harness randomizes
  `parallel_replicas_for_non_replicated_merge_tree`, which made case 17 fail.
  Stderr now goes to a separate file and only stdout is printed on success.
- On failure the error code name is taken from the trailing `(CODE_NAME)`
  token instead of a fixed alternation, so an unexpected error still prints a
  token and the following lines do not shift.

Reference values are unchanged.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…t to replicas

The custom key was checked only when the server applied the filter itself
(`parallel_replicas_count > 1`). When the initiator ships the key to the replicas -
a `Distributed` table over a one-shard multi-replica cluster, or a `MergeTree` table
over `cluster_for_parallel_replicas` - the key is spliced into the shipped query's
`WHERE` and evaluated on the replicas under the cluster user, so the initiator never
checked the current user's column-level `SELECT` grants on it.

Check the key's columns on both initiator paths, in the planner and in the legacy
interpreter.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…y the planner helpers

Note in the `additional_table_filters` description that a filter keyed on a table read
through a `SQL SECURITY DEFINER` or `NONE` view is not applied inside the view, since the
view body runs under the definer's context which does not carry the setting.

Use the `ConstInDepthQueryTreeVisitor` alias for `CollectFilterColumnNamesVisitor` and wrap
the over-long signatures of `checkAccessRightsForFilter` and `buildFilterQueryTree`.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
… with traverseQueryTree

Move `checkAccessRights` and `checkAccessRightsForFilter` from the anonymous
namespace of `PlannerJoinTree.cpp` to `Planner/Utils.{h,cpp}`, replace
`CollectFilterColumnNamesVisitor` with `traverseQueryTree`, and turn
`buildFilterInfoWithAccessCheck` into a `check_access_rights` parameter of
`buildFilterInfo`.

The check-only sites (the view-keyed fold, the Distributed-keyed check in the
pushdown and the two initiator custom-key checks) keep calling
`buildFilterQueryTree` plus `checkAccessRightsForFilter`, because
`buildFilterInfo` registers the filter's columns on the table expression data
and would change the columns read from the Distributed table.

Behaviour-neutral refactor.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ITY DEFINER`/`NONE` view body

`getSQLSecurityOverriddenContext` stripped `additional_table_filters` but still carried
`parallel_replicas_custom_key` (and the settings that activate it) into the body context of a
`DEFINER`/`NONE` view. The custom key filter in the body was then resolved and access-checked as
the definer (or with no user), so an invoker whose key names a view column shadowing a denied
table column could observe the table column through the row count.

The overridden context now turns parallel replicas off and clears the key when the invoker uses
custom-key parallel replicas. The key is still applied to the view's own columns by the outer
query, with the invoker's grants.

Adds `SQL SECURITY DEFINER`/`NONE` custom key cases (and a `NONE` `additional_table_filters` case)
to `05076_additional_table_filters_column_access`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…n 05076

The analyzer resolves a parameterized view as a `TableFunctionNode` wrapping the `StorageView`, and
`checkAccessRightsForFilter` has a dedicated branch for it that nothing exercised. Add cases with a
`SQL SECURITY DEFINER` parameterized view whose body reads the denied column as the definer, so the
view's own column-level grants are the only gate: `additional_table_filters` keyed by the view over an
ungranted column is denied (bare and database-qualified key), over a granted column it works, and the
plain read still returns every row.

The cases run with the analyzer only, because the legacy interpreter does not apply
`additional_table_filters` keyed by a parameterized view at all.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…INER`/`NONE` body even when the invoker's context does not activate it

`drop_custom_key` was derived from `canUseParallelReplicasCustomKey` of the invoker's context only. When the invoker supplied just `parallel_replicas_custom_key` (with the follower-side `parallel_replicas_count` / `parallel_replica_offset`) and the activation settings came from the definer's profile after `setUser`, the key travelled through `changed_settings` into the `SQL SECURITY DEFINER` / `NONE` body context and was evaluated there as the definer, reopening the row-count probe of a denied column.

The key is now dropped both when the invoker's context has custom-key parallel replicas active and when the query supplied the key at all.

Test `05076` gains case 25: a definer whose settings profile enables custom-key parallel replicas, and an invoker that only supplies the key; the parameterized-view cases are renumbered to 26-29.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ble when a trivial view is pushed down to it

With `optimize_trivial_view_pushdown_to_distributed`, the analyzer inlines a trivial view over a `Distributed` table and hands the read to the `Distributed` table itself. `StorageDistributed::read` then ships `parallel_replicas_custom_key` to the replicas as a filter over the columns of that table, but the column-level `SELECT` check for the key only ran when the original storage was already a `Distributed` table, so an invoker could probe a denied column of the underlying table through the row count. Run the same check against the `Distributed` table node in the pushdown branch.

Test `05076`, cases 30-32: an `INVOKER` trivial view over the one-shard, three-replica `Distributed` table, with and without the pushdown.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…gacy interpreter

`checkAccessRightsForFilter` and `generateFilterActions` analyzed the synthetic `SELECT <filter> FROM db.table`
with an empty `TablesWithColumns`, so `TreeRewriter` never translated table-qualified names. On `enable_analyzer = 0`
a filter such as `{'db.t': 'db.t.secret_token = ...'}` or a `parallel_replicas_custom_key` of `db.t.id` failed with
`UNKNOWN_IDENTIFIER` during name resolution, while the same predicate in `WHERE` is resolved (and access-checked).

Pass the first table of the query (`JoinedTables::tablesWithColumns`) to both helpers, the same metadata the main
query is analyzed with, so a granted qualified predicate works and a denied one reaches the column-level `SELECT`
check. Test `05076`: case 03 now expects `ACCESS_DENIED` on both interpreters; new cases 26 and 27 cover a granted
qualified `additional_table_filters` predicate and a qualified `parallel_replicas_custom_key`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…rivial view shadowing a denied column

The test launched a client process per case (61 of them), which took 85-100 s even in the debug
and TSan runs and exceeded the 180 s limit in the ASan flaky check, where three copies run at once:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118063&sha=21cfac6cda3e8bdba06a75e41ee9f7572075289a&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29
Running the cases through `curl` over HTTP brings the test from 30 s to under 2 s on a release build.

Also add cases 35-36: an `SQL SECURITY INVOKER` trivial view over the three-replica `Distributed`
table with a view column shadowing the denied column of the table (`'' AS secret_token`), with the
trivial view pushdown on and off. The `parallel_replicas_custom_key` is shipped to the replicas as a
filter over the columns of the `Distributed` table on both paths (the INVOKER body reads the table
as the invoker), so it resolves against the table, not the view, and is denied on both.

#118063

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The randomized settings `enable_parallel_replicas` and
`parallel_replicas_for_non_replicated_merge_tree` made case 06 of
`05076_additional_table_filters_column_access` return `ACCESS_DENIED`
instead of the count in the distributed-plan job.

A grant on an `ALIAS` column alone does not survive parallel replicas
with a serialized query plan: the replica checks the grant on the whole
table. This is pre-existing behavior unrelated to the change here - a
plain `SELECT count() FROM t WHERE alias_col = '...'` fails the same way
with `enable_parallel_replicas = 1`, `serialize_query_plan = 1` and no
`additional_table_filters` at all.

So pin `enable_parallel_replicas = 0` in that case to keep it about the
filter and not about the randomized settings. Verified against a local
server with `process_query_plan_packet` enabled: the test reproduces the
CI diff without the pin and passes with it, both with and without the
randomized settings.

Report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118063&sha=107cfa7522a4dc7d754fce3a316178e81949864f&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%20selected%20tests%29
PR: #118063
`parallel_replicas_count` and `parallel_replica_offset` are internal settings:
only the initiator puts them on the wire, in `MultiplexedConnections` and
`HedgedConnections`, so only a secondary query can legitimately carry them. On an
initial query they can only come from the invoker, and they were passed straight
into the overridden context of a `SQL SECURITY DEFINER`/`NONE` body together with
the rest of the settings changes.

`MergeTreeDataSelectExecutor::getSampling` splits an active `SAMPLE` into
`parallel_replicas_count` pieces and reads the piece numbered
`parallel_replica_offset`, and for a body that samples explicitly this happens
regardless of `allow_experimental_parallel_reading_from_replicas`. The invoker
could therefore choose which slice of the definer's sample the body reads and use
the aggregate over each slice as an oracle over the sampling key of a table it has
no grants on.

Strip both settings from the changes applied to the body context when the outer
query is not a secondary query. Verified on a table of 100 rows sampled `1/2`:
before the change a `DEFINER` view returned 36 for
`parallel_replicas_count = 2, parallel_replica_offset = 1`, now it returns 100,
while an `INVOKER` view still returns 36.

Test `05076_additional_table_filters_column_access` cases 37-39.
Replace the three long comment blocks around the parallel replicas
settings with one line each.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`buildFilterQueryTree` gains a `check_access_rights` flag and runs
`checkAccessRightsForFilter` right after `QueryAnalysisPass`, so the
check sees the filter before `LogicalExpressionOptimizerPass` and
before `collectSourceColumns` expand ALIAS columns. The callers no
longer have to call the check themselves; it is now `static` in
`Planner/Utils.cpp`.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…R and NONE view bodies unconditionally

`getSQLSecurityOverriddenContext` only stripped `parallel_replicas_count` and
`parallel_replica_offset` when the query kind was not `SECONDARY_QUERY`. The
query kind is declared by the client (`clickhouse-client --query_kind
secondary_query`; `TCPHandler` only validates the enum value), so a restricted
user could keep the pair alive inside a `SQL SECURITY DEFINER` or `SQL SECURITY
NONE` view body and pick a slice of the definer's `SAMPLE`: the full sample of
the test view is 100 rows, the invoker-chosen half is 36.

Remove the gate so the pair is always stripped, and add a regression case that
declares a secondary query from the native client.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…y queries in DEFINER and NONE view bodies

The unconditional strip of the pair from a `SQL SECURITY DEFINER`/`NONE`
view body also removed it on the followers of a real parallel-replicas
read: every follower then re-read the whole `SAMPLE` of the body instead
of its own slice, so a `SELECT sum(c)` through a `Distributed` table over
such a view in `sampling_key` mode returned `300` instead of `100`.

Gate the strip on the query kind: an initial query, where only the
invoker could have supplied the pair, still loses it; a secondary query
keeps it. The query kind is declared by the client and not
authenticated, so a client that sets it can still choose the slice of a
body's `SAMPLE` - that is indistinguishable from a follower by
construction and is not changed by an interserver secret.

Test cases 40 and 41 read the sampled `DEFINER` and `SQL SECURITY NONE`
views through a `Distributed` table over a one-shard, three-replica
cluster and pin the follower result.

Related: ClickHouse/clickhouse-private#69185

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…rs-access

Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key
@robot-clickhouse robot-clickhouse 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-critical-bugfix labels Sep 9, 2026
alexey-milovidov and others added 2 commits September 11, 2026 16:49
…ypick/26.7/118063

# Conflicts:
#	src/Planner/PlannerJoinTree.cpp
#	src/Planner/Utils.cpp
#	src/Storages/StorageInMemoryMetadata.cpp
- `checkAccessRightsForSelect` in `InterpreterSelectQuery` has no `storage` parameter on 26.7 (it was added on master together with `StorageAlias`), so `checkAccessRightsForFilter` calls the four-argument form.
- The test cases that exercise `optimize_trivial_view_pushdown_to_distributed` are dropped: the view pushdown does not exist on 26.7. The two non-pushdown cases over the trivial `INVOKER` views are kept.
- On 26.7 the legacy interpreter disables parallel replicas for a plain `MergeTree` table unless `parallel_replicas_for_non_replicated_merge_tree` is enabled (master gates this on task-based parallel replicas only), so the custom-key cases over `t` enable it to keep exercising the custom key filter and its access check with `enable_analyzer = 0`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@robot-ch-test-poll2
robot-ch-test-poll2 merged commit 4d21135 into backport/26.7/118063 Sep 11, 2026
1 check passed
@robot-ch-test-poll2
robot-ch-test-poll2 deleted the cherrypick/26.7/118063 branch September 11, 2026 18:11
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-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants