Skip to content

Fix for #101423 GRANT SELECT(column) does not give access to its subcolumns - #103402

Merged
fm4v merged 68 commits into
ClickHouse:masterfrom
BoloniniD:granting-access-to-subcolumns
Nov 10, 2026
Merged

fm4v merged 68 commits into
ClickHouse:masterfrom
BoloniniD:granting-access-to-subcolumns

Conversation

@BoloniniD

@BoloniniD BoloniniD commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #101423

Column-level grants are stored against top-level storage columns, but a query that reads a subcolumn (t.a, m.keys, arr.size0, n.null, json.a.b, ...) reached the access check under the subcolumn name, so GRANT SELECT(t) did not allow SELECT t.a. The new helper ColumnsDescription::getColumnNamesForSelectAccessCheck maps each requested name to the storage column that identifier resolution actually reads (type-aware, including dynamic subcolumns; real columns with a dot in the name resolve to themselves; unknown names are left unchanged and fail closed). It is applied to every column-level SELECT check: the analyzer and the old interpreter, joinGet, Alias, Buffer, Merge (per child schema), MaterializedView, mergeTreeIndex, mergeTreeTextIndex, and hypothetical indexes and projections.

This also closes the opposite direction: with GRANT SELECT ON t and REVOKE SELECT(m), SELECT m.keys used to be allowed. It is now denied.

A subcolumn is treated as one more level below its column, like a column below its table: it inherits the grants of its column, and a GRANT or REVOKE on the exact subcolumn name takes precedence. An explicit entry is detected with the new AccessRights::isGrantedInherited, which returns what a column name inherits from its enclosing level while ignoring a grant or revoke on that exact name: when the exact name differs from that, the subcolumn name is checked as is, otherwise its parent column is checked. The analyzer's table visibility check (checkAccessToTableMetadata, from #117979) also accepts a table when the user has an explicit grant on a subcolumn name, found by the new AccessRights::hasExplicitGrantOnColumnsWithPrefix, so a user whose only grant is GRANT SELECT(`json.a`) can query it.

Compatibility

The table has p Tuple(s String, o String) and j JSON. "Before" is measured on 26.8; since #117979, master additionally denied every row whose only grants are on subcolumn names, because no top-level column was visible. "After" is pinned by 05257_grant_select_subcolumn_explicit_entries.

Grants Before After Change
none nothing allowed nothing allowed same
GRANT SELECT(p) p only; p.s, p.o denied p, p.s, p.o allowed fixed: a column grant covers its subcolumns
GRANT SELECT(`p.s`) p.s only p.s only same (on master before this PR: denied)
GRANT SELECT(`j.a`) j.a only j.a only same (on master before this PR: denied)
GRANT SELECT ON t, REVOKE SELECT(p) p denied; p.s, p.o allowed p, p.s, p.o denied fixed: a revoked column hid its value but leaked its subcolumns
GRANT SELECT ON t, REVOKE SELECT(m) for a Map column m m denied; m.keys, m.values allowed m, m.keys, m.values denied fixed: same leak
GRANT SELECT ON t, REVOKE SELECT(`p.s`) p.s denied; p, p.o allowed p.s denied; p, p.o allowed same
GRANT SELECT ON t, REVOKE SELECT(`j.a`) j.a denied; rest allowed j.a denied; rest allowed same
GRANT SELECT(`p.s`), REVOKE SELECT(p) p.s only p.s only same
GRANT SELECT(p), REVOKE SELECT(`p.s`) p only p, p.s, p.o allowed changed: the revoke removes nothing p.s inherits from the table, so it is a no-op and p.s follows p
a role with GRANT SELECT(p) p only p, p.s, p.o allowed fixed, same as the direct grant
a role with GRANT SELECT(`p.s`) p.s only p.s only same

Known limits, both failing closed:

  • A grant or revoke on a subcolumn name that sets exactly what the name already inherits leaves no trace in the grants tree, so the subcolumn follows its column. For example, GRANT SELECT ON t, REVOKE SELECT(p), GRANT SELECT(`p.s`) denies p.s.
  • The grants of several roles are merged before the check, so a subcolumn revoked explicitly in one role can be denied even if another role grants its column.

A revoke on a subcolumn name never hides the data on its own, because reading the parent column still returns it.

The branch also makes EXPLAIN WHATIF deny index estimates when the table has an effective row policy, consistent with what master already does for mergeTreeIndex and mergeTreeTextIndex. mergeTreeAnalyzeIndexes is left as on master, which intentionally does not enforce row policies there.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A column-level grant now covers the subcolumns of that column: GRANT SELECT(t) allows SELECT t.a, m.keys, arr.size0, n.null and dynamic JSON paths, and a revoked column also denies its subcolumns (previously GRANT SELECT ON t with REVOKE SELECT(m) still allowed SELECT m.keys). A grant or revoke on the exact subcolumn name, such as GRANT SELECT(`json.a`), keeps taking precedence over its column. EXPLAIN WHATIF is now denied on tables with a row policy.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Workflow [PR]
Sync PR [sync-upstream/pr/103402]

Version info

  • Merged into: 26.11.1.1809-master (included in 26.11 and later)

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Apr 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1b49f63]

Summary: ✅


AI Review

Summary

Reviewed #103402 at 1b49f6384e8d8688d3d6b09ec59492a3e0f6d627, including all prior discussion. The change makes parent-column SELECT grants and revokes cover subcolumns, preserves effective exact-name entries, aligns forwarded reads with their authorization snapshots, and guards hypothetical estimates against row-policy disclosure. No actionable findings remain from this review.

The newly selectable subcolumn read is consumed by prepareBuildQueryPlanForTableExpression, which registers its resolved name and type, and TableExpressionData, which preserves them for storage readers. Engines without subcolumn support retain physical read columns.

Validation on the exact PR Fast test binary: 05508, 05569, and 05570 pass with exact reference matches; twelve additional EXPLAIN AST optimize=1 checks pass through the legacy interpreter path. The current CI report reports no failing tests. All 35 prior review threads are resolved; none warrants reopening.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 23, 2026
@BoloniniD
BoloniniD force-pushed the granting-access-to-subcolumns branch from 1d10dc0 to 5caacf1 Compare April 23, 2026 19:23
@BoloniniD
BoloniniD marked this pull request as ready for review April 25, 2026 15:08
@pufit pufit self-assigned this Jun 1, 2026
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
@pufit

pufit commented Jun 5, 2026

Copy link
Copy Markdown
Member

I don't think this solution is correct. It isn't general and misses some other critical paths with the same problem (like StorageMerge)

@BoloniniD
BoloniniD force-pushed the granting-access-to-subcolumns branch from 5d4afe3 to 2f9639e Compare July 2, 2026 16:00
Comment thread src/Storages/StorageBuffer.cpp Resolved
Comment thread src/Storages/StorageMerge.cpp Resolved
Comment thread src/Storages/ColumnsDescription.cpp Resolved
…bcolumn grant on the target

With `GRANT SELECT ON a` (or `GRANT SELECT(p) ON a`) on an `Alias` and ``GRANT SELECT(`p.s`)``
on its target, `SELECT p.s FROM a` was allowed, but `SELECT count() FROM a` and `SELECT 1 FROM a`
threw `ACCESS_DENIED`: the trivial-query fallback looked for candidate subcolumns only in the grants
on the alias itself.

Candidates are now collected from the grants on every table of the alias chain (the new
`StorageAlias::getTargetChain`), and a candidate counts only if the alias allows reading it, checked
the same way as a query that reads it (`ColumnsDescription::getColumnNamesForSelectAccessCheck`),
and the chain grants it. Applied to both the analyzer (`checkAccessRights`) and the old interpreter
(`checkAccessRightsForSelect`). The regression test covers the reverse arrangement and the cases
where the alias does not grant the subcolumn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/StorageMerge.cpp Resolved
alexey-milovidov and others added 4 commits November 8, 2026 04:27
…nversion

A subcolumn that exists only in the `Merge` type of a column (e.g. `s.x` of a
`String` child column `s` exposed as `JSON`) is read through the whole child
column, but the access check of the child authorized `s.x` itself. So a revoke
on `s` did not deny `s.x`, and a grant on `s` alone did not allow it. The read
and the access check now share `getChildColumnToRead`, so the check authorizes
the column that is actually read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… hop

