Repository navigation
Make DELETE FROM require only the ALTER DELETE privilege - #107491
tiandiwonder merged 9 commits into
Conversation
For MergeTree, `DELETE FROM` is a lightweight delete that is rewritten internally into `ALTER TABLE ... UPDATE _row_exists = 0 WHERE ...`. The generated mutation's access check required ALTER_UPDATE on every assigned column, including the hidden `_row_exists` marker, so a user effectively needed ALTER_UPDATE(_row_exists) in addition to ALTER_DELETE. This contradicts the documentation, which states DELETE requires only ALTER DELETE. Treat an assignment to the `_row_exists` lightweight-delete marker as a delete operation governed by ALTER DELETE rather than ALTER UPDATE, in both InterpreterAlterQuery::getRequiredAccessForCommand (UPDATE case) and InterpreterUpdateQuery (covers the lightweight-update delete mode and a direct `UPDATE ... SET _row_exists = 0`). The heavy-delete path already checks only ALTER_DELETE. Closes: ClickHouse#90754 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [4eea2e4] Summary: ✅
AI ReviewSummaryThis PR changes the Findings❌ [src/Interpreters/InterpreterAlterQuery.cpp:608, src/Interpreters/InterpreterUpdateQuery.cpp:101] The new
Tests
Final VerdictStatus: ❌ Block |
The privilege check treated every assignment to the _row_exists lightweight- delete marker as ALTER DELETE. But only _row_exists = 0 is a delete (what DELETE FROM rewrites to); the mutation path accepts arbitrary expressions for this hidden column, so a user with only ALTER DELETE could run ALTER TABLE t UPDATE _row_exists = 1 (or the same via the UPDATE statement) to edit or resurrect the deletion mask without ALTER UPDATE. Add a shared helper isLightweightDeleteAssignment (true only for _row_exists = 0) in MergeTreeVirtualColumns, and use it in both InterpreterAlterQuery (ALTER ... UPDATE) and InterpreterUpdateQuery: _row_exists = 0 requires ALTER DELETE, any other _row_exists assignment is treated as a column update requiring ALTER UPDATE. Extend the privilege test to assert _row_exists = 1 needs ALTER UPDATE while _row_exists = 0 needs ALTER DELETE.
…le comment Add UPDATE tab_lw SET _row_exists = 0/1 checks (InterpreterUpdateQuery path) for both the ALTER DELETE-only and ALTER UPDATE-only users, mirroring the ALTER TABLE checks, so the value-gated split is regression-covered on both paths. Update the stale note in 04077_lwd_alter_delete_privilege.sh: only _row_exists = 0 is governed by ALTER DELETE; any other assignment needs ALTER UPDATE.
The _row_exists = 0 -> ALTER DELETE shortcut keyed only on the column name and literal, but _row_exists is the lightweight-delete marker only on engines that expose it as a virtual column. On an engine where _row_exists is an ordinary physical column (e.g. Memory), ALTER TABLE t UPDATE _row_exists = 0 would have been treated as a delete, letting an ALTER DELETE-only user mutate real data. Gate the shortcut on the column not being a physical column of the target table: getRequiredAccess resolves the storage and passes that down to getRequiredAccessForCommand; InterpreterUpdateQuery resolves the table the same way. A null storage (non-local ON CLUSTER target, ALTER DATABASE) fails closed to ALTER UPDATE. Add a Memory-table regression case with a physical _row_exists column.
getRequiredAccessForCommand gained a fourth argument, but InterpreterKillQueryQuery calls it (KILL MUTATION and KILL ... MOVE PARTITION) and was not updated - a build break. Extract the storage-aware marker check into a shared static InterpreterAlterQuery::isRowExistsLightweightDeleteMarker and use it at both KILL sites (each already has the resolved storage), in getRequiredAccess, and in InterpreterUpdateQuery.
CurtizJ
left a comment
There was a problem hiding this comment.
LGTM, but please fix one AI review comment.
…tual marker Address review comment (clickhouse-gh, endorsed by CurtizJ): the previous `!hasPhysical(_row_exists)` check was too broad. Because `getRequiredAccess` computes the marker state once on the pre-ALTER table, a user with `ALTER ADD COLUMN` + `ALTER DELETE` (but no `ALTER UPDATE`) could run `ALTER TABLE t ADD COLUMN _row_exists UInt8, UPDATE _row_exists = 0` and edit a real physical column with only `ALTER DELETE`. Detect the lightweight-delete marker via `isVirtualColumn(RowExistsColumn::name)` so the `ALTER DELETE` shortcut applies only to storages that actually register `_row_exists` as the virtual marker (the MergeTree family), not to e.g. `Memory`. Add a regression case in `04328_delete_from_alter_delete_privilege`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
| for (const ASTPtr & assignment_ast : command.update_assignments->children) | ||
| { | ||
| const auto & assignment = assignment_ast->as<const ASTAssignment &>(); | ||
| if (row_exists_is_lightweight_marker && isLightweightDeleteAssignment(assignment)) |
There was a problem hiding this comment.
This shortcut now makes the raw _row_exists = 0 mutation reachable with only ALTER DELETE, but the execution path behind it still skips the DELETE FROM safety gates. InterpreterDeleteQuery is the only place that checks enable_lightweight_delete and rejects projections when lightweight_mutation_projection_mode = THROW; direct ALTER TABLE ... UPDATE _row_exists = 0 and the corresponding UPDATE statement go straight through MutationsInterpreter, which only checks supportsLightweightDelete().
As a result, after this patch an ALTER DELETE-only user can still create a lightweight-delete mask even when lightweight deletes are disabled or projections are configured to THROW, for example via SET enable_lightweight_delete = 0; ALTER TABLE t UPDATE _row_exists = 0 WHERE .... Before this change they still needed ALTER UPDATE for that raw marker assignment. Please keep the _row_exists = 0 shortcut behind the same guards as DELETE FROM, or reject the raw marker-assignment form whenever those guards would fail.
There was a problem hiding this comment.
This is pre-existing behavior, not introduced here: ALTER TABLE ... UPDATE _row_exists = 0 has been accepted and executed via InterpreterAlterQuery → MutationsInterpreter since lightweight deletes were introduced, and it has never passed through the enable_lightweight_delete / lightweight_mutation_projection_mode = THROW checks, which live only in InterpreterDeleteQuery. This PR changes only which privilege that form requires; execution paths and gating are untouched, and the projection handling itself is enforced deeper in MutateTask for any _row_exists mutation regardless of entry path — gating the raw ALTER form on those settings would be a behavior change best done as a separate follow-up.
# Conflicts: # src/Storages/MergeTree/MergeTreeVirtualColumns.cpp
| @@ -293,7 +293,8 @@ BlockIO InterpreterKillQueryQuery::execute() | |||
| getContext()->getSettingsRef()[Setting::max_parser_depth], | |||
| getContext()->getSettingsRef()[Setting::max_parser_backtracks]); | |||
| required_access_rights = InterpreterAlterQuery::getRequiredAccessForCommand( | |||
There was a problem hiding this comment.
KILL MUTATION is only consistent for the local path now. If the query has ON CLUSTER, execute returns before this per-command check and getRequiredAccessForDDLOnCluster still unconditionally asks for ALTER_UPDATE | ALTER_DELETE | ... for every mutation.
That means a user with only ALTER DELETE can kill the _row_exists = 0 delete-shaped mutation locally, but the same KILL MUTATION ... ON CLUSTER is still rejected on the initiator with ACCESS_DENIED. Please align the cluster pre-check with the per-command access rules, or the privilege contract this PR is fixing stays inconsistent across local vs cluster KILL MUTATION.
There was a problem hiding this comment.
The local-vs-cluster asymmetry is real but pre-existing for every mutation shape — the coarse ON CLUSTER pre-check (all ALTER bits) predates this PR and is deliberately fail-close because the initiator cannot resolve the mutation's command shape before the DDL fans out; this PR only touches the local per-command check. Aligning the cluster pre-check is a fair follow-up, but out of scope for this privilege fix.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 73/82 (89.02%) · Uncovered code |
a943f71
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRMake DELETE FROM require only the ALTER DELETE privilege ClickGap verified: reproduced it with the script below on Reproduction-- Setup (run as an admin with access_management enabled):
CREATE TABLE tab (id UInt32, val UInt32) ENGINE = MergeTree ORDER BY id;
INSERT INTO tab SELECT number, number FROM numbers(10);
CREATE USER u_del IDENTIFIED WITH plaintext_password BY 'pw';
GRANT SELECT, ALTER DELETE ON default.tab TO u_del;
-- Then, as user u_del (granted only SELECT + ALTER DELETE):
DELETE FROM tab WHERE id = 1;
-- Observed pre-fix: Code: 497. ACCESS_DENIED — 'it's necessary to have the grant
-- ALTER UPDATE(_row_exists) ON default.tab', even though only ALTER DELETE is documented.
-- Fixed: the DELETE succeeds with only ALTER DELETE.Bisect
Affects
Suggested next stepThe fix has landed on master; the affected release branches above still need the backport. Component: cc fix PR author: @tiandiwonder ClickGap · Finding: Triage metadata |
…m-row-exists-privilege Make DELETE FROM require only the ALTER DELETE privilege
Closes: #90754
Problem
A user granted only
ALTER DELETEcannot runDELETE FROM x WHERE ...on a MergeTree table. It fails with:The documentation states that
DELETErequires only theALTER DELETEprivilege, so the behavior and the documentation disagree.Root cause
For MergeTree,
DELETE FROMis a lightweight delete that is rewritten internally intoALTER TABLE ... UPDATE _row_exists = 0 WHERE ...(InterpreterDeleteQuery). That generated mutation runs throughInterpreterAlterQuery, whosegetRequiredAccessForCommandUPDATE case requiresALTER_UPDATEon every assigned column — including the hidden_row_existsmarker. So the user effectively needsALTER_UPDATE(_row_exists)in addition toALTER_DELETE. The lightweight-update delete mode hits the same gap throughInterpreterUpdateQuery, which required table-levelALTER_UPDATE.Fix
Treat an assignment to
_row_existsas a delete (governed byALTER DELETE) only when it is the exact lightweight-delete form_row_exists = 0on the hidden_row_existsmarker. Any other assignment — a non-zero value, or the column on an engine where_row_existsis an ordinary physical column (e.g.Memory) — stays a normal update requiringALTER UPDATE. The same rule is applied toALTER ... UPDATE, theUPDATEstatement, and the correspondingKILLaccess checks.So
DELETE FROM(which rewrites to_row_exists = 0) needs onlyALTER DELETE, as documented, while a user with onlyALTER DELETEstill cannot edit the deletion mask (e.g._row_exists = 1) or mutate ordinary data through a physical_row_existscolumn.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
DELETE FROM(lightweight delete) requiring theALTER UPDATEprivilege in addition toALTER DELETE. A user granted onlyALTER DELETEcan now runDELETE FROM, as documented.Documentation entry for user-facing changes
Version info
26.7.1.736(included in26.7and later)