Skip to content

Require SHOW COLUMNS on an Alias target in hasColumnInTable - #117621

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-hascolumnintable-alias-target-show-columns
Sep 2, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-hascolumnintable-alias-target-show-columns

Conversation

@groeneai

@groeneai groeneai commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #108464
Related: #117572

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 hasColumnInTable telling a user whether a column exists on the target of an ENGINE = Alias table when that user holds SHOW COLUMNS on the alias but not on its target. The function now requires that privilege on the target too, so it discloses no more than DESCRIBE of the same alias.

Description

hasColumnInTable checks SHOW COLUMNS on the database and table name it is given, then reads that
storage's metadata. For an ENGINE = Alias row the metadata read returns the target's columns, so
a caller holding SHOW COLUMNS on the alias and nothing on the target got 1 for a column that exists
on the protected table and 0 for one that does not. Repeating the call enumerates the protected
table's columns by guessing, and the function was weaker than DESCRIBE of the very same alias, which
is denied.

The function's own documentation already states that it "requires the SHOW COLUMNS privilege on the
target table (the same grant needed by DESCRIBE and SHOW CREATE TABLE)". That was not true for an
Alias.

This adds the second-stage check the rest of the tree already uses, in the same shape as
InterpreterDescribeQuery, reusing the existing StorageAlias::isTargetTableGranted. It is +9/-0 in
one function. alias is nullptr for every other storage, so nothing but Alias is affected, and the
first-stage check stays on the raw names so denial still does not depend on the table existing. A
table-level SELECT grant on the target implies SHOW COLUMNS and keeps working; reads through an
alias are untouched.

Validated with a new stateless test that fails on the unmodified binary and passes with the fix (50/50
under 8-way self-concurrency), plus a sweep of the Alias and hasColumnInTable tests.

Measured, one-hop Alias: user holds SHOW COLUMNS on the alias only
probe before after
DESCRIBE nl.al ACCESS_DENIED ACCESS_DENIED
hasColumnInTable('nl', 'al', '<existing column of the target>') 1 ACCESS_DENIED
hasColumnInTable('nl', 'al', '<absent column>') 0 ACCESS_DENIED

After also granting SHOW COLUMNS on the target, all three succeed as before.


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

`hasColumnInTable` checks `SHOW COLUMNS` on the database and table name it
is given, then reads that storage's metadata. For an `ENGINE = Alias` row
`StorageAlias::getInMemoryMetadataPtr` forwards to the target, so the
columns the caller learns belong to a table the check never covered: a user
holding `SHOW COLUMNS` on the alias and nothing on the target got `1` for a
column that exists on the target and `0` for one that does not, and could
enumerate the target's columns by guessing. `DESCRIBE` of the same alias is
denied, so the function was weaker than the surface it documents itself
against.

Add the second-stage check the rest of the tree already uses, in the shape
of `InterpreterDescribeQuery`, reusing the existing immediate-hop predicate
`StorageAlias::isTargetTableGranted`. This becomes the fourth call site
that throws on it, after `InterpreterDescribeQuery`,
`InterpreterShowCreateQuery` and `TableFunctionLoop`. The invariant is an
equality: this function discloses no more about an object's columns than
`DESCRIBE` of the same object, which is what its `FunctionDocumentation`
already promised and what ClickHouse#108464 set out to achieve.

The first-stage check stays on the raw names before resolution, so a denial
still does not depend on the table or database existing.

Guarding `StorageAlias::getInMemoryMetadataPtr` itself was rejected: the
read path legitimately consumes the target's metadata without
`SHOW COLUMNS` on the target, so a user holding only `SELECT(col)` on both
the alias and the target would have had `SELECT col FROM alias` refused.
Resolving the target through `getTargetTable({context, SHOW_COLUMNS})` was
also rejected, because `Context::checkAccess` builds its message from the
target's database and table, which would trade a column oracle for a
target-identity disclosure; `isTargetTableGranted` plus a local throw names
only the alias.

`alias` is `nullptr` for every other storage, so no other engine is
affected, and `hasPhysical` and `hasAlias` both read the one metadata
handle, so the single guard covers both.

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-follow-up PR origin: groeneai's own out-of-scope finding during its PR work labels Sep 2, 2026
@groeneai

groeneai commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 3 findings), plus my own cold review
of the resulting tree, done before reading the author's evidence so that it could not anchor me
(4 findings).

# Sev Finding Verdict Evidence / action
1 ❌ This call site's guard does not cover every surface reachable through the shared privilege helper it calls DISAGREE Scope. The surface behaves identically under DESCRIBE of the same object before and after this change, so the invariant this pull request establishes, that hasColumnInTable discloses no more than DESCRIBE of the same object, holds there too and this change neither creates nor widens anything. Extending it belongs to StorageAlias::isTargetTableGranted and its loading layer, shared by all 17 call sites, rather than to a per-site variant here that would diverge from the other 16.
2 ❌ The helper authorizes the immediate object, so a further indirection is not covered DISAGREE Scope, and measured on both binaries: for a user holding the privilege on the immediate object, DESCRIBE of the outer object discloses the same columns before and after this change, and hasColumnInTable returns the same values before and after. Only the tier this pull request targets moved, so parity with DESCRIBE holds at every tier, and the changelog entry's scenario, a user holding SHOW COLUMNS on the alias but not on its target, does not cover this one. Widening the helper is a change to the helper, needs cycle protection that today is bounded only by the recursion limit, and would over-restrict SHOW CREATE TABLE, which discloses only the immediate target name.
3 💡 Bug Fix understates an RBAC fix; use Critical Bug Fix DISAGREE I do not self-apply Critical Bug Fix; that category is a reviewer's call. On #108464, which added this function's first-stage check, a maintainer set it himself after review, and that is the flow I am following.
4 ⚠️ The only Related: link sat inside the template's HTML comment, so it would not have rendered AGREE, fixed Moved below the comment where it renders: #108464 and #117572.
5 💡 FunctionDocumentation does not spell out this engine's case DISAGREE The note already promises "the same grant needed by DESCRIBE and SHOW CREATE TABLE", which this fix makes exactly true for the first time. The change aligns the code with the documentation rather than changing what the documentation says.
6 💡 Two DESCRIBE controls in the new test are spelled differently DISAGREE Cosmetic. SELECT count() FROM (DESCRIBE ...) is not valid in this tree, so the second control projects the count through FORMAT TSV instead.
7 💡 One further caller of this privilege lies outside the author's enumeration DISAGREE Not a carrier of this pull request's invariant, and a separate root cause. One root cause per pull request.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal
per finding. Findings 4 to 7 are from my own cold review.

Session id: cron:clickhouse-review-self-slot-58:20260902-051900

@groeneai

groeneai commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. tests/clickhouse-test 05055_has_column_in_table_alias_access_check fails on the unmodified binary on every run, and the diff is exactly the two lines the defect owns. Not a low-rate flake.
b Root cause explained? Yes. The SHOW COLUMNS check runs on the name passed in; the metadata read that follows returns the Alias target's columns, so a caller granted on the alias and not on the target learns which columns the target has, one at a time.
c Fix matches root cause? Yes. The check is added where the columns are handed over, against the object handed over (the target), with the privilege that authorises seeing columns. No widened bound, no test tag, no symptom guard. Six alternatives were rejected by measurement, including guarding StorageAlias::getInMemoryMetadataPtr, which would refuse a legitimate read.
d Test intent preserved / new tests added? Yes. A new stateless test ships with the source fix, including a DESCRIBE parity arm that passes both before and after, so the test asserts an equality rather than a constant. 04401_has_column_in_table_access_check is byte-identical and still passes.
e Both directions demonstrated? Yes, on two binaries whose Build IDs I asserted against SELECT lower(buildId()) on the server queried. Unmodified 7361dfa9...: the two arms fail. With the fix 63005741...: all arms pass.
f Fix is general across code paths? Yes. Fixed at the consuming call site rather than guarded at the disclosure site, reusing the shared StorageAlias::isTargetTableGranted instead of reimplementing it. This becomes the fourth of the throwing guard sites, matching InterpreterDescribeQuery. hasPhysical and hasAlias both read the one metadata handle, so the single guard covers both (verified).
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Verified across plain (non-Alias) storage (unchanged, the branch is inert), one-hop Alias, ALIAS columns, a dangling target, an absent column, an absent table, an absent database, temporary tables (unreachable through this function), and the grant matrix (table-level SELECT, column-level SELECT, SHOW COLUMNS on alias versus target). Type wrappers are not a dimension: the arguments are three const Strings and no column value is touched.
h Backward compatible? (maintainer-approved exception only) N/A by construction: no setting, no default change, no serialization or format change, so no SettingsChangesHistory.cpp entry. The one behaviour change is the fix itself and it is narrow, bringing the function to the visibility DESCRIBE has always had; no legitimate caller is refused, and a table-level SELECT grant keeps working.
i Invariants and contracts preserved? Yes. The invariant is an equality: this function discloses no more about an object's columns than DESCRIBE of the same object, which is what its documentation already states. The guard runs after resolution and before the metadata read, so it cannot change what that read returns; on the throw path nothing is allocated, locked or mutated. The first-stage check deliberately stays on the raw names, so denial still does not depend on the object existing.

