Skip to content

Match filtered source grants only against the location that is actually accessed - #129899

Merged
pufit merged 8 commits into
ClickHouse:masterfrom
groeneai:fix-source-grant-filter-accessed-location
Nov 7, 2026
Merged

pufit merged 8 commits into
ClickHouse:masterfrom
groeneai:fix-source-grant-filter-accessed-location

Conversation

@groeneai

@groeneai groeneai commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #88089
Related: #114405
Related: #129605

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

Fixed a filtered source grant (e.g. GRANT WRITE ON S3('https://bucket/allowed/.*')) authorizing a location outside the filter. BACKUP/RESTORE and table functions like s3, url and azureBlobStorage with ./.. or empty path segments, a fragment, escapes that change the name, a query in an S3 URI (where ? is a wildcard) or url alternatives outside the path now need the grant on the whole source. A glob character (*?{}|, and , in url) must match the filter both as written and percent-encoded.

Description

On 26.10.2.103, a user with WRITE ON AZURE('db/backups/.*') can BACKUP ... TO AzureBlobStorage(..., 'db/other/../backups/x', ...) and Azurite stores that literal blob; with READ ON S3('.../test/exact'), s3('.../test/exact?') reads test/exactX. An S3 backup to .../denied/../allowed/x.tar passes an allowed/.* filter and sends the literal key (MinIO answers HTTP 400).

Root cause: filtered source checks match the filter against normalizeURIForAccessCheck(uri) (Poco resolves dot and empty segments, decodes and re-escapes the path, lower-cases a scheme-like prefix, keeps a fragment), but storages do not: S3 keeps dot and empty segments in the key and folds a query into it, url sends the raw path and expands {}/| across the whole URI, Azure and local names are used as written.

Change: ITableFunction::checkSourceAccessForURI, now used by table functions and BackupFactory, matches the filter only when the normalized string is the accessed location, else only the whole-source grant passes. Azure and File names are matched as written, an Azure archive as its written key. A glob character must match both ways, so neither a filter spelled %2A nor the . of .../report.csv authorizes report*csv. As on master, a glob is matched as a pattern, not per expanded key.

Behaviour change, filtered grants only: a location with a ./.. segment (including glob alternatives), //, a # fragment, {}| outside the path, a non-glob url path character the check would escape (such as %, !, \) or a ? in an S3 URI (also versionId) needs the unfiltered grant, and a filter spelled %2A/%7B (or %2C in url) no longer authorizes the raw character. Workaround: write the location plainly, or grant the whole source.

Validation: new test 05564_source_grant_dot_segments fails on master and passes with the fix; each rule has a case that fails without it. 03636, 03595, 04774, 04776 and test_backup_source_grants pass.


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

Version info

  • Merged into: 26.11.1.1631-master (included in 26.11 and later)
  • Backported to: 26.10.7.26, 26.9.33.13, 26.8.41.15

groeneai and others added 8 commits November 6, 2026 21:22
A filtered SOURCES grant was matched against `normalizeURIForAccessCheck(uri)`:
Poco resolves `.`/`..`, drops empty segments, percent-decodes the path and
lower-cases a scheme-like prefix. No storage repeats that: `S3::URI` keeps dot
and empty segments in the key and folds a query into it (where `?` is a
wildcard), `url()` sends the raw path and expands `{a,b}` after the check, and
Azure blob names and local paths are used as written. So e.g.
`BACKUP ... TO S3('.../denied/../allowed/x.tar')` passed an `allowed/.*` filter
and AWS S3 stored the literal key, and an Azure backup to `db/other/../backups/x`
or `db%2Fbackups/y` was written outside `WRITE ON AZURE('db/backups/.*')`.

`ITableFunction::checkSourceAccessForURI` is now the single check for table
functions and `BackupFactory` (its copy `normalizeAccessURI` is removed): the
filter is matched only when the normalized string is the accessed location,
otherwise the subject is empty and only the unfiltered grant passes. Azure and
File names are matched as written; an Azure archive destination is authorized
as the post-split key the writer opens, so `blob_path_with_archive` is gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t filters

Poco's toString percent-encodes a raw `*`, `?`, `{`, `}`, `|` of the path exactly as it
encodes their escaped spelling, while S3 and `url()` expand both as a glob after the check.
So a filter written against the escaped form (`.../a%2A.tsv`) authorized the glob `a*.tsv`.
The subject now spells these characters as written: `toString` encodes a `%` of the path as
`%25`, so before the query such an escape can only come from a glob character. This replaces
the refusal of escaped S3 wildcards, which closed nothing for the raw spelling; an escaped
wildcard now gets the same subject as the raw one.

A `#fragment` is kept in the subject but never accessed (the S3 key and the HTTP request use
the path and query only), so a URI with `#` now needs the grant on the whole source.

The test gains the escaped-filter and fragment cases and allows under a filter for a glob,
glob alternatives, a local path and an Azure archive destination.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d escaped

Spelling a glob character as written in the subject (previous commit) let the `.` of an
ordinary exact-object filter match it: `READ ON S3('.../data/report.csv')` authorized
`s3('.../data/report*csv')`, which S3 expands to `report[^/]*csv` and uses to read keys the
filter does not match. Master's subject spelled `*` as `%2A`, which a single `.` cannot
match, so this was wider than master. The same held for an S3 `%3F`, HDFS `*`, a `url()`
top-level `|` failover and the raw Azure and `File` names.

A glob is expanded after the check, so neither spelling is the accessed location. The check
now requires the filter to match the location both as written and with glob characters
percent-encoded, which authorizes nothing that master or the as-written subject alone denied.

The test gains exact-object grants and five cases: the exact read is allowed, while `*`,
`%3F`, `url()` failover and an Azure `*` against the exact filter are refused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…urce

`url()` expands `{}` alternatives and splits `|` failover across the whole URI, but the
access-check subject escapes glob characters only in the path: Poco appends the authority
and the query as written. A `|` or `{}` in the query therefore stayed a single character
that the `.*` of a prefix filter matches, so `READ ON URL('http://h/allowed/.*')`
authorized `url('http://h/allowed/missing?x|http://other/secret')`, which fails over to the
second URL and reads it. Such a location now gets the empty subject, so only the grant on
the whole source passes.

The test gains a failover and an alternative in the query, and is tagged
`no-replicated-database`: replicated access storage drops grant filters when
`enable_read_write_grants` is off, which makes every filtered grant whole-source.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Azure blob paths and local paths are matched against a source grant filter as written,
but a local path is opened as written and the file system resolves `//` as `/`, while
master's subject dropped empty segments. So under a segment-shaped filter such as
`READ ON FILE('/ufp/db/.*/public/.*')`, `icebergLocal('/ufp/db//public/t')` was
authorized although it opens `/ufp/db/public/t`, which the filter refuses. Such a name
now gets the empty subject, the same rule the URI sources already apply to `//`, so only
the grant on the whole source passes. For an Azure flat-namespace blob this refuses a
literal `//` as well, which keeps one rule for both names.

The test gains a local path and an Azure backup destination with `//`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… escapes

`url` sends its path as written (`enable_url_encoding` is 0 by default), but the
subject the filter is matched against is Poco's `toString`, which escapes every path
character other than alphanumerics and `-._~/&+;=:@`. So under
`READ ON URL('http://h/data/a%21b')`, `url('http://h/data/a!b')` was authorized and sent
`/data/a!b`, and under `READ ON URL('http://h/allowed/.*')`, `url('http://h/allowed/..\secret')`
was authorized with the subject `.../allowed/..%5Csecret`, which a server that treats `\`
as `/` resolves to `/secret`. A `url` path may now hold only the characters the subject
keeps plus the glob syntax `*{}|,`, which the as-written and escaped witnesses already
handle; anything else gives the empty subject, so only the grant on the whole source
passes. This subsumes the `%` rule for `url`; other non-S3 URI sources keep it.

The test gains a raw `!` against a filter that spells it `%21`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ritten

The `url` rule allows `,` in a raw path (glob syntax inside `{a,b}`), but the escaped
witness renders it `%2C` and `unescapeGlobCharacters` restored only `*?{}|`, so for
`url('http://h/data/a,b')` both witnesses were `.../a%2Cb`, while `url` sends `/data/a,b`
as written. So `READ ON URL('http://h/data/a%2Cb')` authorized the raw `a,b`, and a filter
excluding commas (`.../allowed/[^,]*`) authorized `.../allowed/a,b`. For `url` the
as-written witness now also turns `%2C` back into `,`: a raw `%` is already refused for
`url`, so before the query a `%2C` can only come from a raw `,`, and the witness equals the
raw path. The escaped witness is still required, so nothing master denied is allowed. S3
decodes the key, so its `%2C` subject already is the accessed key and stays as it is.

The test gains a raw `,` against a filter that spells it `%2C`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense four multi-line comments in ITableFunction.{h,cpp} to one line each and drop the comment on escapeGlobCharacters.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)

Bounce cap reached - published as-is; residual findings listed below (the rows marked "Not changed" or "Noted").

Finding Verdict
❌ Raw glob characters were percent-encoded in the subject, so a filter matching a%2A.tsv authorized the glob a*.tsv Agreed, fixed: the filter must match a glob as written
❌ Matching a glob only as written let the . of an exact filter such as .../report.csv authorize report*csv Agreed, fixed: a glob must match both as written and percent-encoded
❌ A #fragment stayed in the subject but is never accessed Agreed, fixed: a URI with a fragment needs the unfiltered grant
❌ url fails over on a | in the query (.../allowed/x?a|http://other/y), outside the path where glob characters are escaped Agreed, fixed: {}| outside the path need the unfiltered grant
❌ url sends its path as written, but the check matched the escaped path, so a filter on a%21b authorized a!b and allowed/.* authorized allowed/..\secret Agreed, fixed: a non-glob url path character the check would escape needs the unfiltered grant
❌ A raw , in a url path was still matched as %2C, so a filter on a%2Cb, or one excluding ,, authorized a,b Agreed, fixed: , in a url path must match both as written and percent-encoded, like the other glob characters
⚠️ A repeated / in a local path (.../allowed//t) or blob name was matched as written, though the file system collapses it Agreed, fixed: // needs the unfiltered grant for these names too
⚠️ No case where a filtered FILE path, an Azure archive or a visible glob is still allowed, none with an exact filter against a glob, none with url syntax outside the path Agreed, fixed: cases added
⚠️ No case with a raw url character that the check escapes (!, then ,) Agreed, fixed: cases added
⚠️ The test runs with replicated access storage, which drops grant filters without enable_read_write_grants Agreed, fixed: no-replicated-database, as in 03595/03636
⚠️ Per-partition write destinations and *_create_new_file_on_insert renames are not checked Not changed here: a check at write time, open in #129605
⚠️ Two filters can cover the as-written and the escaped spelling of a glob separately Not changed: never wider than master; a glob is still matched as a pattern, not per expanded key (stated in the description)
⚠️ An empty alternative (.../a/{,}/b) expands to a//b, which a filter like a/[^/]+/b would reject Not changed: a glob is matched as a pattern, as on master (* matching an empty name gives the same key); a prefix filter cannot be left
💡 Use Critical Bug Fix for an RBAC fix Not changed: that category is left to maintainers
💡 A local or blob name with a byte the URI parser rejects (space, non-ASCII) is now matched as written instead of needing the unfiltered grant Noted: the name is accessed as written
💡 ?versionId= / presigned S3 queries, % and other escaped characters in url paths (also with enable_url_encoding = 1) and {} kept inside a query now need the unfiltered grant Noted, fail-closed and listed in the description

@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: 05564_source_grant_dot_segments fails on unpatched master on every run (24 of its cases are not refused).
b Root cause explained? The grant filter is matched against the Poco-normalized URI, while S3, URL, Azure and local paths are accessed as written and globs are expanded after the check, so the authorized string and the accessed location differ.
c Fix matches root cause? Yes: the filter is consulted only when the normalized string is the accessed location, and a glob character must match it both as written and percent-encoded; otherwise only the whole-source grant can authorize, the same fail-closed path an unparsable URI already takes. Nothing master refused is now allowed.
d Test intent preserved / new tests added? New stateless test with 37 cases, 7 of them allows under a filter; each rule has a case that fails when that rule is removed (20 mutants run). 03636 is unchanged and passes.
e Both directions demonstrated? Yes: fails on master, passes with the fix (50/50 randomized runs). Live: under READ ON URL('.../a%21b.tsv'), url('.../a!b.tsv') reads on master and is refused with the fix; the same holds for a%2Cb.tsv and a raw ,.
f Fix is general across code paths? Table functions and backups now share one helper (the duplicated copy in BackupFactory is removed); covers S3 (and the S3-based functions), URL, HDFS, Azure and the local data-lake functions; the Azure archive key is authorized as written.
g Fix generalizes across inputs (params/datatypes/wrappers)? Cases cover .., %2e%2e, //, %2F, upper-cased scheme-like names, ?, %3F, #, raw globs against %2A/%7B and exact-object filters, url failover | and {x,y} in the query, a raw ! or , in a url path, {a,..}, plain and archive destinations, READ and WRITE, filtered and whole-source grants.
h Backward compatible? (maintainer-approved exception only) Unfiltered grants are unchanged, and a plain location (nothing ambiguous, no glob character) gets the decision it got before. A filtered grant no longer authorizes the ambiguous locations listed in the PR body, and nothing it refused before is now allowed; workaround in the body.
i Invariants and contracts preserved? One throwing checkAccessWithFilter call per location, the unfiltered grant is still checked first, the refusal message is unchanged, unparsable locations still need the whole-source grant.

Session id: cron:clickhouse-impl-slot-4:20261107-064600

@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

cc @pufit @pamarcos, could you review this and add the can be tested label so CI runs? Source grant filters were matched against the Poco-normalized URI while S3, url, Azure and local storages access the location as written, so .../denied/../allowed/x.tar passed an allowed/.* filter; the filter is now used only when the normalized string is the accessed location, otherwise the whole-source grant is required.

@clickhouse-gh

clickhouse-gh Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3953cf5]

Summary: ✅


AI Review

Summary

PR #129899 centralizes filtered source-grant authorization for table functions and backups, rejects ambiguous locations, and checks both spellings of glob characters. Reviewed head 3953cf583015cdf587ad7707bf422204b82d368f, the full diff, surrounding callers, and all prior discussion. No actionable findings.

Missing context / blind spots
  • ⚠️ No local ClickHouse build or binary was available, so I did not independently run the regression test. The author reports failing-master/passing-fix validation. The fetched CI report reports dropped bugfix validation jobs; a completed run of 05564_source_grant_dot_segments would close the CI verification gap.
Final Verdict

✅ Approve. No new inline findings; CI validation remains outstanding.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. labels Nov 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3953cf583 with master 5581a35f4 (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Object file sizes

6 object files changed (-7.19 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Backups/BackupFactory.cpp.o 112.31 KiB 86.39 KiB -25.92 KiB (-23.08%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

@pufit pufit self-assigned this Nov 7, 2026
@pufit
pufit added this pull request to the merge queue Nov 7, 2026
Merged via the queue into ClickHouse:master with commit 5bc97f0 Nov 7, 2026
373 of 375 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 7, 2026
alexey-milovidov added a commit that referenced this pull request Nov 8, 2026
…rgeTreeParts`

Since #129899, a filtered
`AZURE` grant is matched against a blob name as written, and a subject
containing `//` is refused, so the `<storage_account_url>/<container>/`
subject of `mergeTreeParts` could never match a filter and
`05270_merge_tree_parts_azure_source_grants` failed after merging `master`.
A blob name filter cannot describe a disk that reads any blob of its
container, so an Azure disk now needs the unfiltered grant, as a local disk
needs the unfiltered `READ ON FILE`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@foadl foadl added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 18, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 18, 2026
foadl added a commit that referenced this pull request Nov 19, 2026
foadl added a commit that referenced this pull request Nov 19, 2026
foadl added a commit that referenced this pull request Nov 19, 2026
foadl added a commit that referenced this pull request Nov 19, 2026
26.3 does not check backup destinations against source grants, so the backup part and its test cases are left out.

Co-Authored-By:
Claude Opus 5.5 <[email protected]>
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 19, 2026
Cherry pick #129899 to 26.3: Match filtered source grants only against the location that is actually accessed
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 19, 2026
Cherry pick #129899 to 26.8: Match filtered source grants only against the location that is actually accessed
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 19, 2026
Cherry pick #129899 to 26.9: Match filtered source grants only against the location that is actually accessed
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 19, 2026
Cherry pick #129899 to 26.10: Match filtered source grants only against the location that is actually accessed
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 19, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 19, 2026
Backport #129899 to 26.10: Match filtered source grants only against the location that is actually accessed
clickhouse-gh Bot added a commit that referenced this pull request Nov 19, 2026
Backport #129899 to 26.9: Match filtered source grants only against the location that is actually accessed
clickhouse-gh Bot added a commit that referenced this pull request Nov 19, 2026
Backport #129899 to 26.8: Match filtered source grants only against the location that is actually accessed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants