Skip to content

Cherry pick #115914 to 26.7: Check the target engine's grant when ATTACH carries a URL engine definition - #115950

Closed
robot-clickhouse wants to merge 7 commits into
backport/26.7/115914from
cherrypick/26.7/115914
Closed

robot-clickhouse wants to merge 7 commits into
backport/26.7/115914from
cherrypick/26.7/115914

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #115914

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 7 commits August 22, 2026 03:43
…nition

ENGINE = URL(...) dispatches by URL scheme to File/S3/AzureBlobStorage/HDFS and
re-checks TABLE ENGINE on that target inside the storage creator. The re-check was
gated on LoadingStrictnessLevel::CREATE, so an ATTACH carrying a full definition
skipped it: a user holding only TABLE ENGINE ON URL could attach a File-backed
table and read it, while the equivalent CREATE was correctly denied.

The gate's comment justified itself by claiming it mirrored where the outer engine
privilege is checked. That premise was false. Neither outer site
(InterpreterCreateQuery.cpp:772 and :3522) consults the mode, which is exactly why
ATTACH ... ENGINE = File(...) is denied while the same engine reached through
ENGINE = URL('file://...') was not: the outer check sees only URL, which the user
holds, and the inner check that would have seen File did not run. No source check
exists at read time, so the table remained readable afterwards. The bypass is
reachable on Atomic, the default database engine, through the
ATTACH TABLE t UUID '<uuid>' <definition> form.

Use the fresh-definition predicate that already answers this question in
StorageDistributed, StorageBuffer, StorageNATS and DatabaseBackup, rather than a
mode equality: authorize whenever a definition is introduced now, and skip only
when replaying one already stored on this server. A bare
!isLoadingFromExistingMetadata would not do, because short ATTACH TABLE t and the
tables loaded by ATTACH DATABASE run at mode ATTACH rather than FORCE_ATTACH and
would newly be denied; attach_short_syntax is what separates those.

This also authorizes SECONDARY_CREATE. For replicated-database DDL replay that is a
no-op, since the DDL worker context carries no user and so has full access. For
RESTORE it is a deliberate behaviour change, matching the ruling in
StorageDistributed.cpp:2676-2691 that a restore introduces a definition under a
possibly different user and must be validated like a CREATE.

Extend 04401_url_engine_s3_dispatch_engine_grant with the ATTACH side: file:// and
az:// denials, an http:// positive control, and a short ATTACH arm that pins the
exempt half, so a later simplification to the bare predicate cannot pass. Anchor
the denial assertions on the privilege sentence as well; the previous pattern could
match the failing query echoed back by the client, which contains both the engine
name and the word grant, and so would have accepted a query that was never denied.
The two arms asserting an ATTACH was permitted inferred that from the absence of a
denial in the output. Every other failure leaves the same empty match, so a parse
error, a UUID collision or any storage-construction throw printed the allowed label;
the script has no set -e and the trailing DROP TABLE IF EXISTS succeeds either way,
so nothing downstream exposed it. Measured on the same binary with a failure
injected into each arm: the previous form reported OK while the ATTACH it was
asserting had failed with TABLE_ALREADY_EXISTS, and with CANNOT_GET_CREATE_TABLE_QUERY
for the short ATTACH. Both arms now also assert the table is present in
system.tables, and an unrelated failure prints a third label that is absent from the
reference file, so it surfaces as a mismatch rather than picking either existing
label. This matters most for the short ATTACH arm, which is what pins the exempt
half of the predicate; the mutation dropping that half still reddens it, through the
denial branch rather than the new failure branch.

RESTORE is the one deliberate behaviour change here and had no arm of its own. Add
one following the structure of 04401_remote_storage_engine_restore_access, which
covers the equivalent ruling for the Remote engine: deny without the target engine
grant, assert nothing was created, grant it, then succeed. On the pre-fix binary the
first two invert, so the arm is not vacuous, while the granted retry passes on both
and shows this is a grant requirement rather than a hard break.

The backup destination carries the shell's pid. A destination may not be written
twice and no statement deletes one, and a fixed --database stops the unique name from
varying between runs of a single test, so without the suffix a repeated run fails
BACKUP_ALREADY_EXISTS: measured 1 of 3 runs passing without it against 3 of 3 with it.
The delegate engine check in the URL scheme dispatcher treats
attach_short_syntax as proof that the definition it is about to build came
from this server's own metadata, and therefore skips the target TABLE ENGINE
grant. That inference is sound for every parsed query: no parser sets the
flag, and all of its setters run after parsing, where the interpreter is
replaying a definition it already stored.

It is not sound for a clickhouse_json payload, where the flag is an input the
user supplies directly. readJSON rejected it only when the query was not an
ATTACH, so an ATTACH payload could set it freely, keep its own storage clause
(the interpreter replaces the query from stored metadata only when no storage
definition is present), and reach DatabaseOnDisk's short-syntax early return
with a user-supplied file:// definition marked as replayed metadata. That is
reachable on Atomic, the default, because DETACH only nulls the value in the
UUID map while the mapping check tests for the key: create an http:// table
with a known UUID, detach it, then submit the forged definition under that
UUID.

Reject the flag unconditionally instead, generalizing the guard that was
already there rather than adding a second mechanism. Nothing legitimate
regresses: writeJSON emits the field unconditionally, so every
parseQueryToJSON round trip carries false, and the interpreter paths that do
set it never serialize to JSON on the request path.

The existing AST JSON validation test gains the ATTACH analogue of its
CREATE arm.
… to exist

The arms asserting a statement was permitted checked that the table is
present afterwards. A table left behind by an interrupted run satisfies that
without the statement having run at all: the ATTACH fails
TABLE_ALREADY_EXISTS, the failure does not match the denial pattern, and the
arm prints the label that is supposed to mean the grant was accepted. Measured
on one binary with a leftover holding the arm's name and UUID, the arm passed
with output byte-identical to the reference while its ATTACH had returned 57.

Reachable because the unique name only varies with the database name, and the
stress runner passes a fixed one, so repeated runs of a test reuse the same
table names.

Clear each target first, so its presence afterwards is attributable to the
statement under test, and require exit status 0 in addition to the absence of
a denial. The count probe now reports a failure of its own query rather than
returning it as absence, which the arms asserting nothing was created would
otherwise accept as their pass.
…ape"

This reverts commit 10b0d3b.

PR #114171 already closes the same hole in a stronger form: it rejects the
key's presence rather than only a true value, and stops writeJSON emitting
the field at all, so there is no forgeable input left. Its hunks touch these
exact lines and conflict only with this commit, and its removal of the field
would silently void the test arm added here (the arm injects into a needle
that leaves the payload).

The URL delegate engine check this branch is about does not depend on the
revert: attach_short_syntax remains a sound provenance signal for every
parsed query, which is the only shape reachable without the default-off
enable_json_ast_dialect setting.
…denied

The http CREATE arm read "no access-denial text matched" as success. It captured no
exit status and did not check that the table was there, so a parse error, a table left
by an interrupted run, a host-filter refusal or any other non-access failure printed
the permitted label and the test passed.

That arm is the file's only guard against the four denial arms passing for the wrong
reason: a user holding no engine grant at all is denied everywhere, and only this arm
would notice. A vacuous positive control therefore lets a regression that breaks
URL-engine CREATE outright show all-green.

It now uses the shape the ATTACH http arm already uses on the same statement: the
target is dropped first as the admin, so its absence makes the presence afterwards
attributable to this statement, and the permitted label requires exit status 0 and the
table's presence in addition to the absence of a denial. The reference file is
unchanged; the new failure label is absent from it, so it surfaces as a diff.

Reproduced before fixing: with a leftover table holding the arm's name, on one binary,
the old shape passes while the new one reports rc=57 TABLE_ALREADY_EXISTS.
…atch-engine-grant

Check the target engine's grant when ATTACH carries a URL engine definition
@groeneai

Copy link
Copy Markdown
Collaborator

26.7 is the one branch #115914 actually applies to, and the port is exact.

The bypass is live on the shipped 26.7.6.8. As a user holding only URL and
TABLE ENGINE ON URL, on a Memory database: CREATE TABLE ... ENGINE = URL('file://.../secret.csv')
is denied Code: 497 ... grant TABLE ENGINE ON File, and ATTACH TABLE ... ENGINE = File(...) is
denied the same way, but ATTACH TABLE ... ENGINE = URL('file://.../secret.csv') is accepted and
the following SELECT returns the file contents.

tryDispatchURLEngineByScheme is byte-identical between master and the 26.7 tip (144 lines
each), so the fix hunk applies as written. The resolved tree is the same three files as #115914,
+175/-5.

The conflict is only the two test files, and only because master renamed
04401_url_engine_s3_dispatch.sh to 04401_url_engine_s3_dispatch_engine_grant.sh
(57ec4e243875e44, then d4ab16c71d30bc0). The new name is master-only, so it is a modify/delete
pair on this branch. StorageURL.cpp itself merges cleanly here, unlike on 25.8/26.3/26.5/26.6
where the whole dispatch block is absent.

Validated in both directions against a stateless-equivalent server, having first asserted live that
table_engines_require_grant denies a no-grant user and that the backups disk accepts a backup:

  • official 26.7.6.8: the ported test fails on exactly four lines, attach-file-engine-denied,
    attach-azure-engine-denied, restore-file-engine-denied and restore-rejected-not-created,
    the same signature it produced against pre-fix master.
  • fixed build (Build-ID d24e031ab4c378abd55ed58bf3f82e0753b81f59, the binary Check the target engine's grant when ATTACH carries a URL engine definition #115914 shipped):
    passes, zero diff.

So the port is not a silent no-op on this branch.

I cannot resolve the conflicts here, because I have no push access to this repository and the
cherrypick/26.7/115914 branch lives in it. The resolved tree is open as #115966, a PR against
26.7 from my fork, the same route as #112916 and #112899. This one can be closed in favour of
it.

clickhouse-gh Bot pushed a commit that referenced this pull request Aug 23, 2026
… carries a URL engine definition

The URL engine dispatches by scheme to File/S3/AzureBlobStorage/HDFS. The
delegate's TABLE ENGINE privilege was re-checked only when the mode was
CREATE, so a user holding TABLE ENGINE ON URL alone could ATTACH a table with
a full URL('file://...') definition and then read it, while the equivalent
CREATE and a direct ENGINE = File(...) ATTACH were both denied. The outer
engine check is not mode-gated, so the inner one was the only gap.

The predicate now exempts only a definition replayed from this server's own
metadata: server startup, force-restore, a short ATTACH TABLE t, and the
tables an ATTACH DATABASE loads. Those were validated when the table was
created and must stay loadable after a revoke. Every other statement, ATTACH
with a definition and RESTORE included, supplies a fresh definition and is
checked.

The cherry-pick #115950 conflicts only because master renamed
04401_url_engine_s3_dispatch.sh to 04401_url_engine_s3_dispatch_engine_grant.sh,
which makes the pair a modify/delete on this branch. The source hunk applies
unchanged: tryDispatchURLEngineByScheme is byte-identical between master and
26.7. This is that resolved tree as a single commit on the 26.7 tip.

Validated in both directions on a server configured like the stateless one,
after asserting that table_engines_require_grant denies a no-grant user and
that the backups disk accepts a backup. Against the released 26.7.6.8 binary
the test fails on attach-file-engine-denied, attach-azure-engine-denied,
restore-file-engine-denied and restore-rejected-not-created, the same four
lines it inverted against pre-fix master; against the fixed build it passes
with no diff. The bypass itself reproduces on 26.7.6.8: ATTACH of a
URL('file://...') table is accepted and the following SELECT returns the file
contents, while both the CREATE and a direct File engine ATTACH are denied.

The four older branches are unaffected and need no backport: URL scheme
dispatch does not exist there, so a URL('file://...') read fails with
Code: 36, Unknown protocol in the URL.
@PedroTadim

Copy link
Copy Markdown
Member

Superseded by #115966, which is merged into 26.7: src/Storages/StorageURL.cpp there now carries the from_existing_metadata guard, and both 04401_url_engine_s3_dispatch_engine_grant.{sh,reference} are in place.

This robot cherry-pick is CONFLICTING only in the two test files, because master renamed 04401_url_engine_s3_dispatch.sh to 04401_url_engine_s3_dispatch_engine_grant.sh, which reaches 26.7 as a modify/delete. StorageURL.cpp itself merges cleanly. #115966 resolved that by hand.

Closing.

@PedroTadim PedroTadim closed this Aug 24, 2026
@maxknv
maxknv deleted the cherrypick/26.7/115914 branch November 13, 2026 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants