Repository navigation
Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key - #118063
Conversation
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]>
Related: ClickHouse/clickhouse-private#69185 Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [e5c73dd] Summary: ✅
AI ReviewSummaryThis PR closes the main column-level Findings❌ Blockers
Tests
Final Verdict❌ Changes requested LLVM Coverage ReportMeasured on commit e5c73dd.
Changed lines: Changed C/C++ lines covered: 225/227 (99.12%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes9 object files changed (+28.51 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units43 translation units recompiled, 396 s compile time in total, 43 of them have a recent master baseline. |
… 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
|
🕵 Both CI failures on 1. Reproducer from the report:
CREATE TABLE t1 (i UInt32, str Nullable(String),
INDEX inv_idx str TYPE text(tokenizer = 'splitByNonAlpha') GRANULARITY 1)
ENGINE = MergeTree ORDER BY i SETTINGS min_bytes_for_wide_part = 0;
INSERT INTO t1 VALUES (1, 'aa'), (2, 'bb'), (3, NULL);
SELECT count() FROM t1 WHERE NOT hasToken(str, 'aa'); -- 2, wrong
SELECT count() FROM t1 WHERE NOT hasToken(str, 'aa') SETTINGS use_skip_indexes = 0; -- 1, correctThe direct read from the text index replaces This is exactly #106922, and the fix is open in #113332 (not yet on 2. Triggered by a fuzzed Nothing to change here: 0 unresolved review threads, the AI review verdict on the head is Approve, and the branch already contains the latest |
Backport #118063 to 26.8: Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key
Two conflicts, both at the two sites this branch gates. Master (ClickHouse#118063, "Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key") started parsing the custom key in the same StorageDistributed branch, to run an access check on it: * InterpreterSelectQuery.cpp: master assigns the parsed key to parallel_replicas_custom_key_ast_to_check, consumed later by the column-grant check. * PlannerJoinTree.cpp: master parses it into custom_key_ast and passes it to buildFilterQueryTree(..., check_access_rights=true). Both sides needed the same parsed AST, so the resolution keeps master's parse and access check untouched and in its original order, and gates only the setSetting("distributed_group_by_no_merge", 2) call on customKeyResultCanSkipMerge() using master's already-parsed AST. That drops the branch's own parseCustomKeyForTable() call at both sites: the key is now parsed exactly once per query, and this branch's diff at each call site shrinks from ten lines to five. buildFilterQueryTree() takes the ASTPtr by value and does not mutate the pointee, so reading it afterwards is safe. No behaviour change relative to either side: master's access check still runs unconditionally, and the no-merge fast path is still taken only when the custom key is a deterministic function of the GROUP BY keys.
…for additional_table_filters and parallel_replicas_custom_key
Resolve the cherry-pick of #118063 against the 26.3 release branch. The 26.3 branch predates several master-only changes the original patch was written on top of, so the mechanical merge could not be used as is: * `buildFilterInfo` takes `const QueryTreeNodePtr &` in 26.3, not `TableExpressionNodePtr`, so `buildFilterQueryTree` and the new `check_access_rights` parameter use the 26.3 signature. * 26.3 has no standalone-subquery wrapping in `buildFilterInfo` and runs `LogicalExpressionOptimizerPass` in the `ASTPtr` overload; the split into `buildFilterQueryTree` keeps that behaviour unchanged. * `checkAccessRights` moved from `PlannerJoinTree.cpp` to `Planner/Utils.cpp` with the storage/storage id/snapshot signature, but without the `StorageAlias` target-table check, which does not exist in 26.3 (`StorageAlias::isTargetTableGranted` was added later on master). * `getSQLSecurityOverriddenContext` in 26.3 applies the parallel replicas callbacks at the end of the function and has no cluster read-task callback, so only the settings handling from the patch was applied. * `checkAccessRightsForSelect` takes four arguments in 26.3 and `generateFilterActions` is not `static`; the new `makeSelectFromTable` / `getTablesWithColumnsForFilter` helpers and `checkAccessRightsForFilter` are adapted accordingly. * The master-only trivial view pushdown to `Distributed` (`optimize_trivial_view_pushdown_to_distributed`) does not exist in 26.3, so that part of the patch and test cases 32-36 are dropped. Test adaptations for 26.3: * Case 06 (a grant on an `ALIAS` column only) is dropped: in 26.3 a plain `SELECT count() FROM t` by a user granted only an `ALIAS` column already fails with `No available columns`, independently of this change. * Cases 15, 16, 17 and 27 pin `parallel_replicas_for_non_replicated_merge_tree` to 1: without it, 26.3's `InterpreterSelectQuery` turns custom-key parallel replicas off on a plain `MergeTree` table before the key is built, so the cases would not exercise the filter at all. The test passes with both analyzers. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Cherry pick #118063 to 26.3: Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key
…onal_table_filters and parallel_replicas_custom_key
- `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]>
Cherry pick #118063 to 26.7: Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key
…onal_table_filters and parallel_replicas_custom_key
Resolve the conflicts of the cherry-pick of #118063 against 26.6: - `PlannerJoinTree.cpp`, `Planner/Utils.cpp`, `Planner/Utils.h`: 26.6 still has `checkAccessRights` taking a `TableNode` (master takes storage, id and snapshot after the `Alias` grant work), so it is moved to `Planner/Utils.cpp` with the 26.6 signature and `checkAccessRightsForFilter` builds a temporary `TableNode` for the check. `TableExpressionNodePtr` does not exist in 26.6, `QueryTreeNodePtr` is used instead. The hunks for the inlined view over a `Distributed` table are dropped: that code path does not exist in 26.6. - `InterpreterSelectQuery.cpp`: `checkAccessRightsForSelect` has no `storage` argument in 26.6. - `StorageInMemoryMetadata.cpp`: applied on top of the 26.6 version of `getSQLSecurityOverriddenContext`. Co-Authored-By: Claude Fable 5.1 <[email protected]>
- Case 06 (filter over an ALIAS column with a grant on the alias only): on 26.6 a trivial `SELECT count()` over a table whose only granted column is an ALIAS column throws a logical error even without any filter, so the user also gets `SELECT(id)`; `secret_token` stays denied, and the case still checks that the grant on the alias is sufficient. - Cases 15, 16, 17, 27 (custom key parallel replicas over a plain `MergeTree`): #112942 is not in 26.6, so the old analyzer disables the parallel replicas together with the custom key filter unless `parallel_replicas_for_non_replicated_merge_tree` is set. Set it. - Cases 32-36 (trivial view over a `Distributed` table inlined into the outer query): the `optimize_trivial_view_pushdown_to_distributed` setting and the code path it exercises do not exist in 26.6, so the cases and the views they read are dropped. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Cherry pick #118063 to 26.6: Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key
…onal_table_filters and parallel_replicas_custom_key
Backport #118063 to 26.6: Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key
Backport #118063 to 26.7: Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key
Backport #118063 to 26.3: Check column-level SELECT grants for additional_table_filters and parallel_replicas_custom_key
The arm read a table from its own row policy through a `SQL SECURITY NONE` view, the one way to do that without recursion, and expected an `additional_table_filters` entry for that table to apply inside the view. Since ClickHouse#118063 such entries are stripped from `DEFINER` / `NONE` view bodies on purpose, so the arm read `[]` on every path, including a local read on master, and could no longer tell a per-read suppression of the entry from a per-context one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Predicates supplied through the
additional_table_filterssetting (and the expression inparallel_replicas_custom_key) were resolved and evaluated over table columns without the column-levelSELECTcheck aWHEREpredicate gets. A user withSELECT(id, public_label)on a table could putsecret_token = '...'into the setting and read the denied column value through the row count. The planner ran its only column check before these filters were resolved, and the legacy interpreter checked only the query's own columns.The fix resolves each settings-provided filter against its table expression and checks the referenced columns with the same
checkAccessRightsused for the query's columns, before the filter DAG is built. It covers the planner (buildAdditionalFiltersIfNeeded,buildCustomKeyFilterIfNeeded, the trivial view pushdown toDistributed, and the initiator side ofparallel_replicas_custom_keywhen the key is shipped to replicas), the legacyInterpreterSelectQuery, andSQL SECURITY DEFINER/NONEviews, whose body no longer inherits the invoker'sadditional_table_filtersorparallel_replicas_custom_key(an access check there would run as the definer; the custom key is still applied to the view's own columns by the outer query). Row policies are unchanged. Column names are collected beforeALIASexpansion so a grant on the alias name keeps working, matching the main path.User-visible changes: an
additional_table_filtersorparallel_replicas_custom_keyexpression over a column the user cannot select (including one coming from a settings profile) now fails withACCESS_DENIED; an invoker'sadditional_table_filtersentry keyed on a table read inside aSQL SECURITY DEFINERorNONEview is no longer applied (filters keyed on the view itself still are); the body of such a view is read without the invoker'sparallel_replicas_custom_key(the key still applies to the view's columns) and, for an initial query, without the internalparallel_replicas_count/parallel_replica_offsetpair (an invoker could otherwise pick the slice of aSAMPLEin the body); a query that declares itself secondary, that is a follower or a client that setsquery_kind, keeps the pair so that follower partitioning inparallel_replicas_mode = 'sampling_key'still works.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Expressions supplied through the
additional_table_filtersandparallel_replicas_custom_keysettings are now subject to the column-levelSELECTcheck, and are no longer injected into the body ofSQL SECURITY DEFINERorNONEviews. Previously a user could evaluate predicates over columns they were not granted.Workflow [PR]
Sync PR [sync-upstream/pr/118063]
Version info
26.8.3.74,26.7.7.84,26.6.5.113,26.3.33.64