Skip to content

Check access to a parameterized view when a query resolves it - #129106

Merged
PedroTadim merged 8 commits into
ClickHouse:masterfrom
groeneai:check-parameterized-view-access-in-analyzer
Nov 6, 2026
Merged

PedroTadim merged 8 commits into
ClickHouse:masterfrom
groeneai:check-parameterized-view-access-in-analyzer

Conversation

@groeneai

@groeneai groeneai commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #78938

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 EXPLAIN QUERY TREE and EXPLAIN SYNTAX run_query_tree_passes = 1 showing the columns of a parameterized view to a user without grants on it. Such a user now gets ACCESS_DENIED for any call of the view, instead of errors that name its columns, parameters or the tables it reads, or quote its definition. A user whose only grant on a parameterized view is on a subcolumn (SELECT(`t.a`)) can no longer read it, as for tables since #117979. With the old analyzer (enable_analyzer = 0, before 26.9), an unqualified call of a parameterized view named like a table function, such as numbers(n = 3), now calls the table function, as the analyzer does; use db.numbers(n = 3) for the view.

Description

On 26.8.34.13 and 26.10.2.103, EXPLAIN QUERY TREE SELECT * FROM pv(n = 3) prints a SQL SECURITY DEFINER parameterized view's columns to a user without grants; SELECT, DESCRIBE, SHOW CREATE refuse. #117979 checks tables and ordinary views, not parameterized view calls (table functions). Errors leak too: SELECT no_such_column FROM pv(...) gives UNKNOWN_IDENTIFIER, pv() names the missing parameter, an INVOKER view names its table.

Two checks:

Both are weaker than the column SELECT execution needs, so no working query is refused, except with a subcolumn-only grant (#103402 counts such grants). A SHOW TABLES-only or column-grant holder passes the first check, so a malformed call's error can still name parameters or, via an Identifier parameter, quote the definition. Since #129074, EXPLAIN SYNTAX run_query_tree_passes = 1 and EXPLAIN AST optimize = 1 also reach the first check: two 05493 arms (base-table-only user) now expect ACCESS_DENIED.

The test covers three EXPLAIN modes, an IN subquery, errors, INSERT ... SELECT into Distributed, a shadowed table function, an Alias, and _table-only, base-table-only and SHOW TABLES-only users. Sixteen checks fail on master and pass with the fix (100 randomized runs).


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

Version info

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

groeneai and others added 2 commits November 3, 2026 20:06
A parameterized view call is resolved as a table function, so the
metadata check that ClickHouse#117979 added to table identifier resolution never
runs for it. A user with no grant on a SQL SECURITY DEFINER
parameterized view therefore got its column names and types from
EXPLAIN QUERY TREE and EXPLAIN SYNTAX run_query_tree_passes = 1, and
every user got errors that named the view's parameters, its columns
(UNKNOWN_IDENTIFIER vs ACCESS_DENIED) or, for an INVOKER view, the
tables behind it, because the parameters are substituted and the body
is analyzed before anything checks the view.

Check the view where each piece of information is obtained:
- Context::buildParameterizedViewStorage: without implicit SHOW TABLES
  on the view (which any grant on the view or on one of its columns
  implies), report the standard SELECT denial before substituting the
  parameters or analyzing the body.
- QueryAnalyzer::resolveTableFunction: once the columns are known, run
  the ClickHouse#117979 check (at least one column must be visible). The helper
  becomes a public static member of IdentifierResolver; the
  temporary-table exemption moves to its identifier-resolution caller.

Executing a call needs SELECT on at least one of its columns, which
passes both checks, so no executable query changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The parameterized-view branch of `Context::executeTableFunction` substitutes
the parameters and analyzes the body like `buildParameterizedViewStorage`, and
`INSERT ... SELECT` into a `Distributed` or replicated table reaches it through
`JoinedTables::getLeftTableStorage` before the analyzer when
`parallel_distributed_insert_select` is on (the default). Without the check, a
user with only `INSERT` on the destination got errors naming the view's
parameters or the tables an `INVOKER` view reads.

Move the first check into a file-local helper and call it from both places.
The test gets two `INSERT ... SELECT` arms that fail when only the new call is
removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 3, 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) plus my own review, 2 rounds: round 1 on the first head (3 findings), round 2 a delta recheck of the fix round (0 findings).

# Sev Finding Verdict Evidence / action
1 ❌ The parameterized-view branch of Context::executeTableFunction (reached by INSERT ... SELECT into a Distributed or replicated table before the analyzer, parallel_distributed_insert_select default 2) had no check, so errors still named parameters or the tables an INVOKER view reads AGREE, fixed @ 9a78e72 the same first check is called there; two INSERT ... SELECT arms fail when only that call is removed
2 ❌ EXPLAIN SYNTAX inlines the definition of an INVOKER view before any check DISAGREE that expansion is ExpandParameterizedViewsMatcher::tryExpandTableExpression, which #129074 guards; this PR does not touch it
3 ⚠️ A user with only SHOW TABLES (or a column grant) on the view passes the first check, so an error of a malformed call can still name the parameters or, through an Identifier parameter, quote the definition DISAGREE outside this PR's contract (a user without any grant on the view); a parameterized view stores no columns, so no predicate exists before the body is analyzed; the column list is refused (SHOW TABLES arm); stated in the description
4 💡 No arm for an invalid Identifier parameter noted, not blocking the first check runs before parameter substitution, which the missing-parameter and INVOKER arms already prove (each fails when that check is removed)

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261103-213200

@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. As a user with no grants, EXPLAIN QUERY TREE SELECT * FROM db.pv_definer(n = 3) prints the view's columns on every run on master and on 26.8.34.13 / 26.10.2.103; with the fix it returns ACCESS_DENIED.
b Root cause explained? Yes. The table check from #117979 runs only when a table identifier is resolved. A parameterized view call goes through resolveTableFunction and Context::buildParameterizedViewStorage (or, before the analyzer, the parameterized-view branch of Context::executeTableFunction), which substitute the parameters and analyze the body (raising errors that name parameters or base tables) and bind the view's columns with no check on the view.
c Fix matches root cause? Yes. One check before the definition is touched in both functions that substitute the parameters (no SHOW TABLES on the view, so nothing is visible), and the #117979 column check once the columns exist.
d Test intent preserved / new tests added? New test 05497_parameterized_view_access_check (11 denial arms, 4 controls). No existing test changed.
e Both directions demonstrated? Yes. On master arms 1-11 fail and the controls pass; with the fix all pass. Removing any one check reddens exactly the arms it owns (only the SHOW TABLES arm; only the missing-parameter and INVOKER base-table arms; only the two INSERT ... SELECT arms).
f Fix is general across code paths? Yes. The first check is in both places that substitute a parameterized view's parameters: buildParameterizedViewStorage (its one caller, the analyzer's branch, serves FROM, JOIN, IN, scalar subqueries and view bodies) and the parameterized-view branch of Context::executeTableFunction, which INSERT ... SELECT into a Distributed or replicated table reaches before the analyzer (parallel_distributed_insert_select, default 2). DESCRIBE pv(...) already checks SHOW COLUMNS.
g Fix generalizes across inputs (params/datatypes/wrappers)? Access check, not type-dependent. Covered: DEFINER and INVOKER views, qualified and unqualified names, a call without arguments, and grants none / table behind the view / SHOW TABLES / one column / SHOW COLUMNS / SELECT / through a definer view.
h Backward compatible? (maintainer-approved exception only) Yes. Executing a call already needs SELECT on one of its columns, which passes both checks, so every executable query is unchanged (controls, and 0 new failures in 281-test and 251-test sweeps against master). No setting.
i Invariants and contracts preserved? Yes. The helper body is unchanged and identifier resolution keeps the temporary-table exemption at its call site. The checks throw before any state is built, and probing the rights does not touch used_privileges.

Session id: cron:clickhouse-impl-self-slot-57:20261103-205300

@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

cc @novikd @alexey-milovidov, could you review this and add the can be tested label so CI runs? A parameterized view call is resolved as a table function and skipped the #117979 column check, so EXPLAIN QUERY TREE printed its columns to a user with no grant on it; this adds that check in resolveTableFunction, plus a SELECT check (skipped when the user has SHOW TABLES on the view) before parameter substitution in both Context functions that expand the view.

@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1428451]

Summary: ✅


AI Review

Summary

The PR checks access before parameterized-view expansion and before exposing resolved columns, while allowing virtual-column grants and aligning table-function precedence with the analyzer. Review of the current diff, callers, regression coverage, and all prior discussion found no new actionable issues. The focused regression passed 35 CI runs at 5c2a3797ecf0ced253d4a30ad6578d255ef49485; the reviewed logic and test are unchanged at head 14284515b21720c1a5f1b3f401b0b9049fd1c431. Current-head CI is still in progress; no local build or tests were run.

Final Verdict

Status: ✅ Approve. No additional changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. can be tested Allows running workflows for external contributors labels Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 14284515b with master 360ab5255 (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

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

Job report

groeneai and others added 5 commits November 4, 2026 20:01
… and update 05493

With `ORDER BY {sort:Identifier}` and `sort = 'missing'`, the analysis error under
`EXPLAIN SYNTAX run_query_tree_passes = 1` and `EXPLAIN AST optimize = 1` quoted the view
definition to a user with SELECT on the base table only. Since ClickHouse#129074 these modes no longer
inline the view for such a user and reach the check in `buildParameterizedViewStorage`, so the
two base-table-only analyzer arms of 05493 now get ACCESS_DENIED.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e same name in executeTableFunction

The INSERT ... SELECT preflight into a Distributed table reached the parameterized view's access check
for a call like `numbers(3)` that the analyzer resolves as the table function, when a view of that name
exists in the current database. Check the factory first, as `QueryAnalyzer::resolveTableFunction` does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A grant on only a virtual column such as `_table` lets the planner read it, but the metadata
check counted only the declared columns, so such a query was refused before it was planned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…yzer's metadata check

An Alias reports its target's virtual columns, so a grant on one of them on the alias alone
made the target's columns visible to EXPLAIN QUERY TREE.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 5, 2026

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

Review of the update (merge of master for 05493, the Identifier-parameter arms and three follow-up fixes) by an independent model:

Finding Verdict
❌ Context::executeTableFunction looked a call's name up in the catalog before the table-function factory, so the new view check could reject INSERT ... SELECT FROM numbers(3) into a Distributed table when an unrelated parameterized view named numbers exists AGREE, fixed: a registered table function now takes precedence there, as in the analyzer; test arm added
❌ The column check counted only declared columns, so a user granted only a virtual column (SELECT(_table)) could no longer read it from a parameterized view; #117979's check refused the same grant on tables AGREE, fixed: virtual columns count too; arms for a parameterized view and a table
❌ Counting virtual columns let a grant on only _table of an Alias reveal its target's columns in EXPLAIN QUERY TREE, while reading them is denied AGREE, fixed: for an Alias a virtual column counts only if it is also granted through the target chain, as Merge already requires; test arms added
⚠️ The test's denial check stops at the first (, so a column-level grant message could hide a table name DISAGREE: with the view check removed, every arm gets the table-level SELECT ON <table> message, which the check inspects and flags (measured on master and on mutants)
⚠️ DESCRIBE numbers(3) still describes a same-named parameterized view the user can see instead of the table function DISAGREE: unchanged by this PR (same result before and after); the order is the deliberate choice of #68978
❌ A user whose only grant on a parameterized view is on a subcolumn name (SELECT(`t.a`)) can no longer read it DISAGREE for this PR: #117979 already refuses that grant on tables in 26.9 and 26.10, and #103402 adds subcolumn grants to the same check, which now also covers parameterized views; stated in the description

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (update: invalid `Identifier` parameter, `05493`, a table function shadowed by a view, virtual-column grants, an `Alias`) (click to expand)
# Question Answer
a Deterministic repro? Yes. As a user with SELECT on the base table only, EXPLAIN SYNTAX run_query_tree_passes = 1 and EXPLAIN AST optimize = 1 of an INVOKER view with ORDER BY {secret_sort:Identifier} and secret_sort = 'missing' fail on master with UNKNOWN_IDENTIFIER quoting the whole definition on every run; with this PR both return ACCESS_DENIED. With a parameterized view named numbers, INSERT INTO <Distributed> SELECT ... FROM numbers(3) fails on master with Substitution n is not set; with this PR it inserts.
b Root cause explained? Yes. Since #129074 these modes resolve the call of a user without SHOW COLUMNS through Context::buildParameterizedViewStorage, whose analysis error carries the body. Context::executeTableFunction looked the name up in the catalog before the table-function factory, the opposite of the analyzer. #117979's metadata check counted only declared columns, while a grant on only a virtual column can read it; an Alias reports its target's virtual columns.
c Fix matches root cause? Yes. The existing buildParameterizedViewStorage check covers the Identifier case once #129074 is on the branch. executeTableFunction takes the view branch only when no table function of that name is registered. checkAccessToTableMetadata also counts virtual columns; for an Alias only if the grant holds through the target chain (StorageAlias::isTargetTableGranted), as StorageMerge requires.
d Test intent preserved / new tests added? 05497: two invalid-Identifier arms, one INSERT ... SELECT FROM numbers(3) arm, three _table-only lines (view SELECT, view EXPLAIN QUERY TREE, table SELECT), two Alias arms. 05493: two base-table-only analyzer arms change from definition 0, call 1 to error: Code: 497.
e Both directions demonstrated? Yes. On master after #129074, 16 checks of 05497 and those two 05493 lines fail; with this PR all pass. Each follow-up change has a mutant (the previous heads, or one removed check or condition) that reddens exactly its own arms.
f Fix is general across code paths? Yes. The view check is in both places that substitute the parameters (buildParameterizedViewStorage, the view branch of executeTableFunction); DESCRIBE keeps its own lookup, unchanged. The metadata check is shared with tables and ordinary views, so they count virtual columns too (table line, plus a hand probe on MergeTree). Of the storages overriding getInMemoryMetadataPtr, only Alias reports another table's metadata (a TO materialized view adds its target's virtual columns and reads them as its definer).
g Fix generalizes across inputs (params/datatypes/wrappers)? Not type-dependent. Covered: DEFINER and INVOKER views, an Identifier parameter, a call without arguments, positional and named calls of a shadowed table function, qualified and unqualified names, and grants none / base table / SHOW TABLES / one column / one virtual column (view, table, Alias with and without its target) / SHOW COLUMNS / SELECT / through a definer view.
h Backward compatible? (maintainer-approved exception only) Yes. No setting; 0 new failures in a 313-test sweep of grant, virtual-column, Alias and parameterized-view tests against master.
i Invariants and contracts preserved? Yes. The checks throw before any state is built; isTableFunctionName only reads the registry; the virtual-column loop only adds an early return. For an Alias the chain walk uses ContextAccess::isGranted, so a target grant it finds is logged in used_privileges.

@PedroTadim

Copy link
Copy Markdown
Member

🕵️ @groeneai The access checks look right to me. Both places that substitute the parameters are covered, and the Alias and virtual-column handling hold up. Two user-visible changes are missing from the changelog entry, though:

  1. With enable_analyzer = 0, Context::executeTableFunction is the only path, so the new precedence in it changes which object a call resolves to. A parameterized view in the current database named like a table function (CREATE VIEW numbers AS SELECT {n:UInt64} AS a) used to win, and SELECT * FROM numbers(n = 3) returned the view. Now that call goes to the numbers table function and fails. The analyzer already resolves it that way, so aligning is fine, but users still on the old analyzer lose access to such a view without any notice. A qualified db.numbers(n = 3) still reaches the view, so that is the workaround to mention.
  2. A user whose only grant on a parameterized view is on a subcolumn (SELECT(`t.a`)) can no longer read it. The description says this, but the changelog entry says the only effect is denying users without grants. The tables precedent (Check table access when the analyzer resolves a table identifier #117979) and the dependency on Fix for #101423 GRANT SELECT(column) does not give access to its subcolumns #103402, which is still open, are fine with me, but users should see this in the changelog.

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks, I added both to the changelog entry. On 1: master replaces enable_analyzer = 0 with 1, so there SELECT * FROM numbers(n = 3) already calls the table function with or without this PR. The change shows only in a backport to 26.8 or older (on 26.8.34.13 with enable_analyzer = 0 the call returns the view), so the entry scopes it to the old analyzer before 26.9.

@PedroTadim
PedroTadim enabled auto-merge November 5, 2026 21:10
@PedroTadim
PedroTadim added this pull request to the merge queue Nov 6, 2026
Merged via the queue into ClickHouse:master with commit 17e0ee2 Nov 6, 2026
185 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 6, 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. 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