The trivial-query fallback (`SELECT count()`, `SELECT 1`) checked each candidate
name raw across the whole `Alias` chain, so a chain that grants the column on
one hop and the exact subcolumn on another denied it, while `SELECT p.s` through
the same chain is allowed. The new `StorageAlias::filterColumnsSelectableThroughChain`
normalizes each name separately on each hop, as `StorageAlias::read` does, and
both the analyzer and the old interpreter use one shared helper,
`getColumnsSelectableByTrivialQuery`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
alexey-milovidov and others added 2 commits November 8, 2026 07:08
…torage without subcolumn support

`chooseSmallestColumnToReadFromStorage` could return an exactly granted subcolumn (e.g. `p.s`) for a storage
that only reads whole columns, such as `StripeLog` or `Join`, which made `SELECT 1` and `SELECT count()` fail
with `INCORRECT_INDEX` or `NOT_FOUND_COLUMN_IN_BLOCK` once that grant was added. Honor
`IStorage::supportsSubcolumns` when choosing the read column, so such storages always read a physical column.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fm4v
fm4v added this pull request to the merge queue Nov 9, 2026
Merged via the queue into ClickHouse:master with commit 5560335 Nov 10, 2026
187 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 10, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 10, 2026
Since ClickHouse#103402 a subcolumn inherits the grants of its column, and a grant on
the subcolumn's own name is enough to select it and to run `count()`. Two
reads of the fill still used the rules from before:

- The destination columns the fill reads were checked by their raw names,
  so a user with SELECT(t) on the destination could select `t.a` from the
  Buffer but got ACCESS_DENIED for a Buffer-only column with `DEFAULT t.a`.
  They are now mapped with getColumnNamesForSelectAccessCheck, like the
  selected columns.
- The column read for the row count was chosen among whole columns only, so
  with a grant on `j.a` alone it fell back to a column the user may not
  read, which an `Alias` destination then denied. It is now chosen by the
  planner's own chooseSmallestColumnToReadFromStorage (moved out of the
  anonymous namespace of PlannerJoinTree.cpp), so it is the column `count()`
  reads, including granted subcolumns and dynamic paths.

03631 gains both cases; each fails without its change.

Also read a default input from the destination only when the name resolves
to the same column there. With `j JSON, y DEFAULT j.c::UInt64` over a
destination that has a column named `j.c`, the fill read that column after
the flush instead of the path of `j` (9 before the flush, 0 after). The
input is now read through `j`; fixture W of 05337 covers it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov pushed a commit that referenced this pull request Nov 18, 2026
Resolve the conflict in src/Planner/PlannerJoinTree.cpp: keep the 26.10 side and take only the lines of #131371. The `skip_subcolumns` argument of the master call refers to `filtered_by_allowed_columns`, which comes from #103402 (not in 26.10), so the call uses the default, as the replaced `getSmallestColumn` call on 26.10 did.
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 20, 2026
…arget-access

Conflicts in `src/Analyzer/Resolve/IdentifierResolver.cpp` and `src/Storages/StorageAlias.h`.

master made `IdentifierResolver::checkAccessToTableMetadata` a public static taking the storage,
id and snapshot, counted exact subcolumn grants and virtual columns as making the table visible,
and gated the view-source swap on `replaced_storage`. The Alias chain check now runs once the
table is visible by any of those paths, in a file-local `checkAccessThroughAliasChain`, and skips
exactly the view source the analysis swaps in (`checkAccessToTableMetadata` takes
`replaced_storage`). It covers virtual columns, so the per-virtual `isTargetTableGranted` clause
master added for an `Alias` is folded into it.

Since ClickHouse#103402 a subcolumn inherits the grants of its column on each table unless that table has a
grant or revoke on the subcolumn itself (`getColumnNamesForSelectAccessCheck`), and
`StorageAlias::read` decides each hop that way. The chain check maps each name per hop the same
way, so the enumerated subcolumns and the stand-in name for unnamed dynamic paths are no longer
needed: such a subcolumn is visible exactly when its column is. Without the mapping, the chain
case of `05508_grant_select_subcolumn_trivial_query` (a column grant on the middle hop, an exact
subcolumn grant on the target) was refused.

master no longer creates a temporary table with the `Alias` engine (74af670), so the check for
a temporary `Alias` and its test case are dropped. Test cases that master itself now refuses at
execution (a subcolumn of a revoked column, a read through an `Alias` of `system.one` without
`SHOW TABLES` on it) are removed or reduced to what master reads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 23, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. external Author is not a member of the ClickHouse organization pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet