Repository navigation
Check table access when the analyzer resolves a table identifier - #117979
Conversation
`EXPLAIN QUERY TREE` and `EXPLAIN SYNTAX` resolve the query and dump table metadata (column names and types) without building a query plan, so the `SELECT` access check the planner performs when the table is read was never reached, and a user with no privileges could see the structure of any table. Check access at the moment the analyzer obtains the table metadata, in `IdentifierResolver::tryResolveTableIdentifierFromDatabaseCatalog`: the user must be allowed to see at least one column of the table. Any grant on a column implies `SHOW_COLUMNS` on it, so the check is weaker than every check the planner performs later for the columns the query actually reads, and a query that can be executed can always be analyzed. Table expressions constructed programmatically (e.g. by `MutationsInterpreter`) do not pass through the identifier resolution and keep their own access semantics. A simpler reimplementation of #110180, as requested in #110180 (comment) Closes #78938 Co-Authored-By: Claude Fable 5 <[email protected]>
Probe the visibility of the table's columns on the raw access rights, so the probing does not pollute `used_privileges` of the query log, and `SELECT` is checked in addition to `SHOW_COLUMNS` because the implicit `SELECT` on the `system` and `information_schema` databases is granted after implications are derived. When not a single column is visible, report the denial through the standard `Context::checkAccess`: the user gets the same sanitized message a plain `SELECT` produces (`grant SELECT ON db.table`, no column names), and `missing_privileges` of the query log is populated - without naming columns the user may not see (they leaked there before). Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [09a5315] Summary: ✅
AI ReviewSummaryThis PR moves the table-metadata visibility check into analyzer-time identifier resolution so Findings❌ Blockers
Tests
Final VerdictStatus: Minimum required actions:
LLVM Coverage ReportMeasured on commit 09a5315.
Changed lines: Changed C/C++ lines covered: 15/16 (93.75%) · Uncovered code |
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is good.
| const auto access_rights = context->getAccess()->getAccessRightsWithImplicit(); | ||
| for (const auto & column : table_node.getStorageSnapshot()->metadata->getColumns()) | ||
| { | ||
| if (access_rights->isGranted(AccessType::SELECT, storage_id.database_name, storage_id.table_name, column.name) |
There was a problem hiding this comment.
TableNode metadata is not always about storage_id itself. For ENGINE = Alias, getStorageSnapshot()->metadata comes from the target table (StorageAlias::getInMemoryMetadataPtr), and the existing metadata surfaces already treat that as protected by the target grant too (InterpreterDescribeQuery.cpp:255-257, hasColumnInTable.cpp:109-117, 05055_has_column_in_table_alias_access_check.sh).
With this loop, a user who has SHOW COLUMNS or SELECT on the alias name but none on the target now passes the new guard and EXPLAIN QUERY TREE can dump the target schema again. The metadata-visibility check needs to mirror the alias-target check as well, not only the outer table name.
| { | ||
| if (auto result = tryResolveTableIdentifier(table_identifier, context)) | ||
| { | ||
| checkAccessToTableMetadata(*result, context); |
There was a problem hiding this comment.
This only closes the IdentifierNode carrier. joinGet can still name the StorageJoin through the documented const-string form (or a WITH alias that resolves to one), and that path never calls tryResolveTableIdentifierFromDatabaseCatalog: FunctionJoinGet::buildImpl resolves the table in getJoin and calls joinGetCheckAndGetReturnType before prepare finally does checkAccess (FunctionJoinGet.cpp:149-205).
So joinGet('db.join_table', 'secret', bad_key) can still leak TYPE_MISMATCH or NO_SUCH_COLUMN_IN_TABLE to a user with no SELECT. The pre-typecheck guard needs to live in the joinGet const-string path as well, not only in identifier resolution.
|
One sibling gap the resolution-site check structurally misses, and one description inaccuracy. Parameterized views bypass the new check entirely, because they never go through The description inaccuracy: "the materialized-view push path does not go through identifier resolution" is not quite right. The MV push analyzes the select query with the passes, and |
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, 11 s compile time in total, 7 of them have a recent master baseline. |
…es a table identifier
Backport #117979 to 26.8: Check table access when the analyzer resolves a table identifier
Cherry pick #117979 to 26.6: Check table access when the analyzer resolves a table identifier
…es a table identifier
Cherry pick #117979 to 26.7: Check table access when the analyzer resolves a table identifier
…es a table identifier
Resolve the conflict in `04510_select_access_rights_rewrite.lib`: the file does not exist on 26.3 (its test was never backported), so the hunk from #117979 touching it is dropped. All other hunks of #117979 apply cleanly; the APIs used (`getAccessRightsWithImplicit`, `TableNode::getStorageSnapshot`, `Context::checkAccess` with `StorageID`) exist on 26.3. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Cherry pick #117979 to 26.3: Check table access when the analyzer resolves a table identifier
…es a table identifier
…access check On master this integration test was already rewritten as the functional `04510_select_access_rights_rewrite.lib`, which #117979 updated, so the backport had nothing to change there. On 26.7 the integration test still exists and expected the column-level message `SELECT for at least one column` for `SELECT count() FROM table1` without any grant. With the analyzer checking table access while resolving the identifier, the error is now `SELECT ON default.table1`, exactly as the master test expects. Pin `enable_analyzer=1` for these assertions so the result does not depend on the analyzer mode of the CI job. CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=118179&sha=b7bfce27db5ca4f47c17b1768587ef9de2ec3715&name_0=BackportPR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%202%2F6%29 PR: #118179
…access check On master this integration test was already rewritten as the functional `04510_select_access_rights_rewrite.lib`, which #117979 updated, so the backport had nothing to change there. On 26.6 the integration test still exists and expected the column-level message `SELECT for at least one column` for `SELECT count() FROM table1` without any grant. With the analyzer checking table access while resolving the identifier, the error is now `SELECT ON default.table1`, exactly as the master test expects. Pin `enable_analyzer=1` for these assertions so the result does not depend on the analyzer mode of the CI job. Same fix as c7d1f4b in the 26.7 backport (#118179). CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=118169&sha=2bc6581ff7d9eb1d276ec947c28162e4108455c4&name_0=BackportPR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%203%2F6%29 PR: #118169
Backport #117979 to 26.7: Check table access when the analyzer resolves a table identifier
Backport #117979 to 26.6: Check table access when the analyzer resolves a table identifier
…access check On master this integration test was already rewritten as the functional `04510_select_access_rights_rewrite.lib`, which #117979 updated, so the backport had nothing to change there. On 26.7 the integration test still exists and expected the column-level message `SELECT for at least one column` for `SELECT count() FROM table1` without any grant. With the analyzer checking table access while resolving the identifier, the error is now `SELECT ON default.table1`, exactly as the master test expects. Pin `enable_analyzer=1` for these assertions so the result does not depend on the analyzer mode of the CI job. CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=118179&sha=b7bfce27db5ca4f47c17b1768587ef9de2ec3715&name_0=BackportPR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%202%2F6%29 PR: #118179 (cherry picked from commit c7d1f4b)
Backport #117979 to 26.3: Check table access when the analyzer resolves a table identifier
…tries` Since ClickHouse#117979 the analyzer denies a query unless at least one top-level column of the table is visible to the user, so the cases granting only a subcolumn name were denied before the column check ran. Grant `id` in every case so the test exercises the subcolumn semantics. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=103402&sha=85c65e7a00a91cce1f2603c5ece735522f15342b&name_0=PR&name_1=Fast%20test
`checkAccessToTableMetadata` (from ClickHouse#117979) denies a query unless the user may see at least one top-level column of the table. A user whose only grant is on the exact name of a subcolumn, such as ``GRANT SELECT(`json.a`)``, was therefore denied before the `SELECT` check that honors that grant ran. Also accept the table when a column `c` has an explicit grant of `SELECT` or `SHOW_COLUMNS` on some name starting with `c.`, found by the new `AccessRights::hasExplicitGrantOnColumnsWithPrefix`. A grant counts only where it gives flags that the name does not inherit, consistent with `ColumnsDescription::getColumnNamesForSelectAccessCheck`, so a table grant with every column revoked still hides the table. Remove the `SELECT(id)` workaround from `05257_grant_select_subcolumn_explicit_entries` and add a case without grants. Addresses review: ClickHouse#103402 (comment)
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]>
Closes: #78938
Related: #110180
Related: #91067
EXPLAIN QUERY TREEandEXPLAIN SYNTAX run_query_tree_passes = 1resolve the query and dump table metadata (column names and types), but do not build a query plan, so theSELECTaccess check the planner performs when a table is read was never reached, and a user with no privileges could see the structure of any table.A simpler reimplementation of #110180, which duplicated the planner's access checks in
InterpreterExplainQueryand grew unmanageable, as requested in #110180 (comment). Of the two things requested there, hiding column names from the error messages was already done by #91067, and this change covers the second one: the access check happens at the moment the query actually obtains the information that requires access — when the analyzer resolves a table identifier through the database catalog (IdentifierResolver::tryResolveTableIdentifierFromDatabaseCatalog) and binds the table's metadata into the query tree. The user must be allowed to see at least one column of the table; otherwise the resolution fails through the standardContext::checkAccess, which produces the same sanitizedit's necessary to have the grant SELECT ON db.tablemessage a plainSELECTproduces and populatesmissing_privilegesof the query log — now without naming columns the user may not see (the raw column list leaked there before).Any grant on a column implies
SHOW_COLUMNSon it, so the new check is weaker than every check the planner performs later for the columns the query actually reads: a query that can be executed can always be analyzed, and no executable query changes behavior. Table expressions constructed programmatically (MutationsInterpreter,StorageMerge, the materialized-view push path, query-plan deserialization) do not go through identifier resolution and keep their access semantics. Views work naturally: the view object itself is resolved through the catalog and checked, and when a view is inlined its base tables resolve under the view'sSQL SECURITYcontext.Verified by comparing an access-focused behavior matrix (mutations by
ALTER-only users, materialized view chains byINSERT-only users,Mergetables,joinGet, parameterized views,SQL SECURITY DEFINER/INVOKERviews, partial column grants, trivialcount(), row policies,systemtables) between a master build and this branch: the only differences are the intended ones. Besides theEXPLAINleak, the check also closes two smaller metadata oracles for users with no access to a table: the error kind no longer reveals whether a column exists (UNKNOWN_IDENTIFIERvsACCESS_DENIED), andjoinGetwith a mismatched key type no longer reveals the key type (TYPE_MISMATCHbefore the access check).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
EXPLAIN QUERY TREEandEXPLAIN SYNTAXexposing column names and types of tables the user has no privileges for: the analyzer now checks that the user may see at least one column of the table at the moment a table identifier is resolved. The check also makes the error kind independent of whether the queried column exists and stops recording column names the user may not see intomissing_privilegesof the query log. Closes #78938.Documentation entry for user-facing changes
Workflow [PR]
Sync PR [sync-upstream/pr/117979]
Version info
26.9.1.687(included in26.9and later)26.8.3.33,26.7.7.29,26.6.5.53,26.3.32.9