Repository navigation
Require SET DEFINER to rewrite the body of a view that runs as another user - #121629
Conversation
|
Workflow [PR], commit [9cdc084] Summary: ✅
AI ReviewSummaryThis PR hardens Findings
Final Verdict
LLVM Coverage ReportMeasured on commit 9cdc084.
Changed lines: Changed C/C++ lines covered: 62/64 (96.88%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units17 translation units recompiled, 96 s compile time in total, 17 of them have a recent master baseline. |
| if (!table) | ||
| throw Exception(ErrorCodes::UNKNOWN_TABLE, | ||
| "Table {}.{} does not exist on this host. `ALTER TABLE ... ON CLUSTER ... MODIFY QUERY` is authorized " | ||
| "on the initiator, so it must be run from a host that has the view", |
There was a problem hiding this comment.
If host A and host B have different stored metadata for the same view, this initiator-side check is still not enough. Concrete trace: db.mv is SQL SECURITY INVOKER on A but DEFINER = alice (or NONE) on B, the caller has ALTER VIEW MODIFY QUERY but no SET DEFINER on alice, and distributed_ddl_use_initial_user_and_roles = 0; the check here passes on A, then B rewrites its DEFINER/NONE copy without ever re-checking as the initiator. ON CLUSTER is exactly how skewed metadata gets reconciled, so this still needs a fail-closed guard or worker-side execution under the initiator's user.
e75ad03 to
919ac1a
Compare
…uns as another user `ALTER TABLE ... MODIFY QUERY` needed only `ALTER VIEW MODIFY QUERY`, so it could make a `SQL SECURITY DEFINER` view read anything its definer can read. It now needs `SET DEFINER` on the definer, or `ALLOW SQL SECURITY NONE` for a `SQL SECURITY NONE` view. The initiator checks this, so it also holds for `ON CLUSTER` and for `Replicated` databases, and an `ON CLUSTER` statement is refused on a host that does not have the view. A `MODIFY SQL SECURITY` in the same statement is authorized instead of the stored security.
919ac1a to
2e49135
Compare
There was a problem hiding this comment.
Note
Copilot was unable to run its full agentic suite in this review.
Copilot review overview
Review effort: Lite
Findings: 1
Open (2)
What changed in this PR
Tightens authorization for ALTER TABLE <view> MODIFY QUERY so rewriting a view body that runs with SQL SECURITY DEFINER / SQL SECURITY NONE requires the same privileges as creating/modifying that SQL security, preventing privilege escalation.
Changes:
- Add access checks for
MODIFY QUERYbased on the view’s stored SQL security (SET DEFINER/ALLOW SQL SECURITY NONE). - Reject
ALTER ... ON CLUSTER ... MODIFY QUERYon the initiator when the view doesn’t exist locally (to ensure initiator-side authorization is correct). - Add stateless tests covering foreign definers,
SQL SECURITY NONE, row-policy cases, and ephemeral definers.
| File | Description |
|---|---|
| tests/queries/0_stateless/05296_alter_view_modify_query_ephemeral_definer.sh | New test covering ephemeral-definer grant semantics for MODIFY QUERY. |
| tests/queries/0_stateless/05296_alter_view_modify_query_ephemeral_definer.reference | Expected output for the ephemeral-definer test. |
| tests/queries/0_stateless/05237_alter_view_modify_query_access.sh | New broader access-control regression tests for MODIFY QUERY under different SQL security modes and ON CLUSTER. |
| tests/queries/0_stateless/05237_alter_view_modify_query_access.reference | Expected output for the access-control regression test. |
| src/Interpreters/InterpreterAlterQuery.h | Declares helper to add required privileges based on stored SQL security during MODIFY QUERY. |
| src/Interpreters/InterpreterAlterQuery.cpp | Implements SQL-security-based access requirements for MODIFY QUERY and initiator-side ON CLUSTER existence check. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| /// The hosts run the entry without the user, unless `distributed_ddl_use_initial_user_and_roles` is on, | ||
| /// so the view's SQL security is authorized here, which needs the view on this host. | ||
| if (modify_query && !table) | ||
| throw Exception(ErrorCodes::UNKNOWN_TABLE, | ||
| "Table {}.{} does not exist on this host. `ALTER TABLE ... ON CLUSTER ... MODIFY QUERY` is authorized " | ||
| "on the initiator, so it must be run from a host that has the view", | ||
| backQuoteIfNeed(alter.getDatabase()), backQuoteIfNeed(alter.getTable())); |
| const auto row_exists_column_kind = getRowExistsColumnKind(storage, getContext()); | ||
| /// A `MODIFY SQL SECURITY` in the same statement decides what the new body will execute as, and | ||
| /// `processSQLSecurityOption` has already authorized exactly that. The stored security then says | ||
| /// nothing about the body being written, so it must not add a requirement of its own. | ||
| const bool sql_security_is_being_replaced = std::ranges::any_of( | ||
| alter.command_list->children, | ||
| [](const auto & child) { return child->template as<ASTAlterCommand &>().sql_security != nullptr; }); | ||
|
|
||
| for (const auto & child : alter.command_list->children) | ||
| { | ||
| const auto & command = child->as<ASTAlterCommand &>(); | ||
| required_access.append_range( | ||
| getRequiredAccessForCommand(child->as<ASTAlterCommand&>(), alter.getDatabase(), alter.getTable(), row_exists_column_kind)); | ||
| getRequiredAccessForCommand(command, alter.getDatabase(), alter.getTable(), row_exists_column_kind)); | ||
|
|
||
| if (command.type == ASTAlterCommand::MODIFY_QUERY && !sql_security_is_being_replaced) | ||
| addRequiredAccessForModifyQuerySQLSecurity(required_access, storage); | ||
| } |
Cherry pick #121629 to 26.9: Require SET DEFINER to rewrite the body of a view that runs as another user
…a view that runs as another user
Cherry pick #121629 to 26.3: Require SET DEFINER to rewrite the body of a view that runs as another user
…a view that runs as another user
Cherry pick #121629 to 26.7: Require SET DEFINER to rewrite the body of a view that runs as another user
…a view that runs as another user
Cherry pick #121629 to 26.8: Require SET DEFINER to rewrite the body of a view that runs as another user
…a view that runs as another user
Backport #121629 to 26.9: Require SET DEFINER to rewrite the body of a view that runs as another user
Backport #121629 to 26.8: Require SET DEFINER to rewrite the body of a view that runs as another user
Backport #121629 to 26.7: Require SET DEFINER to rewrite the body of a view that runs as another user
|
26.3 in public has different code, the fix does not work there, so I would not backport it, there is a risk that I could break other things. |


ALTER TABLE <view> MODIFY QUERYreplaces the body that aSQL SECURITY DEFINERview runs with its definer's privileges, or that aSQL SECURITY NONEview runs without any checks. It required onlyALTER VIEW MODIFY QUERY, whichALTER TABLEimplies. So a user with that grant could make a view owned by another user read what that user can read, and then read the result from the view's target table. Since #117979, a table that the user cannot see at all is refused on the local path. But a grant on some columns,ON CLUSTER, or aReplicateddatabase still let such a body through, and so does a row policy.Rewriting the body now needs the grants that
CREATEandMODIFY SQL SECURITYdemand for that SQL security:SET DEFINERon the definer, orALLOW SQL SECURITY NONE. The initiator checks them, so they also apply toON CLUSTERand toReplicateddatabases. AnON CLUSTER MODIFY QUERYfor a view that the initiator does not have is refused, because the hosts that have it apply the entry without the user. When the same statement has aMODIFY SQL SECURITY, its new security is what gets authorized.The checks of the body itself, to match
CREATE, are in #123063.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Rewriting the body of a view with
ALTER TABLE ... MODIFY QUERYnow needs the same grants as creating it with its SQL security:SET DEFINERon the definer of aSQL SECURITY DEFINERview, orALLOW SQL SECURITY NONEfor aSQL SECURITY NONEview. AnON CLUSTER MODIFY QUERYfails withUNKNOWN_TABLEunless it runs on a host that has the view.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/121629]
Version info
26.10.1.1290-master(included in26.10and later)26.9.12.8,26.8.19.10,26.7.24.8