Skip to content

Cherry pick #119848 to 26.3: Do not skip access checks of a user query executed as a nested query - #120618

Merged
Manerone merged 12 commits into
backport/26.3/119848from
cherrypick/26.3/119848
Sep 18, 2026
Merged

Manerone merged 12 commits into
backport/26.3/119848from
cherrypick/26.3/119848

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #119848

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 11 commits September 13, 2026 20:02
`InterpreterParallelWithQuery::executeSubquery`,
`InterpreterExecuteAsQuery::execute` and the local execution path of the
`QueryRunner` table engine re-execute a user-supplied query with
`QueryFlags{ .internal = true }`. That flag reaches `InterpreterCreateQuery`,
whose `getRequiredAccess` returns an empty set of required privileges for
internal queries, so no authorization check ran for `CREATE` subqueries. An empty
access set also means `checkAccessImpl` is never reached, which defeats the
`readonly` and `allow_ddl` settings on the same path.

A user holding only `GRANT SELECT` on a single database was denied a direct
`CREATE TABLE`, but the same statement wrapped in `PARALLEL WITH` succeeded.
`EXECUTE AS` was affected the same way, which inverts its contract: the
impersonated statement was checked against the privileges of neither party. For a
`QueryRunner` table the queued query text comes from whoever inserted it, so a
user with only `INSERT` on such a table could queue a `CREATE` and have it
executed regardless of the privileges of the principal the job runs as.

The flag cannot simply be cleared for these subqueries: `internal` also keeps a
nested query out of the `ProcessList` and deliberately leaves it without a
`SpanHolder` so that the spans already on the stack finish in the correct order,
`EXECUTE AS` relies on it to get raw blocks from the inner `executeQuery`, and it
is what marks `QueryRunner` jobs with `is_internal = 1` in `system.query_log`.
So the queries stay `internal`, and the authorization decision stops depending on
that flag: the new `QueryFlags::user_initiated` marks a query whose text was
written by the user, and `InterpreterCreateQuery` is told a query is
server-initiated only when `internal` is set and `user_initiated` is not.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…tem.query_log`

The `INSERT` into a `QueryRunner` table succeeds no matter how the job ends, and the
absence of the database would also be observed if the job never ran at all, so the
test now reads the job's log entry - an `ACCESS_DENIED` exception before the grant,
`QueryFinish` after it - instead of discarding the client output.

Also cover two layers of nesting: a job that queues another job into a second
`QueryRunner` table, and `PARALLEL WITH` inside `EXECUTE AS`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…g by it

The style check asks every query of `system.query_log` to be constrained to the
database of the test. A job of a `QueryRunner` table runs with the database its
row names, so both tables now carry a `database` column and every job names the
test database - which is what puts it in `current_database`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…a `Replicated` database

The `05214` test read `system.query_log` rows filtered by names derived from the test database
only, so a second run against the same database would print the jobs of the first run again, and
its `QueryRunner` tables were never dropped. The user and the database now carry a random suffix,
and the tables are dropped before and after the run: the test passes repeatedly in one database.

The new `05215` test runs `PARALLEL WITH CREATE TABLE` in a `Replicated` database: the subquery is
denied without `CREATE TABLE` and succeeds with it. The replay through the replicated DDL log is a
separate server-side query which is non-internal for a direct `CREATE` as well, so a user-authored
subquery is now treated exactly like the direct statement on that path.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The negative assertions of the two tests counted every table in the test database, and the success
paths left their fixed-name tables behind, so a rerun with `clickhouse-test --database=X` printed the
leftovers and failed with `TABLE_ALREADY_EXISTS`. Now only the objects each test creates are counted,
and they are dropped both before and after the run, as `05214` already does. Verified locally with two
consecutive runs of all three tests on one fixed database.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Address review: the test assumed it owned `rdb_${CLICKHOUSE_DATABASE}` and a fixed
ZooKeeper path, so a previous run that died after `CREATE DATABASE` (or after creating
`t_parallel_1` / `t_parallel_2`) made the next run on the same server fail at setup.

Now the user, the database and its ZooKeeper path carry a per-run random suffix, the
database is dropped with `IF EXISTS ... SYNC` up front and at the end, and only the
tables this test creates are counted and listed, as was already done for `05212`-`05214`.

Verified: two consecutive runs on one fixed database pass.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…lickHouse` dictionary source

For a local source, `ClickHouseDictionarySource::doInvalidateQuery` executed the `invalidate_query`
text as an `internal` query on behalf of the configured user, without the `SELECT`-only check that
`createStreamForQuery` applies to the main query. The text comes from `CREATE DICTIONARY`, so a user
who may create a dictionary but has no `CREATE TABLE` privilege could run `CREATE TABLE ...` through
it with the access checks of `internal` queries skipped - the same bypass this PR closes for
`PARALLEL WITH`, `EXECUTE AS` and `QueryRunner`.

The check is now shared by both paths (`checkQueryIsSelect`), and a non-`SELECT` invalidate query is
rejected with `INCORRECT_QUERY` before anything is executed. Test `05218` creates such a dictionary as
a restricted user, waits for the periodic invalidate check, and asserts that nothing was created and
that a `SELECT` invalidate query keeps working.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…d-access-checks

Picks up the MinIO image mirror from #119999, which fixes the integration test
jobs that failed to pre-pull `minio/mc` after the upstream Docker Hub
repository was deleted.

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

Refresh the branch against master to get a new CI run; the previous run's only
failure was `Build profile diff`, which errored on a CI logs cluster query
rather than on anything in this branch.

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

Do not skip access checks of a user query executed as a nested query
@robot-ch-test-poll2 robot-ch-test-poll2 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 pr-critical-bugfix labels Sep 17, 2026
The `QueryRunner` table engine does not exist in 26.3 - it was introduced by
`7f8391e4636` after the 26.3 branch point - so the change to
`src/Storages/StorageQueryRunner.cpp` has nothing to apply to and the file stays
deleted. Its test `05214_query_runner_access_checks` is dropped for the same
reason: it creates a table with `ENGINE = QueryRunner`, which 26.3 cannot
resolve.

The rest of the fix applies unchanged. `PARALLEL WITH`, `EXECUTE AS` and the
`invalidate_query` of a `ClickHouse` dictionary source all exist in 26.3 and
keep their access checks.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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