Skip to content

Cherry pick #116647 to 26.8: Check access to the source table when resolving the structure of mergeTreeCodecBlockCounts - #120038

Merged
PedroTadim merged 24 commits into
backport/26.8/116647from
cherrypick/26.8/116647
Sep 17, 2026
Merged

PedroTadim merged 24 commits into
backport/26.8/116647from
cherrypick/26.8/116647

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #116647

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.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

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

alexey-milovidov and others added 23 commits August 27, 2026 05:28
…eTreeCodecBlockCounts

`mergeTreeCodecBlockCounts` checked the `SELECT` privilege on its source table
only in `StorageMergeTreeCodecBlockCounts::read`, that is, only when the
function was read. Resolving the structure of the function went straight to
`DatabaseCatalog`, so `DESCRIBE mergeTreeCodecBlockCounts(db, t)` succeeded for
a user without any privilege on `db.t`, and the `BAD_ARGUMENTS` message thrown
for a table of another engine revealed that engine.

Run the same check in `getActualTableStructure`, before anything is derived
from the table. The check is now shared with `read` as
`StorageMergeTreeCodecBlockCounts::checkSourceTableAccess`.

The read side had no test for its check either, so the new test covers both
paths, a grant that covers only one column of the table, which is not enough,
and a grant that covers every column of it individually, which is.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`rienath` asked for a test that `DESCRIBE mergeTreeCodecBlockCounts` over a
table of another engine, without the grant, returns `ACCESS_DENIED` rather than
the engine-revealing `BAD_ARGUMENTS`. That is what this change already produces,
because the check runs before the `dynamic_cast`, but nothing pinned it.

The new arm runs `DESCRIBE` over a `Log` table, first without any grant on it,
which gives `ACCESS_DENIED`, then with `SELECT` on it, which gives
`BAD_ARGUMENTS`, so the ordering of the two checks is witnessed rather than
assumed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`getActualTableStructure` and `executeImpl` resolved `source_table_id` through `DatabaseCatalog` before
any check, so a user with no privilege on the source table could tell an existing hidden table from a
missing one by `ACCESS_DENIED` against `UNKNOWN_TABLE`. Both now check `SHOW COLUMNS` on the name first,
the privilege `DESCRIBE` of the source table itself requires, which any grant on the table implies, so
the `SELECT` rule that follows the resolution is unchanged. The test pins that a hidden source and a
missing one answer alike.
Measured against a build of this branch: a user holding `SELECT` on every column of the source table
separately is refused a table-level `SHOW COLUMNS` check, which `DESCRIBE` of the source table requires,
so checking `SHOW COLUMNS` before the resolution would reject exactly the grant this function accepts.
`SHOW TABLES` is implied by a grant on any single column, and it is the privilege that governs whether
the existence of the table may be learned, which is all this tier hides.
The engine of a table is disclosed by `SHOW CREATE TABLE`, which requires `SHOW COLUMNS` on it, not
`SELECT`, as the comment above the arm said. The arm is unaffected: the user it uses holds neither.
The new arms only ran `DESCRIBE`, which exercises `getActualTableStructure`. An ordinary
`SELECT` goes through `TableFunctionMergeTreeCodecBlockCounts::executeImpl`, which carries
its own pre-resolution `SHOW TABLES` check, so a regression there would have kept the test
green while execution became an existence oracle again. The test now runs both paths, and it
is renumbered to `05059` after the merge with master.
…ad, under the reader's context

`CREATE TABLE ... AS mergeTreeCodecBlockCounts(...)` stores the function and materialises it lazily,
under the global context, on the first read of the created table (`ITableFunction::execute` with
`use_global_context`, through `StorageTableFunctionProxy`). The checks in `executeImpl` and in
`getActualTableStructure` therefore ran as nobody for that entry point: a user who may read the created
table but not the source could tell a hidden source (`ACCESS_DENIED` from `read`) from a missing one
(`UNKNOWN_TABLE` from the catalog), and learn its engine (`BAD_ARGUMENTS`) once it was recreated.

`StorageMergeTreeCodecBlockCounts` now holds only the `StorageID` of the source and resolves it in
`read`, under the context of the user who reads, in the order that discloses nothing: `SHOW TABLES` on
the name, then `SELECT` on every column, then the engine check. `executeImpl` no longer consults the
catalog at all; `getActualTableStructure` still resolves the source, so `DESCRIBE` and
`CREATE TABLE ... AS` keep checking under the asking user's context.

The test gains an arm that reads through a table created from the function, over a hidden, a missing,
a recreated non-`MergeTree` and a recreated `MergeTree` source.

Addresses the AI review blocker on #116647

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`assert_cast` asserts the exact dynamic type, so casting the resolved source table to its base
`MergeTreeData` failed for every `MergeTree` table in a debug or sanitizer build, where
`SELECT ... FROM mergeTreeCodecBlockCounts(...)` raised `Bad cast from type DB::StorageMergeTree to
DB::MergeTreeData`. Use `dynamic_cast`, which is the cast to a base class; `resolveSourceTable` has
already rejected a source table that is not a `MergeTree`.

The `assert_cast` is a no-op in a release build, which is why the local run did not catch it.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=116647&sha=8190adbbec84a4506e135677b4a3ce7106e28fd5&name_0=PR&name_1=AST%20fuzzer%20(amd_debug,%20targeted)

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Review feedback: `EXPLAIN QUERY TREE` (and analyzer-backed `EXPLAIN SYNTAX`)
resolve the table function through `QueryAnalyzer::resolveTableFunction`, which
calls `ITableFunction::execute` and stops at `executeImpl`, so neither
`getActualTableStructure` nor `StorageMergeTreeCodecBlockCounts::read` runs and
no source-table check happens there.

That discloses nothing. The structure of this function is a fixed constant and
`executeImpl` deliberately never consults the catalog, so the answer on that
path is the same whether the source table is readable, hidden, or missing.
Pinned by comparing the three outputs, so that a later change which makes the
analysis path depend on the source table breaks the test.

`EXPLAIN PLAN` and `EXPLAIN PIPELINE` do build the read plan and therefore go
through the checks; covered too.

Also fixes a stale comment: the check on the read path is in
`StorageMergeTreeCodecBlockCounts::read`, not in `executeImpl`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`EXPLAIN QUERY TREE` exists only with the analyzer, so the configurations that
run the suite with `enable_analyzer = 0` answered that arm with a rejection
instead of a query tree - and the rejection is not the same for a readable, a
hidden and a missing source table, so the test reported a disclosure that the
statement never made. Pin the setting for that comparison, which is what the
other configurations were exercising all along.

Fixes the `05059_mergeTreeCodecBlockCounts_access` failure in
`Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated, parallel)`.
…ockCounts(...)`

Master (e5ae260) now refuses to persist any `MergeTree` introspection table function through
`canBeUsedToCreateTable`, and pins that in `05138_forbid_mergetree_introspection_table_functions_as_engine`.
The arm of `05059_mergeTreeCodecBlockCounts_access` that created such a table and read it as another user
can no longer run, so it is removed together with its reference lines.

The comment master attached to the veto said the storage holds the source table's storage object; on this
branch `StorageMergeTreeCodecBlockCounts` holds only the source's `StorageID` and resolves it on every read,
so the comment is reworded: the veto is kept for parity with the sibling functions. The comments that
justified the per-read resolution by the persisted-table path are reworded accordingly.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`canBeUsedToCreateTable` refuses `CREATE TABLE ... AS mergeTreeCodecBlockCounts(...)`, but it is checked
only on the outermost table function, so the same function nested in an argument of another one still
reaches a persisted definition: both `remote(..., f(...))` and `ENGINE = Remote(..., f(...))` keep it in
`remote_table_function_ptr`.

That carrier is closed by this change rather than by the veto. For a cluster with a local shard, both
forms resolve the nested function under the creating user's context, through
`getStructureOfRemoteTableInShard` -> `getActualTableStructureWithAccess`, which is the seam the check
was added to. Measured on a build of this branch: with every privilege the carrier itself needs already
granted, and none on the source table, both forms answer `ACCESS_DENIED`; the same shape over
`mergeTreeIndex`, whose check is added in #116793, creates the table.

Four arms, one per form and per privilege tier: `SELECT` when only one column of the source table is
granted, and `SHOW TABLES` when the source table is one the user may not see at all. A fifth arm pins
that no destination table is left behind. The privilege each attempt demanded is reported next to the
error code, so an arm cannot pass on a refusal that came from the carrier instead of the source table.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ts-access-check

Check access to the source table when resolving the structure of `mergeTreeCodecBlockCounts`
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Sep 14, 2026
…ypick/26.8/116647

# Conflicts:
#	src/TableFunctions/TableFunctionMergeTreeCodecBlockCounts.cpp
@PedroTadim
PedroTadim merged commit 7245673 into backport/26.8/116647 Sep 17, 2026
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!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants