Repository navigation
Conversation
The new body of `ALTER TABLE ... MODIFY QUERY` is now checked against the user for every table it reads, including inside subqueries, and also on an `ON CLUSTER` initiator and before a `Replicated` database enqueues the entry, where the hosts and replicas apply it without the user. The subquery check no longer refuses a valid column grant, which affected `CREATE` as well.
…equest This pull request keeps only the SQL security gate. The body checks that match `CREATE` move to #123063.
|
Workflow [PR], commit [46a07dc] Summary: ✅
AI ReviewSummaryThis PR tightens Findings❌ Blockers
Tests
Final VerdictRequest changes. LLVM Coverage ReportMeasured on commit 46a07dc.
Changed lines: Changed C/C++ lines covered: 63/66 (95.45%) · Uncovered code |
A `SQL SECURITY DEFINER` parameterized view nested in a subquery skipped the `SELECT` check on the view itself, on the `CREATE` path as well.
… DBReplicated job
| /// The hosts run the entry without the user, unless `distributed_ddl_use_initial_user_and_roles` is on, | ||
| /// so the new body is authorized here. | ||
| if (modify_query) | ||
| checkAccessForModifyQuery(table, table_id); |
There was a problem hiding this comment.
This initiator-side check now makes ALTER ... ON CLUSTER depend on having the target view locally. Context::tryResolveStorageID keeps table_id non-empty even when the specified database exists only on cluster peers, so table can still be null on a valid dispatcher node. In that case checkAccessForModifyQuery falls into the new UNKNOWN_TABLE throw at 673-675 instead of forwarding the DDL, which regresses the supported non-local ON CLUSTER path. Please preserve that path here instead of unconditionally requiring a local StoragePtr.
There was a problem hiding this comment.
This is intended. With the default distributed_ddl_use_initial_user_and_roles = 0 the hosts apply the entry without the user, so the initiator is the only place where the body can be checked, and that needs the view's metadata. A node without the view usually lacks the source tables as well, which the check needs anyway. Each worker reads that setting from its own server settings, so its value on the initiator cannot relax the check either. The statement works when it runs on a host that has the view.
| { | ||
| /// Every replica, this one included, applies the entry without the user. | ||
| if (modify_query) | ||
| checkAccessForModifyQuery(table, table_id); |
There was a problem hiding this comment.
MODIFY QUERY still skips the same authorization on the shared-catalog path. The new DatabaseReplicated branch calls checkAccessForModifyQuery, but SharedDatabaseCatalog::shouldReplicateQuery at 525-527 forwards immediately, while AlterCommand::apply on shared-catalog secondaries explicitly returns before getSampleBlock / access analysis (AlterCommands.cpp:1266-1270). So in CLICKHOUSE_CLOUD the initiator can still persist a body it cannot SELECT, which is the same privilege hole this PR is fixing for ON CLUSTER and Replicated. The same pre-check needs to run before tryExecuteDDLQuery.
There was a problem hiding this comment.
On the shared-catalog path the initiator executes the statement again as the user, and that execution counts as the initial query. So the early return in AlterCommand::apply does not apply there, and the body is analysed with checkSubqueryTableAccess against the user. Only the secondaries skip the analysis, and they apply what the initiator has already authorized.
The subquery check also walked the expression of an `ALIAS` column, so a grant on the alias alone was refused one level deeper than at the top level.
| result[column_source.get()].insert(column_node->getColumnName()); | ||
|
|
||
| /// A grant on an `ALIAS` column is enough to read it, as at the top level, so its expression is not walked. | ||
| if (column_node->hasExpression() && column_source && column_source->as<TableNode>()) |
There was a problem hiding this comment.
collectReferencedColumnsPerTableExpression only stops descending through alias columns when the source is a TableNode. Parameterized views in subqueries are resolved as TableFunctionNodes, and the rest of the analyzer treats their output aliases the same way as table aliases (see CollectTableExpressionData::isAliasColumn). With the current check, SELECT alias_col FROM (SELECT alias_col FROM pv(x = 1)) will still walk the alias expression and can reintroduce ACCESS_DENIED on the underlying columns even when the user has a valid grant on the alias itself. Please short-circuit alias expressions for parameterized-view TableFunctionNodes as well, or reuse the existing alias-column predicate here.
There was a problem hiding this comment.
A parameterized view never gives an alias expression to walk. Its columns are the output columns of the view, so a view body that selects a table's ALIAS column hands it out as a plain column. EXPLAIN QUERY TREE SELECT s FROM (SELECT secret_alias AS s FROM pv(x = 1)) shows COLUMN secret_alias with source TABLE_FUNCTION pv and no EXPRESSION child. A view that declares an ALIAS column in its column list does not expose it: SELECT secret_alias FROM pv(x = 1) fails with UNKNOWN_IDENTIFIER. So the walk cannot reach the underlying columns under a parameterized view, and the stop condition only matters for tables.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+65.16 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units17 translation units recompiled, 119 s compile time in total, 17 of them have a recent master baseline. |
ALTER TABLE <view> MODIFY QUERYchecked its new body less strictly thanCREATE ... VIEWdoes:UNIONleg were not checked against the user. Since Check table access when the analyzer resolves a table identifier #117979 a table with no visible column is refused anyway, but with a grant on some columns the body could still read the others;CREATEpath as well;ON CLUSTERandReplicateddatabases skipped the body check completely, because the hosts and replicas apply the entry without the user. The initiator now applies everyMODIFY QUERYof the statement to a copy of the view's metadata, as the user and in the view's database, before it dispatches or enqueues the entry.Related: #121629
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ALTER TABLE ... MODIFY QUERYnow checks the new body the same wayCREATE ... VIEWdoes:SELECTon every table it reads, including inside subqueries, also withON CLUSTERand in aReplicateddatabase. A column grant is no longer refused when the column is read inside a subquery.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123063]