Repository navigation
Do not require privileges on the temporary table of CREATE OR REPLACE - #117967
Conversation
`CREATE OR REPLACE TABLE` / `VIEW` / `DICTIONARY` and `REPLACE TABLE` build the new object under an internal `_tmp_replace_<hash>_<random>` name and publish it with a `RENAME`/`EXCHANGE`. Several steps of that machinery were authorized against the internal name: the publishing rename asked for `SELECT` and `DROP TABLE` on it, the populating `INSERT SELECT` asked for `INSERT` on it, the `CLONE AS` attach asked for `ALTER DELETE` and `INSERT` on it, and the cleanup `DROP` of a failed replace asked for `DROP TABLE` on it. The name is random, so no grant can cover it, and the statement failed with `ACCESS_DENIED` for anyone without wildcard grants over the whole database. A failed replace additionally stranded its temporary table, because its cleanup `DROP` was denied too. The same rename also asked for `CREATE TABLE` and `INSERT` on the final name, which are not the grants of the object's kind, so replacing a view demanded `INSERT, CREATE TABLE` on the view. Now the internal steps skip their own access check, and every privilege is checked against a user-visible name: `CREATE`/`DROP` of the object's own kind on the final name up front, `DROP` of the replaced object's kind on its real name before the swap, `INSERT` on the final name before a populating `AS SELECT`, and `SELECT` on the source plus `ALTER DELETE`/`INSERT` on the final name for `CLONE AS`. `CREATE OR REPLACE` therefore needs exactly the grants of the plain statement it is equivalent to, plus the `DROP` of what it replaces. A populating `CREATE OR REPLACE TABLE ... AS SELECT` now requires `INSERT` on the final name, where it used to require `INSERT` on the temporary one. Without that check `CREATE TABLE` + `DROP TABLE` alone would let a user write arbitrary data into a table they cannot `INSERT` into. The publishing rename of a REPLACE keeps running as the user rather than under an internal context: it has to invalidate the storage cache of the user's query context for the names it swapped, which is what keeps a replaced materialized view subscribed to its source (#108726). Closes: #90919 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [0c0ece9] Summary: ✅
AI ReviewSummaryThe PR fixes the Tests
Final VerdictLLVM Coverage ReportMeasured on commit 0c0ece9.
Changed lines: Changed C/C++ lines covered: 50/51 (98.04%) · Uncovered code |
| ${CLICKHOUSE_CLIENT} --query "SELECT x FROM ${db}.v" | ||
|
|
||
| echo "-- [CREATE TABLE, DROP TABLE, INSERT ON db.t_populated] replacing with a populating SELECT must succeed:" | ||
| run_granted "CREATE OR REPLACE TABLE ${db}.t_populated ENGINE = MergeTree ORDER BY a AS SELECT a FROM ${db}.src" |
There was a problem hiding this comment.
isCreateQueryWithImmediateInsertSelect() also covers CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE, so this RBAC change is no longer table-only. The new coverage only exercises CREATE OR REPLACE TABLE ... AS SELECT, which leaves the populated materialized-view path unproven: a regression there could still authorize against _tmp_replace_* and CI would not notice. Please add one focused POPULATE grant test, including a negative case without the required INSERT.
| CREATE TABLE cloned_denied (a Int32) ENGINE = MergeTree ORDER BY a; | ||
|
|
||
| -- Table-scoped grants only: none of them can cover the internal \`_tmp_replace_*\` name. | ||
| GRANT SELECT ON ${db}.src TO ${granted}, ${nogrant}; |
There was a problem hiding this comment.
The denied user still has SELECT on src, while the code now bypasses InterpreterAlterQuery's built-in access check and replays REPLACE PARTITION access manually before setSkipAccessCheck(true). That means this test would still fail even if the new manual check accidentally dropped the source-side SELECT requirement. Please add a second denied case without SELECT on the clone source so the moved RBAC check is fully pinned down.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units49 translation units recompiled, 455 s compile time in total, 49 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbenchFlagged queries (7 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
alexey-milovidov
left a comment
There was a problem hiding this comment.
Ok, reasonable.
Resolve the cherry-pick of #124477 onto 26.8. The only conflict is context: on master the new settings list sits above fillTableIfNeeded, whose signature #117967 changed (published_table_name); 26.8 keeps its own signature (skip_target_insert_access_check), so the resolution adds only the lines #124477 adds and does not bring #117967 along. The test files merged cleanly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CREATE OR REPLACE TABLE/VIEW/DICTIONARYandREPLACE TABLEbuild the new object under an internalname (
_tmp_replace_<hash>_<random>) and publish it with aRENAME/EXCHANGE. Several steps of thatmachinery were authorized against the internal name: the publishing rename asked for
SELECTandDROP TABLEon it, the populatingINSERT SELECTasked forINSERTon it, theCLONE ASattach asked forALTER DELETEandINSERTon it, and the cleanupDROPof a failed replace asked forDROP TABLEon it.The name is random, so no grant can ever cover it, and the statement failed for anyone without wildcard
grants over the whole database:
The same rename also asked for
CREATE TABLEandINSERTon the final name, which are not the grants of theobject's kind, so replacing a view demanded
INSERT, CREATE TABLE ON default.the_view. And because thecleanup
DROPwas denied too, a failed replace left its temporary table behind (Cannot DROP temporary table: ... it's necessary to have the grant DROP TABLE ON default._tmp_replace_*).Now every privilege is checked against a user-visible name, and the internal steps on the temporary name are
not authorized against it:
CREATE/DROPof the object's own kind on the final name is checked up front, as before, andDROPof thereplaced object's kind is checked on its real name right before the swap, as before;
INSERTon the final name is checked, as the user, before a populatingAS SELECT, andSELECTon thesources is still checked by the populate itself;
CLONE ASchecksSELECTon the source andALTER DELETE/INSERTon the final name, as the user.So
CREATE OR REPLACEnow needs exactly the grants of the plain statement it is equivalent to, plus theDROPof what it replaces, and table-scoped grants are enough.One case becomes stricter rather than looser: a populating
CREATE OR REPLACE TABLE ... AS SELECTnowrequires
INSERTon the final name. Previously it requiredINSERTon the temporary name, so users whoworked around this bug by granting on the
_tmp_replace*prefix (instead of on the table) need the grant onthe table. Without that check
CREATE TABLE+DROP TABLEalone would let a user write arbitrary data intoa table they cannot
INSERTinto.The publishing rename of a REPLACE keeps running as the user rather than under an internal context: it has to
invalidate the storage cache of the user's query context for the names it swapped, which is what keeps the
replaced materialized view subscribed to its source (issue #108726, covered by
04492_create_or_replace_materialized_view_populate_keeps_subscription).Tests
05060_create_or_replace_scoped_grantsand05061_create_or_replace_clone_as_scoped_grantscover thetable, view,
AS SELECTandCLONE ASforms with table-scoped grants, and check that a user missingDROP,INSERTor the sourceSELECTis still denied and leaves no temporary table behind.Closes: #90919
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
CREATE OR REPLACE TABLE/VIEW/DICTIONARYandREPLACE TABLErequiring privileges on the internaltemporary table they create (
_tmp_replace_*). Its name is random, so no grant could cover it and the queryfailed with
ACCESS_DENIEDunless the user held wildcard grants on the whole database. The requiredprivileges are now checked against the user-visible names only: replacing an object needs the
CREATEandDROPgrants of its own kind, plusINSERTon it when the statement populates it.Workflow [PR]
Sync PR [sync-upstream/pr/117967]
Version info
26.9.1.808(included in26.9and later)