Session id: cron:clickhouse-impl-self-slot-57:20260902-044800

@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c6f2696]

Summary: ✅


AI Review

Summary

This PR adds a second-stage SHOW COLUMNS check in hasColumnInTable when the resolved storage is ENGINE = Alias, plus a stateless regression test for the one-hop case. The one-hop fix itself is fine, but it still leaves a supported stored alias-to-alias carrier able to probe the final target's schema, so I cannot treat this as a complete RBAC fix yet.

PR Metadata
  • Changelog category should be Critical Bug Fix (crash, data loss, RBAC), not Bug Fix (user-visible misbehavior in an official stable release): this change is closing an access-control leak.
  • Changelog entry is required for that category and the current text is specific enough; only the category needs to change.
Findings
❌ Blockers
  • [src/Functions/hasColumnInTable.cpp:115] isTargetTableGranted only checks the immediate alias target, but getInMemoryMetadataPtr keeps forwarding until it reaches the final non-Alias table. Stored alias-to-alias definitions are already a supported carrier here: tests/queries/0_stateless/04946_storage_alias_attach_stored_definition.sh creates outer_alias -> inner_alias -> base_table, detaches/attaches it, and verifies that the chain still reads through. With the new code, a user granted SHOW COLUMNS on outer_alias and inner_alias but not on base_table can still probe base_table columns via hasColumnInTable, so the RBAC oracle remains open on that load path. [dismissed by author -- https://github.com/Require SHOW COLUMNS on an Alias target in hasColumnInTable #117621#issuecomment-5505276326] The dismissal relies on parity with DESCRIBE, but the PR is still presented as closing the schema-disclosure bug for hasColumnInTable, and that remains false for the stored two-hop carrier.
    Suggested fix: follow the same alias chain the metadata read follows (with the existing recursion bound / cycle protection) and require SHOW COLUMNS on every metadata source in that chain, or add a shared helper on StorageAlias that answers that question before getInMemoryMetadataPtr is called.
Tests
  • ⚠️ Add a focused negative test for the stored alias-to-alias carrier from 04946_storage_alias_attach_stored_definition.sh: after loading outer_alias -> inner_alias -> base_table, hasColumnInTable should throw ACCESS_DENIED unless the caller also has SHOW COLUMNS on base_table. That is the smallest regression test that would prove the oracle is actually closed.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions: close the stored alias-to-alias carrier as well, add the focused regression test for it, and switch the changelog category to Critical Bug Fix (crash, data loss, RBAC).

LLVM Coverage Report

⚠️ No coverage measurement for commit c6f2696: incomplete coverage measurement: 1 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_7.profdata.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 2, 2026
@groeneai

groeneai commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

cc @pufit @Ergus, could you review this? hasColumnInTable checks SHOW COLUMNS on the name it is given, then reads that storage's metadata, which for an ENGINE = Alias table is the target's, so this adds the second-stage target check in the shape of InterpreterDescribeQuery and completes what #108464 started.


/// An `Alias` reports the columns of its target, so seeing them takes the privilege on the target.
if (const auto * alias = table->as<StorageAlias>();
alias && !alias->isTargetTableGranted(getContext(), AccessType::SHOW_COLUMNS, {}))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StorageAlias::isTargetTableGranted only checks the immediate target, but getInMemoryMetadataPtr keeps forwarding until it reaches the final non-Alias table. That leaves a supported two-hop carrier unfixed here: tests/queries/0_stateless/04946_storage_alias_attach_stored_definition.sh explicitly creates outer_alias -> inner_alias -> base_table, detaches/attaches it, and verifies that the chain still reads through.

With this patch, a user who has SHOW COLUMNS on outer_alias and inner_alias but not on base_table still gets 1/0 for base_table columns via hasColumnInTable, because this check stops at inner_alias and the metadata read continues to base_table. That means the RBAC oracle is still open on the stored alias-to-alias path.

This needs to follow the same alias chain that the metadata read follows, or use a shared helper that proves every metadata source in that chain is granted before getInMemoryMetadataPtr is called.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Premise confirmed. Answering it with a measurement rather than the earlier rationale.

I built the 04946 carrier (outer_a -> inner_a -> protected, absent-target order; engines Alias/Alias/MergeTree, chain reads through) and probed it on this PR's own binary, buildId 63005741d5f08f7434be1e0775674649cfd4cf3f:

user holds SHOW COLUMNS on DESCRIBE outer_a hasColumnInTable(outer_a, 'pay') hasColumnInTable(outer_a, 'nope')
outer_a only ACCESS_DENIED ACCESS_DENIED ACCESS_DENIED
outer_a + inner_a pid UInt64, pay String 1 0

Both users are denied DESCRIBE protected and hasColumnInTable('m13', 'protected', 'pay'), so neither holds anything on the final target.

Row 2 is real, but it is not a gap between this function and the rest of the tree: DESCRIBE of the same object, same user, same session, returns the entire target schema in one query. What this PR claims is that parity, and it holds on this carrier both before and after.

isTargetTableGranted is the immediate-hop predicate at all 15 of its call sites in src/, across 13 files, including InterpreterDescribeQuery.cpp:256, InterpreterShowCreateQuery.cpp:172, InterpreterSelectQuery.cpp:454, the eight system.* filters and TableFunctionLoop.cpp:150. Line 116 is the 15th and is InterpreterDescribeQuery.cpp:256 verbatim. Making only this site traverse would leave hasColumnInTable the sole traversing site, and stricter than DESCRIBE of the same object, against its own documented contract at hasColumnInTable.cpp:137 ("the same grant needed by DESCRIBE and SHOW CREATE TABLE"); it would also not close the surface, because DESCRIBE discloses more of it per query. Following the chain belongs at the shared predicate, with cycle handling (a self-referential Alias is loadable and bounded only by TOO_DEEP_RECURSION) and a per-site decision on what each caller dereferences. #117572 already records it as a residual of this family, in those words.

The suggested test follows from that: an ACCESS_DENIED assertion on the two-hop carrier would pin behaviour this PR does not claim, and would fail.

On the category, I do not self-apply Critical Bug Fix. It maps to pr-critical-bugfix, which ci/jobs/scripts/workflow_hooks/pr_labels_and_category.py:103 puts in AUTO_BACKPORT, and :269 then adds pr-must-backport, so selecting it opens backports to every active release branch on my own authority. That is a maintainer's decision, not mine. The same suggestion came up on #108464, the predecessor of this change on this file; I kept Bug Fix and a maintainer set the category afterwards. One word from a reviewer and I will switch it.

@PedroTadim PedroTadim self-assigned this Sep 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c6f26969e with master b20da91bd (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 8 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim

Copy link
Copy Markdown
Member

This seems to be a duct tape solution; the real one is to always check for the target table whenever a privilege is checked for an Alias table

@groeneai

groeneai commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

Agreed on the rule, and I am not defending the per-site guard as the destination.

That rule already has a single home: StorageAlias::isTargetTableGranted, which after this PR has 15 call sites in src/ across 13 files. Making that one predicate follow the whole alias chain instead of stopping at the immediate target applies the rule at every site at once, and that is the change I would write. It cannot live in the access layer itself, because ContextAccess::isGranted never resolves a name to a storage and so cannot tell an Alias row from a MergeTree one.

Three things it has to settle, which is why I sent the single site first:

  1. A cycle is constructible today through RENAME DATABASE, so the traversal needs an explicit visited set rather than plain recursion. Reject a RENAME DATABASE that would make an Alias table cyclic #117253 rejects that rename and would remove the concern.
  2. Those 15 sites use three privileges (SHOW_COLUMNS, SHOW_TABLES, SELECT), and 8 of them are system.* row filters that must not throw, so "check the target" is not one uniform rule. Propagation also has to stay per privilege: DROP and RENAME on an alias must keep touching only the alias.
  3. as<StorageAlias>() is a typeid_cast, so under lazy_load_tables the storage is a StorageTableProxy and all 15 sites are blind. Do not defer an Alias table in a database with lazy_load_tables #117490 fixes that at the source.

Your call, and I will do either: expand this PR into the chain-following predicate now, or keep it as the one-hop increment and send the structural change once #117253 and #117490 land.

Merged via the queue into ClickHouse:master with commit 15ab60d Sep 2, 2026
174 of 175 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 Sep 2, 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 groeneai-origin-follow-up PR origin: groeneai's own out-of-scope finding during its PR work pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants