Skip to content

Require READ and WRITE on the source to create a table from a table function - #128996

Closed
kolsha wants to merge 9 commits into
masterfrom
create-as-table-function-source-access
Closed

kolsha wants to merge 9 commits into
masterfrom
create-as-table-function-source-access

Conversation

@kolsha

@kolsha kolsha commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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 ON CLUSTER queries, whose hosts create the table without that user. For example, a user with WRITE ON FILE but without READ ON FILE could read a file through CREATE TABLE t (line String) AS file(...).

Now both READ and WRITE on the source are required 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. A replayed definition (a short ATTACH, a replay on a follower replica, a recovery from stored metadata) is not checked again, since it was checked when the table was created; RESTORE is checked like a new definition, because its definition comes from the backup. The source of a table function wrapped in loop is checked as well. For legacy ON CLUSTER entry formats the initiator checks the definition the hosts will build (e.g. a table function replaced by Null with restore_replace_external_table_functions_to_null).

Rollout notes: for ON CLUSTER the table function is now resolved on the initiator, so a cluster or named collection that exists only on the hosts, or a different url_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):

  • Bug Fix (user-visible misbehavior in an official stable release)

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 both READ and WRITE on the source (e.g. READ ON FILE and WRITE ON FILE), also with an explicit column list, with a definition copied from another table, and with ON CLUSTER.


Workflow [PR]
Sync PR [sync-upstream/pr/128996]

kolsha and others added 2 commits November 3, 2026 14:16
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]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2a793ca]

Summary: ✅


AI Review

Summary

This PR closes the direct CREATE TABLE ... AS table_function(...) privilege gap by requiring the source grants that the persisted table will later exercise under the global context, and the local CREATE / ATTACH / RESTORE paths look aligned with that contract. I would still block it as-is, because RESTORE ... ON CLUSTER keeps a privilege-bypass path: the initiator checks backup contents without running the new table-function source authorization, while the actual per-host restore runs without a user.

Findings

❌ Blockers

  • [src/Interpreters/InterpreterCreateQuery.cpp:1090] RESTORE ... ON CLUSTER still bypasses the new source-grant check. The initiator-side CHECK_ACCESS_ONLY path only runs RestorerFromBackup::checkAccessForObjectsFoundInBackup() and never reaches checkCreateTableAsTableFunction, while the per-host restore executes without a user. A user who lacks READ ON FILE can therefore restore a backup containing CREATE TABLE ... AS file(...) onto the cluster and then read the file through the restored table.
    Suggested fix: mirror this authorization in the initiator-side restore preflight (for example by normalizing each restored ASTCreateQuery the same way and applying checkCreateTableAsTableFunction or an equivalent source check there), and add a focused RESTORE ... ON CLUSTER regression test.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions:
    • Enforce the same source-grant check for RESTORE ... ON CLUSTER before dispatching the internal per-host restore.
    • Add a focused regression test that covers restoring CREATE TABLE ... AS file(...) (or another source-backed table function) with ON CLUSTER.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 3, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. label Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2a793ca6a with master b306e87de (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 10746f6d0; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 25 s compile time in total, 7 of them have a recent master baseline.

Job report

kolsha and others added 2 commits November 3, 2026 20:24
…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]>
kolsha and others added 4 commits November 4, 2026 09:34
… `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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@groeneai let's fix this in a separate pr

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@groeneai open it against master

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do: I'll open it against master once this PR merges, since it builds on checkCreateTableAsTableFunction. Please mention me here when it does.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 url picks its engine from its arguments (the scheme), so only url is parsed, and url(<collection>) with the collection coming from the same backup is refused ON CLUSTER until the collection exists. One ITableFunction hook marks url.
  • B: A, plus resolving that collection from the backup in the preflight: nothing refused, more code.
  • C: keep RESTORE ... ON CLUSTER as it is, and I drop the follow-up.

I would go with A.

@kolsha
kolsha marked this pull request as ready for review November 11, 2026 10:24
@kolsha
kolsha requested a review from yariks5s November 16, 2026 13:50
@kolsha

kolsha commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #129605, which requires the TABLE ENGINE grant of the engine of the table function for CREATE TABLE ... AS f(...), at the same points as this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants