Repository navigation
Do not let filtered source grants be bypassed by INSERT INTO FUNCTION destinations or CREATE TABLE ... AS - #129605
Conversation
The SQL query `INSERT INTO FUNCTION ... PARTITION BY` checked the source grant once against
the URI with `{_partition_id}` placeholder still in it without any replacement. The partition
ids come from the inserted data, so a partition id like `../protected/foo` could write outside
the path that a filtered grant such as `WRITE ON URL('^http://host/allowed/.*$')` allows.
Now the table function passes a write access check to its storage, and each partitioned sink
runs it on the destination of every new partition before writing to it. For object storages,
the check uses the key that is actually written: the decoded key, so that a percent-encoded
`{_partition_id}` cannot hide it, and the key after a rename by `s3_create_new_file_on_insert`.
The key is checked before it is looked up, so that an existing object outside the grant is not
disclosed. `hdfs` grants filter only the authority, so its partitions are checked against the
authority, which a partition cannot change.
This covers `url`, `s3`, `azureBlobStorage`, `hdfs` and their `*Cluster` variants. Tables with
these engines are not affected, because creating one requires an unfiltered grant on the source.
The `file` function is not affected either, because its grant has no URI to filter.
|
Workflow [PR], commit [500fd6f] Summary: ✅
AI ReviewSummaryThe PR checks computed destinations of Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block. Minimum actions: fix the remaining authorization gaps or link the issues that explicitly track their accepted deferral, and add focused regression coverage for the AST-cloning fix. The dismissed testing-setting concern remains recorded without reopening its thread. |
There was a problem hiding this comment.
🟡 Changes recommended
Critical authorization gaps permit writes outside filtered grants through bucket overrides, replica forwarding, and renamed destinations, while candidate probing can disclose forbidden object existence.
5 open issues
Authorize the effective bucket for fully qualified partition keys · New Enforce destination grants for unpartitioned renamed writes · New Preserve initiator grant restrictions across distributed inserts · New Expand schema hash before checking filtered grants · New Authorize renamed candidates before probing object existence · New
What changed in this PR
This PR adds per-partition destination checks to ClickHouse table-function writes, addressing cases where data-derived partition IDs can escape paths permitted by filtered WRITE grants.
Changes:
- Passes write-access callbacks from table functions to URL and object-storage partitioned sinks.
- Reconstructs access-check URIs from resolved object keys and rechecks renamed destinations.
- Adds integration and unit coverage for allowed writes, path traversal, encoded placeholders, and key renaming.
| File | Description |
|---|---|
tests/integration/test_storage_url/test.py |
Adds filtered-grant write regression coverage. |
src/TableFunctions/TableFunctionURL.h |
Removes the specialized partition setter. |
src/TableFunctions/TableFunctionURL.cpp |
Forwards partition expressions to delegates during execution. |
src/TableFunctions/TableFunctionObjectStorage.h |
Uses shared partition-expression storage. |
src/TableFunctions/ITableFunction.h |
Centralizes partition-expression handling. |
src/TableFunctions/ITableFunction.cpp |
Attaches partition write-access callbacks. |
src/Storages/StorageURL.cpp |
Checks each partition URL before writing. |
src/Storages/ObjectStorage/tests/gtest_uri_for_access_check.cpp |
Tests access-URI reconstruction. |
src/Storages/ObjectStorage/StorageObjectStorageSink.h |
Adds the partition sink callback parameter. |
src/Storages/ObjectStorage/StorageObjectStorageSink.cpp |
Checks partition keys and renamed destinations. |
src/Storages/ObjectStorage/StorageObjectStorageConfiguration.h |
Declares destination access-URI reconstruction. |
src/Storages/ObjectStorage/StorageObjectStorageConfiguration.cpp |
Reconstructs URIs using resolved write paths. |
src/Storages/ObjectStorage/StorageObjectStorageCluster.cpp |
Forwards callbacks for cluster storage writes. |
src/Storages/ObjectStorage/StorageObjectStorage.h |
Extends the sink factory interface. |
src/Storages/ObjectStorage/StorageObjectStorage.cpp |
Passes callbacks into partitioned sinks. |
src/Storages/ObjectStorage/HDFS/Configuration.h |
Uses authority-only access checks for HDFS. |
src/Storages/IStorage.h |
Stores callbacks with a table-function-only setter. |
Review effort: Balanced
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes463 object files changed (+92.02 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units733 translation units recompiled, 7343 s compile time in total, 733 of them have a recent master baseline. |
…endent of where the sequence number goes Related: #129630
…se bucket switches only on S3
|
@groeneai please review |
groeneai
left a comment
There was a problem hiding this comment.
Reviewed 66ff3d4. The per-partition check refuses the cases in the description. I measured on master caf2f3b with this diff applied (MinIO, Azurite), and on the official 26.10.2.103 for release behaviour. Two gaps: the check will diverge from the template check once #129899 lands (inline below), and the unpartitioned s3_create_new_file_on_insert rename still leaves a filtered grant with #129630 in the tree (my reply in the thread on ITableFunction.cpp:128).
|
The per-destination check works for me on
So "creating a table needs an unfiltered source grant" holds for Suggestion: require for
|
Thank you @pufit . Both are fixed |
The conflicts are with #129605, which checks the written keys of an insert against a filtered `WRITE ON` grant. The check is applied to the starting key of an insert, to the keys `checkAndGetNewFileOnInsertIfNeeded` steps aside into, and also to the numbered keys of this PR: `getNextKeyForSplittingBySize` and `removeStaleSplitObjectsByNumber` check every key before it is looked up, written or removed. The check is captured by value, because the sink calls it for every next object. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ct fix The checks for a `url(...)` nested in a persisted `remote`, `loop`, `view` or `Remote` definition gated only the `CREATE`: the same read runs in a query with the same grants, as the connection user of `remote`. They also refused valid definitions, such as `AS remote(..., sharding_key = rand())` or `AS s3(..., filename = currentDatabase() || ...)`. A table created `AS url(...)` already needs `TABLE ENGINE ON URL` since #129605, so the creation hook of `url` is not needed either: only a user who may create a table with the `URL` engine for any URL can create a table that is read with the grant on the table. Co-Authored-By: Claude <[email protected]>
Adapted to the release: `StorageTableFunctionProxy` has no `hasUnreplicatedTableDataOnDisk` argument, `StorageURL` names the method `http_method`, and `StorageObjectStorage::createSink` writes `paths.back()`, so that path is checked. `05565_azure_structure_inference_does_not_create_container` is not on the release, so it stays deleted. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #129605 to 26.10: Do not let filtered source grants be bypassed by `INSERT INTO FUNCTION` destinations or `CREATE TABLE ... AS`
…sed by `INSERT INTO FUNCTION` destinations or `CREATE TABLE ... AS`
Adapted to the release: `StorageTableFunctionProxy` has no `hasUnreplicatedTableDataOnDisk` argument, `StorageURL` names the method `http_method`, and `StorageObjectStorage::createSink` writes `paths.back()`, so that path is checked. `05565_azure_structure_inference_does_not_create_container` is not on the release, so it stays deleted. The new integration test imports `uuid`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #129605 to 26.9: Do not let filtered source grants be bypassed by `INSERT INTO FUNCTION` destinations or `CREATE TABLE ... AS`
…ed by `INSERT INTO FUNCTION` destinations or `CREATE TABLE ... AS`
Backport #129605 to 26.10: Do not let filtered source grants be bypassed by `INSERT INTO FUNCTION` destinations or `CREATE TABLE ... AS`
Backport #129605 to 26.9: Do not let filtered source grants be bypassed by `INSERT INTO FUNCTION` destinations or `CREATE TABLE ... AS`
Since ClickHouse#129605 a table created as a table function needs `TABLE ENGINE` on the function's engine when that engine has a source, the same grant as `ENGINE = <engine>`. `filesystem` has no registered engine: it declares its source only through `ITableFunction::getSourceAccessObject()` (FILE), so the `StorageFactory` lookup missed and nothing was required. With an explicit column list the structure is not inferred, so the READ check of the inference never ran, and the table function was executed only for writing (`WRITE ON FILE`). `ON CLUSTER` and `Replicated` databases build the table with full access, so there no FILE grant was needed at all. `StorageFilesystem` reads with the global context, so a later `SELECT` checks only the table's grant: a user with `CREATE TABLE` and `WRITE ON FILE` (or nothing, via `ON CLUSTER`) could read every file under `user_files`. `getSourceEngineOfTableFunction` becomes `getRequiredAccessOfTableFunction` and returns the required element: `TABLE ENGINE` on a registered source engine (unchanged), nothing for a registered engine without a source (unchanged), and unfiltered `READ` and `WRITE` on the source a function declares when it has no engine. Those are the grants that imply `TABLE ENGINE` on a source engine, and the element `ATTACH ... FROM '<path>'` already requires. Both call sites, the local/ON CLUSTER/Replicated initiator in `getRequiredAccess` and the inherited function of `CREATE TABLE copy AS src` (also short `ATTACH` and local `RESTORE`), use it. `filesystem` is the only table function that overrides `getSourceAccessObject`. Behaviour change: a short `ATTACH` or local `RESTORE` of a table created as `filesystem(...)` now needs `READ` and `WRITE ON FILE`, as such a `File` table needs `TABLE ENGINE ON File`. Server startup and replicas are not affected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the conflict in src/TableFunctions/ITableFunction.h: 26.9 keeps the protected isClusterFunction() next to the getSourceAccessObject() declaration that #133420 moves to the public section. 26.9 has a third caller of getSourceEngineOfTableFunction, the ON CLUSTER check of a definition inherited by CREATE TABLE copy AS src, added by the 26.9 backport of #129605 (#133148). Switch it to getRequiredAccessOfTableFunction like the other two callers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the conflict in src/TableFunctions/ITableFunction.h: 26.10 keeps the protected isClusterFunction() next to the getSourceAccessObject() declaration that #133420 moves to the public section. 26.10 has a third caller of getSourceEngineOfTableFunction, the ON CLUSTER check of a definition inherited by CREATE TABLE copy AS src, added by the 26.10 backport of #129605 (#133147). Switch it to getRequiredAccessOfTableFunction like the other two callers. Co-Authored-By: Claude Opus 5.5 <[email protected]>


This change checks access rights for each partition written by
INSERT INTO FUNCTION ... PARTITION BYintourl,s3,azureBlobStorage,hdfsand their*Clustervariants. It also makesCREATE TABLE ... ASa source table function, such ass3(...), need theTABLE ENGINEgrant, likeENGINE = S3(...), in addition to the existing source check of the typed URI.Problem
A table function checked its source grant once, against the URI template, before
{_partition_id}was replaced. The partition id comes from the inserted data, so it could take the write outside the path allowed by a filtered grant. For example, a user withGRANT WRITE ON URL('^http://host/allowed/.*$')could run:The template matches the grant, but the second partition is written to
http://host/protected/x.tsv.The same problem occurs for unpartitioned inserts when the setting
s3_create_new_file_on_insert=1and the key is renamed.CREATE TABLE t AS s3(...)creates the same table asCREATE TABLE t ENGINE = S3(...), but only its URI was checked against the source grant, notTABLE ENGINE ON S3. Only unfilteredREAD ON S3andWRITE ON S3giveTABLE ENGINE ON S3, so a user with a filtered grant could create a table thatENGINE = S3would refuse. Writes to a table are not checked against source grants, so it could write outside the filter. The check had to run as the user before the table leaves the user's hands:ON CLUSTERcreates the table on hosts with full access, andCREATE TABLE copy AS srcinherits the table function ofsrcafter the access check of the query.Solution
The table function gives its storage a write access check, and each partitioned sink runs it on the destination of every new partition before writing anything to it. The destination is normalized like the template, so dot segments are resolved before matching. The check runs when a partition is first seen, because partition ids are known only when the data is read: they come from
VALUES, from streamed input or fromINSERT SELECT, so they cannot be checked when the query is parsed.The renamed key of unpartitioned writes is checked too, and the check is
checkSourceAccessForURI, the same one as for the typed URI since #129899.For object storages, the check uses the key that is actually written rather than the URI as typed.
hdfsgrants filter only the authority, so its partitions are checked against the authority, which a partition cannot change.CREATE TABLE ... ASa table function now checksTABLE ENGINE ON <engine>when the engine has a source (S3,URL,AzureBlobStorage,HDFS, ...), the same check asENGINE = <engine>. The write check is set only forINSERT INTO FUNCTION, not for a table created with AS: like a table withENGINE = S3, it is written by users withINSERT onit, and it behaves the same before and after a restart.The grant is part of
getRequiredAccess, which is checked as the user locally and on the initiator before anON CLUSTERquery is sent. A table function inherited byCREATE TABLE copy AS srcis checked aftersetEngine, next to the existing check of an inherited engine, and in the preflight of the legacyON CLUSTERformat. A definition that cannot be a table is refused before the grant is checked, so its error does not depend on the user's grants.Out of scope
RESTORE ... ON CLUSTERof a table createdAS <function>does not checkTABLE ENGINEon the initiator, as on master. Checking it there means resolving the function, which fails when its named collection is restored from the same backup. A localRESTOREis checked.randConstant, can give the check and the created table different engines. This is not a regression: master does not checkTABLE ENGINE for CREATE TABLE ... ASat all.Corner cases
allowed/%7B_partition_id%7D.tsv, is decoded into the written key, so the key, not the typed URI, is checked. A%that the partition id puts into the key is a literal character, so%61is not checked asa.s3_create_new_file_on_insert, an existing key is renamed by inserting a number before the first dot of the key, or of the file name after Put thecreate_new_file_on_insertsequence number into the file name, not a directory #129630, and the new key can be outside the grant. The renamed key is checked again. The original key is checked before it is looked up, so that the error for an existing key does not disclose objects outside the grant.{_schema_hash}is expanded in the written key, so the check expands it in the template URI too, and the combination withPARTITION BYkeeps working with filtered grants.s3key can make the key refer to another bucket, e.g.s3://other-bucket/x.tsv. Such partition keys are refused, because the checked URI would be in the granted bucket.ON CLUSTERwith explicit columns, and withdistributed_ddl_entry_format_versionbelow 3, is checked on the initiator, which does not otherwise resolve the function.url('s3://...')takes the engine of its scheme, so it needsTABLE ENGINE ON S3, notON URL.Not affected
S3,URLand similar engines: creating one needs an unfiltered source grant, and inserting into such a table is not checked against the source grants of the user. This now also holds for tables created withCREATE TABLE ... AS s3(...).filetable function: without a URI, only an unfiltered grant allows it, and that grant covers every path. Its writes stay limited touser_files_path.Tests
Dot segments are resolved by a real HTTP server (like Nginx) and by S3, so an integration test covers
urlands3. It checks that:s3_create_new_file_on_insertis refused, with the number in either place;CREATE TABLE ... AS s3(...)accepts inserts from a user with onlyINSERT onit;CREATE TABLE ... AS s3(...)by a user with only a filtered grant is refused.AS url(...)or copy one withCREATE TABLE copy AS src, locally,ON CLUSTERand with entry format 2, or restore one locally, and that each is allowed withTABLE ENGINE ON URL.Unit tests check how the checked URI is built from the written key for
s3(path-style, virtual-hosted, encoded placeholder, renamed key, literal and lone%, encoded dot segments,{_schema_hash}) and forazureBlobStorage(blob paths with escapes). Thehdfsoverride is verified only by reading the code.Backport: this fix needs a backport. The refusal of partition keys that refer to another bucket applies to 26.9 and later only and older releases do not resolve such keys to another bucket, so the backports leave it out. The
TABLE ENGINEcheck forCREATE TABLE ... ASgoes intogetRequiredAccessand aftersetEngine, which all release branches have. 26.3 and 26.6 do not check an inherited engine in the legacyON CLUSTERpreflight, so that part is left out there, as forENGINE = .....Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed filtered
WRITEgrants (such asGRANT WRITE ON S3('<regex>')) being bypassed byINSERT INTO FUNCTIONintourl,s3,azureBlobStorageandhdfs. Previously only the URI as typed was checked, so aPARTITION BYvalue like../other/x, or a file renamed bys3_create_new_file_on_insert, could be written outside the granted path. Now every destination is checked. If a renamed file falls outside a filtered grant, widen the grant or uses3_truncate_on_insert.CREATE TABLE ... AS s3(...)and other source table functions now needTABLE ENGINE ON <engine>grant, likeENGINE = <engine>; with only a filtered source grant, grant the engine explicitly. This also applies toCREATE TABLE copy AS srcwhensrcis such a table, and to localRESTOREof such a table.Workflow [PR]
Sync PR [sync-upstream/pr/129605]
Version info
26.11.1.2555-master(included in26.11and later)26.10.8.35,26.9.34.20