Repository navigation
Check SHOW COLUMNS before EXPLAIN SYNTAX inlines a parameterized view definition - #129074
Conversation
… definition `ExpandParameterizedViewsMatcher::tryExpandTableExpression` replaced a parameterized view call with the view's stored query before any analysis and without any access check. A user refused `SHOW CREATE` on the view (no `SHOW COLUMNS`) therefore read the whole definition, literals included, from `EXPLAIN SYNTAX` or `EXPLAIN AST optimize = 1`. With the default `run_query_tree_passes = 0` nothing checks access afterwards; with `run_query_tree_passes = 1` only the tables behind the view are checked. Require `SHOW COLUMNS` on the view at the point the definition is read, the privilege `SHOW CREATE`, `system.tables.create_table_query` and `DESCRIBE pv(...)` already require, and otherwise leave the call as written, like the existing `SQL SECURITY DEFINER` branch. Not throwing keeps an inaccessible view printing like an unknown name in the default mode, as `DESCRIBE` does. The check is keyed on the resolved storage rather than the parsed name. Reported by PedroTadim in the review of ClickHouse#128992. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings) plus my own review (2 findings);
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261103-182000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261103-180900 |
|
cc @pufit @vdimir, could you review this and add the |
|
Workflow [PR], commit [5351082] Summary: ❌
AI ReviewSummaryThis PR adds a Findings❌ Blockers
Tests
Final Verdict
|
| if (!query_context->getAccess()->isGranted(AccessType::SHOW_COLUMNS, view_id.getDatabaseName(), view_id.getTableName())) | ||
| return; |
There was a problem hiding this comment.
❌ [P1] Protect the definition when the analyzer throws
Returning here leaves the call for subsequent analysis. With run_query_tree_passes = 1 or EXPLAIN AST optimize = 1, QueryAnalyzer::resolveTableFunction still reaches Context::buildParameterizedViewStorage, which clones the hidden definition and analyzes it through getSampleBlock without this privilege check. An analysis exception can therefore disclose the entire query, including its literals.
Reproduced on this head's Fast test binary: extend this test's view with ORDER BY {sort:Identifier}, keep only SELECT on base for the user, and execute:
EXPLAIN SYNTAX run_query_tree_passes = 1
SELECT * FROM pv(n = 3, sort = 'missing');Although SHOW CREATE TABLE pv is denied, the exception includes Unknown expression identifier followed by In scope SELECT x, s FROM default.review_base WHERE (x < _CAST(3, 'UInt32')) AND (s != 'definition_marker') ORDER BY missing ASC (the probe used review_base as the base table). EXPLAIN AST optimize = 1 leaks the same definition.
Please protect the subsequent inner-analysis path as well, so an inaccessible view's definition cannot appear in exceptions, and add a negative test for both modes with an invalid Identifier parameter.
There was a problem hiding this comment.
Yes, it is in my #129106, which checks access to the view in Context::buildParameterizedViewStorage before the definition is analyzed. Measured on a build with both PRs, for this repro (SELECT on the base table only, sort = 'missing'): EXPLAIN SYNTAX run_query_tree_passes = 1 and EXPLAIN AST optimize = 1 return ACCESS_DENIED, while master quotes the definition in both.
These two modes need both PRs, since without this one the body is inlined before the analyzer reaches the check. So after this PR is merged, I'll add this case for both modes to the #129106 test, together with the 05493 reference update it then needs (the two base-table-only arms for these modes become ACCESS_DENIED).
There was a problem hiding this comment.
Done in 6bd2b0a: 05497 now runs this repro (ORDER BY {secret_sort:Identifier}, secret_sort = 'missing', SELECT on the base table only) under EXPLAIN SYNTAX run_query_tree_passes = 1 and EXPLAIN AST optimize = 1, and both return ACCESS_DENIED; the two 05493 base-table-only arms for these modes now expect ACCESS_DENIED too.
|
🕵️ @groeneai Thanks for following up on this. The fix is in the right place and I think it is complete for this path:
One request for the test. The positive case grants
The |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline. |
`SELECT` on the view implies `SHOW COLUMNS` on it, so the definition is still expanded. A column-level `SELECT(x)` does not, so the call is left as written, as for `SHOW CREATE`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Added both in Noted for #128992: whichever PR lands second gets the merge, with the check kept after the final |
CI finish ledger - 4420898Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261104-003004 |
| return; | ||
|
|
||
| /// The inlined body is the view definition, which needs `SHOW COLUMNS` on the view, as `SHOW CREATE` does. | ||
| const auto view_id = storage->getStorageID(); |
There was a problem hiding this comment.
@groeneai does other EXPLAIN statements have similar issues?
There was a problem hiding this comment.
Yes. Anything that goes through the analyzer still shares the unresolved path: EXPLAIN QUERY TREE via src/Interpreters/InterpreterExplainQuery.cpp:1187, EXPLAIN PLAN via src/Interpreters/InterpreterExplainQuery.cpp:1239, EXPLAIN PIPELINE via src/Interpreters/InterpreterExplainQuery.cpp:1321, and EXPLAIN ESTIMATE via src/Interpreters/InterpreterExplainQuery.cpp:1389. They all end up in QueryAnalyzer::resolveTableFunction (src/Analyzer/Resolve/QueryAnalyzer.cpp:5549) -> Context::buildParameterizedViewStorage (src/Interpreters/Context.cpp:3836), so this patch only closes the AST-side pre-analysis rewrite.
There was a problem hiding this comment.
Yes, on the analyzer side. Measured on master (4f752f7) for a user without SHOW COLUMNS on the view (no grants, or SELECT on the table it reads):
EXPLAIN SYNTAXandEXPLAIN AST optimize = 1print the definition of anINVOKERview. This PR.EXPLAIN QUERY TREE,EXPLAIN SYNTAX run_query_tree_passes = 1andEXPLAIN AST optimize = 1print the columns of aDEFINERview to a user with no grants at all.EXPLAIN QUERY TREE,PLAN,PIPELINE,ESTIMATEand a plainSELECTquote the definition in the error when its analysis fails. For example,ORDER BY {sort:Identifier}called withsort = 'missing'givesUNKNOWN_IDENTIFIERwithIn scope SELECT x, s FROM db.base WHERE ... ORDER BY missing ASC. For anINVOKERview, theACCESS_DENIEDof a user with no grants names the table the view reads.
All of these come from the analyzer building the view in Context::buildParameterizedViewStorage without checking access to the view. My #129106 adds that check, plus the #117979 column check in QueryAnalyzer::resolveTableFunction. On a build with both PRs, every case above returns ACCESS_DENIED or prints the call as written.
One case remains. A user with only a column grant, such as SELECT(x) ON db.pv, can run the view, and a failing analysis still quotes the definition, in a plain SELECT too. The description of #129106 states this. Ordinary views and EXPLAIN AST without optimize show nothing.
|
The CI failure is unrelated |
CI finish ledger - 5351082Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261104-190006 |
086c23e
… 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]>
Related: #128992 (comment)
Related: #129106
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
EXPLAIN SYNTAXandEXPLAIN AST optimize = 1showing the definition of a parameterized view to a user without theSHOW COLUMNSprivilege on the view, whichSHOW CREATErequires. For such a user the view call is now printed as written.Description
On 26.8.34.13 and 26.10.2.103, a user refused
SHOW CREATEon a parameterized view still gets its whole definition, literals included, fromEXPLAIN SYNTAX SELECT * FROM db.pv(p = 1). Reported by @ PedroTadim in the review of #128992.ExpandParameterizedViewsMatcher::tryExpandTableExpressionreplaces the call with the view's stored query before any analysis and checks no access. With the defaultrun_query_tree_passes = 0nothing checks access afterwards; withrun_query_tree_passes = 1orEXPLAIN AST optimize = 1only the tables behind the view are checked.The definition is now read only if the user has
SHOW COLUMNSon the view, the privilegeSHOW CREATE,system.tables.create_table_queryandDESCRIBE pv(...)require. Otherwise the call is left as written, as for aSQL SECURITY DEFINERview, so by default an inaccessible view prints like an unknown name. The check uses the resolved storage, not the parsed name. A user with only column-level grants on the view no longer sees the expansion, which matchesSHOW CREATE.The analyzer's resolution of the call comes from other code and is not changed here: it still exposes the view's column list (for example in
EXPLAIN QUERY TREEfor aSQL SECURITY DEFINERview) and, when analyzing the definition fails, the definition in the error message, also for a plainSELECT. #129106 fixes both for a user with no grant on the view.The new test covers
EXPLAIN SYNTAXwith and without the database name,run_query_tree_passes = 1andEXPLAIN AST optimize = 1for a user withoutSHOW COLUMNS, a user with onlySELECT(x)on the view, and the expansion withSHOW COLUMNSorSELECTon the view. The six checks withoutSHOW COLUMNSfail on master and pass with the fix (50/50 randomized runs).Workflow [PR]
Sync PR [sync-upstream/pr/129074]
Version info
26.11.1.1369-master(included in26.11and later)