Repository navigation
Conversation
Add a test that a user holding `WRITE ON FILE` but not `READ ON FILE` can create no table from `file`: with an explicit column list, with the structure inferred from the file, with the definition copied from another table, and with `ON CLUSTER` in both the current and the legacy `distributed_ddl_entry_format_version`. With `READ ON FILE` alone the creation is refused as well, and with both grants it succeeds. The test fails without the fix: the table with an explicit column list and the `ON CLUSTER` tables are created, and the first one returns the content of the file to a user who cannot read it directly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a table function A table created by `CREATE TABLE ... AS f(...)` reads and writes the source of `f` under the global context, so a `SELECT` from it does not check the source grants of the reader. Creating such a table checked only `WRITE` on the source when the column list was given explicitly or copied from another table, and at most `READ` under the issuing user for an `ON CLUSTER` query, whose hosts create the table without that user. A user with `WRITE ON FILE` but without `READ ON FILE` could thus read a file through such a table. Check both `READ` and `WRITE` on the source under the issuing user, before the query is sent to other hosts: in `getTablePropertiesAndNormalizeCreateQuery` for every way the structure is obtained, and in `execute` for the legacy `ON CLUSTER` entry formats, which ship the query without normalizing it. This matches what the implicit `TABLE ENGINE` grant for the matching engine requires. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [2a793ca] Summary: ✅
AI ReviewSummaryThis PR closes the direct Findings❌ Blockers
Final Verdict
|
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, 25 s compile time in total, 7 of them have a recent master baseline. |
…hout `READ ON FILE` Extend the test of `CREATE TABLE ... AS file(...)`: after `READ ON FILE` is revoked from the user who created the table, a short `ATTACH TABLE` by that user still attaches it from the stored definition, while a `RESTORE` of the same definition from a backup is refused for the missing `READ ON FILE`. The `ATTACH` fails without the fix, because the source access is checked again for the definition that it replays. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…S f(...)` `getTablePropertiesAndNormalizeCreateQuery` also runs for a definition that is replayed rather than supplied by the user: a short `ATTACH`, a replay on a follower replica, a recovery from stored metadata. Checking `READ` and `WRITE` on the source there authorized an existing table again under the context that replays it, so a short `ATTACH` failed once the grants of the attaching user no longer covered the source. Run these checks only for a definition that `isReplayedTableDefinition` does not consider replayed, as other checks of a fresh definition do, and for `RESTORE`: it runs under `SECONDARY_CREATE` too, but its definition comes from the backup and it runs under the user who restores it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… `Null` With `distributed_ddl_entry_format_version = 2` the hosts of an `ON CLUSTER` query receive the query settings, so with `restore_replace_external_table_functions_to_null = 1` they create a `Null` table instead of resolving `file`, as a local `CREATE` does. Check that a user without `READ ON FILE` can create such a table, while with `distributed_ddl_entry_format_version = 1`, which ships no settings, the creation is still refused. The test fails without the fix: the initiator checks the source of the table function that the hosts do not keep. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e hosts build it The legacy `ON CLUSTER` branch of `execute` checked the source of a direct `AS f(...)` unconditionally. With `distributed_ddl_entry_format_version = 2` the hosts receive the query settings, and with `restore_replace_external_table_functions_to_null` they replace `f` with a `Null` table, as a local `CREATE` does, so the initiator refused a query that both would accept. Check the direct `AS f(...)` on the same copy of the query as `AS src`, after `setEngine` under the context that drops these settings for `DDLLogEntry::OLDEST_VERSION`, which ships none: `TABLE ENGINE` for the engine it gets, and the table function only if it keeps one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Extend the test of the source grants required to create a table from a table function: a user holding `WRITE ON FILE` but not `READ ON FILE` cannot create a table from `loop(file(...))` either, since that table reads the same file. The test fails without the fix: the source of the wrapped table function is not checked, so the table is created. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`loop` has no source of its own, so `checkSourceAccess` of `loop(f(...))` checked nothing, and `CREATE TABLE ... AS loop(f(...))` with a column list created a table that reads the source of `f` without `READ` on it. Make `ITableFunction::checkSourceAccess` virtual and let `loop` delegate it to the wrapped table function. `loop` over a table is unchanged. Do not check `RESTORE` again on the followers of a `Replicated` database, which replay a restore already checked where it was issued, and state in the comments that only the new `READ` and `WRITE` check is skipped for a replayed definition: `doCreateTable` still checks `WRITE` on the source under the context that replays it, so a short `ATTACH` by a user without `WRITE` on the source fails as before, now also for the source wrapped in `loop`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The source of `f` was checked in three places of `getTablePropertiesAndNormalizeCreateQuery`, behind a predicate of its own that skipped replayed definitions but re-enabled `RESTORE`. The same function already checks `TABLE ENGINE` right after `setEngine`, without any gate, and the implicit `TABLE ENGINE` grant of a source engine (e.g. `File`) is exactly `READ` and `WRITE` on its source. Check `READ` and `WRITE` on the source of `f` there, in one place: it sees `AS src` inheriting the table function and its replacement with `Null`, runs before the structure is inferred, before a `Replicated` database or an `ON CLUSTER` query sends the query, and for a local `RESTORE`. The legacy `ON CLUSTER` preflight keeps the same check next to its own `TABLE ENGINE` check. As for `ENGINE = File`, a short `ATTACH` now needs `READ` as well as `WRITE`. Drop the `checkSourceAccess` override of `loop`: a `Loop` table reads the wrapped table function through a query under the context of the reader, so `READ` is checked at read time, and `Loop` has no write path. The override made `doCreateTable` require `WRITE` on the wrapped source, refusing `CREATE TABLE ... AS loop(file(...))` to a user with `READ ON FILE` only. `RESTORE ... ON CLUSTER` is not covered: its hosts restore without the user, and the initiator checks neither `TABLE ENGINE` nor the source of a table function in `RestorerFromBackup::checkAccessForObjectsFoundInBackup`.
| /// The same for a table function, which `setEngine` may also have inherited from the `AS` source table or | ||
| /// replaced with `Null`. Checked before the structure is inferred from it, which reads the source. | ||
| if (create.as_table_function) | ||
| checkCreateTableAsTableFunction(*create.as_table_function, getContext()); |
There was a problem hiding this comment.
RESTORE ... ON CLUSTER still bypasses this new source-grant check. The initiator-side preflight only runs RestorerFromBackup::checkAccessForObjectsFoundInBackup() in CHECK_ACCESS_ONLY (src/Backups/BackupsWorker.cpp:1275, src/Backups/RestorerFromBackup.cpp:337), which never reaches checkCreateTableAsTableFunction, while the actual per-host restore executes without a user.
That means a user who lacks READ ON FILE can still restore a backup containing CREATE TABLE ... AS file(...) onto the cluster and then read the file through the restored table. Please mirror this authorization in the restore preflight (or otherwise normalize/check the restored ASTCreateQuery there) and add a focused RESTORE ... ON CLUSTER regression test.
There was a problem hiding this comment.
@groeneai let's fix this in a separate pr
There was a problem hiding this comment.
Acknowledged. I'm investigating the RESTORE ... ON CLUSTER preflight bypass of this source-grant check; I'll follow up here with a separate PR (or my findings) shortly.
There was a problem hiding this comment.
The fix is ready as one commit on top of this PR's head: in CHECK_ACCESS_ONLY mode, RestorerFromBackup::checkAccessForObjectsFoundInBackup now runs checkCreateTableAsTableFunction (and the TABLE ENGINE grant of f) for every table the restore creates AS f(...), plus a stateless test with RESTORE ... ON CLUSTER test_shard_localhost that fails without it. It calls checkCreateTableAsTableFunction from this PR, so it cannot target master yet.
@kolsha which do you prefer: (A) I open it now with base create-as-table-function-source-access, so it can be reviewed in parallel and retargeted to master after this PR merges, or (B) I open it against master after this PR merges (please mention me here when it does)?
There was a problem hiding this comment.
Will do: I'll open it against master once this PR merges, since it builds on checkCreateTableAsTableFunction. Please mention me here when it does.
There was a problem hiding this comment.
@foadl a design question before I open the follow-up that @ kolsha asked for in this thread. #129605 requires TABLE ENGINE of f for CREATE TABLE ... AS f(...) in getRequiredAccess() and in the local create, but the RESTORE ... ON CLUSTER preflight on the initiator (checkAccessForObjectsFoundInBackup in CHECK_ACCESS_ONLY, which since #129468 checks ENGINE = and inner engines) does not look at AS f(...), and the hosts create the table with no user. You removed that check from #129605 in d1b146e because parsing f on the initiator fails when its named collection is restored from the same backup (access is checked before restoreNamedCollections).
Which do you prefer?
- A: take the engine from the function class without parsing. Only
urlpicks its engine from its arguments (the scheme), so onlyurlis parsed, andurl(<collection>)with the collection coming from the same backup is refusedON CLUSTERuntil the collection exists. OneITableFunctionhook marksurl. - B: A, plus resolving that collection from the backup in the preflight: nothing refused, more code.
- C: keep
RESTORE ... ON CLUSTERas it is, and I drop the follow-up.
I would go with A.
|
Superseded by #129605, which requires the |
A table created by
CREATE TABLE ... AS f(...)reads and writes the source offunder the global context, so aSELECTfrom it does not check the source grants of the reader. Creating such a table checked onlyWRITEon the source when the column list was given explicitly or copied from another table, and at mostREADunder the issuing user forON CLUSTERqueries, whose hosts create the table without that user. For example, a user withWRITE ON FILEbut withoutREAD ON FILEcould read a file throughCREATE TABLE t (line String) AS file(...).Now both
READandWRITEon the source are required under the issuing user, before the query is sent to other hosts: ingetTablePropertiesAndNormalizeCreateQueryfor every way the structure is obtained, and inexecutefor the legacyON CLUSTERentry formats, which ship the query without normalizing it. This matches what the implicitTABLE ENGINEgrant for the matching engine requires. A replayed definition (a shortATTACH, a replay on a follower replica, a recovery from stored metadata) is not checked again, since it was checked when the table was created;RESTOREis checked like a new definition, because its definition comes from the backup. The source of a table function wrapped inloopis checked as well. For legacyON CLUSTERentry formats the initiator checks the definition the hosts will build (e.g. a table function replaced byNullwithrestore_replace_external_table_functions_to_null).Rollout notes: for
ON CLUSTERthe table function is now resolved on the initiator, so a cluster or named collection that exists only on the hosts, or a differenturl_base, makes the initiator reject the query, and filtered source grants are checked against the URI as the initiator resolves it.The first commit adds the test, the second contains the fix.
🤖 Generated with Claude Code
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix missing privilege check in
CREATE TABLE ... AS table_function(...): creating the table now requires bothREADandWRITEon the source (e.g.READ ON FILEandWRITE ON FILE), also with an explicit column list, with a definition copied from another table, and withON CLUSTER.Workflow [PR]
Sync PR [sync-upstream/pr/128996]