Skip to content

Cherry pick #122678 to 26.10: Check access rights before loading a dictionary or reading its definition - #132701

Merged
robot-ch-test-poll merged 31 commits into
backport/26.10/122678from
cherrypick/26.10/122678
Nov 19, 2026
Merged

robot-ch-test-poll merged 31 commits into
backport/26.10/122678from
cherrypick/26.10/122678

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Original pull-request #122678

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

Manerone and others added 30 commits September 28, 2026 13:48
The `dictionary` table function, `dictGet`-family functions and
`naiveBayesClassifier` loaded the dictionary first and checked `dictGet`/`SELECT`
access afterwards, so a query without the required grants still triggered the
load. The injective `dictGet` optimization of `GROUP BY` in the old analyzer
loaded the dictionary without any check.

Add `ExternalDictionariesLoader::getDictionaryID`, which returns the dictionary ID
from the loaded dictionary or from its configuration without loading it, and use
it to check access before the load. The injective `GROUP BY` optimization is
skipped when `dictGet` is not granted; the query then fails at the regular check.
`DESCRIBE TABLE dictionary(...)` now also requires `dictGet` or `SELECT`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A user with `SELECT` on a `Dictionary` table but without `dictGet` or `SELECT`
on the underlying dictionary triggered the dictionary load before getting
`ACCESS_DENIED`. Check access on the dictionary ID obtained without loading.

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

`dictGet`-family functions and `dictGetKeys` read the dictionary structure, and
`naiveBayesClassifier*` read the layout, to infer the result type before any
access check. A user without the `dictGet` grant could learn attribute names,
types and the layout from the result type or the error messages. Check `dictGet`
access before reading the definition.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`CREATE TABLE ... ENGINE = Dictionary(d)` loaded the dictionary to validate the
columns, without checking access to the dictionary. Validate the columns against
the dictionary definition without loading it, and require `dictGet` or `SELECT`
on the dictionary, the same rule as for reading it. The rule is now in
`StorageDictionary::checkDictionaryAccess`, shared by `StorageDictionary::read`,
the `Dictionary` engine and the `dictionary` table function.

Test joins against a `Dictionary` table as a user without access to the
dictionary, too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`CREATE VIEW ... AS SELECT ... JOIN` against a `Dictionary` table only analyzes
the query, so `StorageDictionary::read` is not called, but the direct join
planning of both analyzers still loaded the dictionary without checking access
to it. `StorageDictionary::getDictionary` now takes the query context and checks
access before loading; the unchecked overload is removed. Remove the unused
`trySetStorageInTableJoin`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The direct join planning of the old analyzer loaded the dictionary a second time
through `FunctionDictHelper`, which requires `dictGet`, so a user with only
`SELECT` on the dictionary could not join against a `Dictionary` table, unlike
with the analyzer and when reading the table. Reuse the dictionary returned by
`StorageDictionary::getDictionary`, which accepts `dictGet` or `SELECT`.

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

At `CREATE` time the `Dictionary` engine resolved the dictionary name with the
global context, while reading the table resolves it with the query context, so
an unqualified name could point to different dictionaries, and `CREATE TABLE ...
ENGINE = Dictionary(d)` with a non-default current database failed with
`Dictionary not found`. Use the query context for the access check and the
validation of the columns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…fter the access check

The access check now runs before the dictionary is loaded, so loading it to
validate the columns is fine for a user who may read it. Keep the previous
behavior for such users, including failing early when the dictionary cannot be
loaded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`dictGet*`, `dictGetKeys`, `naiveBayesClassifier*`, the `dictionary` table function and `ENGINE = Dictionary` check access with the ID returned by `ExternalDictionariesLoader::getDictionaryID`, which threw `Dictionary ... not found` for a missing dictionary before the check ran. A user without access could therefore tell a missing dictionary from an existing one. For a missing dictionary, `getDictionaryID` now returns the ID the dictionary would have, so such a user gets `ACCESS_DENIED`, and a user with access still gets the not-found error from the lookup that follows.

#122678 (comment)

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

With the analyzer, `dictGet(d, ...)` and the other `dictGet`-family functions, `dictHas`, the hierarchy functions and `naiveBayesClassifier*` resolve an unquoted dictionary name in `QueryAnalyzer::resolveFunction`, which called `assertDictionaryStructureExists` before the access check in the function itself. A user without `dictGet` got `Dictionary ... not found` for a missing dictionary and `ACCESS_DENIED` for an existing one. Check `dictGet` first. `05292` now covers identifier arguments, and revokes the earlier grant so that its "no grants" cases really have none.

#122678 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The access checks resolved the dictionary name to get the ID to check, and the load or definition read after them resolved the name again. A dictionary renamed in between could be read instead of the one that passed the check. `ExternalDictionariesLoader::resolveDictionary` replaces `getDictionaryID`: it resolves the name once and returns the key the loader stores the dictionary under together with the ID to check, and the load and the definition and layout reads take that result and go by the key. For a dictionary in an `Atomic` database the key is its UUID, which does not change on rename. For an XML dictionary or one in an `Ordinary` database the key is the name, so there the check and the read still go by name.

`FunctionDictHelper` and the `naiveBayesClassifier*` functions no longer remember that access was checked, because later calls resolved the name again without a check. Every call now resolves once and checks; a `REVOKE` during a query takes effect on the next block. `assertDictionaryStructureExists` had no caller left and is removed.

#122678 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolving a dictionary name looks the table up in the database catalog to find the UUID of a DDL dictionary, and this happens before access is checked. For a `PostgreSQL`, `MySQL` or `DataLake` database the lookup queries the remote server, so a user without access got the remote server's error, e.g. a connection failure naming its address, instead of `ACCESS_DENIED`. Only a database with its own tables can contain a DDL dictionary, so the table is looked up only there; for other databases the name stays the key, as it already did for databases without a UUID.

#122678 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
In a `Replicated` database, and with `ON CLUSTER`, `CREATE TABLE` is executed from the DDL log with full access, so the check in `registerStorageDictionary` always passed there: a user without `dictGet` or `SELECT` on the dictionary could create a `Dictionary` table, and creating it loaded the dictionary. `InterpreterCreateQuery` now checks the access to the dictionary while the query still runs as the user, next to `checkExportAccess` and after an engine inherited through `AS` is known.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122678&sha=e7f2f8bb5111a8a8dbfa2d9704862f78557b77e0&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%202%2F2%29
#122678

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The access check for `ENGINE = Dictionary` that runs before a `CREATE` is replicated missed three cases. With `distributed_ddl_entry_format_version` 1 or 2, `ON CLUSTER` sends the query before `createTable` is reached. A materialized view keeps its inner engine in `create.targets`, which the check did not look at. And the check evaluated the dictionary name argument without storing the result, so a replica evaluated the expression again and could get another dictionary. The check now runs on the legacy `ON CLUSTER` path too, for the query and for an engine inherited through `AS`, visits the inner engines of a view, and replaces the argument with the name it checked.

#122678 (comment)
#122678 (comment)
#122678 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The access check for a `Dictionary` table resolved an unqualified name against the current database of the user, but the query that creates the storage and loads the dictionary may run elsewhere: a replay in a `Replicated` database runs in that database, and a replay of `ON CLUSTER` in the default database of the server. The check now resolves the name in the database of the query that loads the dictionary.

`CREATE TABLE ... AS dictionary(...)` with an explicit column list was not checked at all: the table function is executed with the global context, so the check inside it had full access. The same check now covers the table function, with the default database of the server, which is where the global context resolves an unqualified name.

#122678 (comment)
#122678 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A table created `AS view(...)` with an explicit column list does not analyze the query of `view` when it is created: the storage is created on first use with the global context, which has full access. A `dictGet` in that query was therefore evaluated with full access and loaded a dictionary the user creating or reading the table cannot access, before the read failed with `ACCESS_DENIED`. `checkDictionaryEngineAccess` now analyzes the query of `view` with the context of the user before the table is created, as happens without an explicit column list, so the `CREATE` needs access to what the query uses and fails before anything is loaded.

#122678 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`predictXGBoost` checked `dictGet` on the qualified name once, remembered that, and then resolved the name again for the layout, the structure and the load, so a dictionary renamed in between could be read instead of the one that passed the check, and later calls skipped the check. It now resolves the name once with `resolveDictionaryCheckingAccess` and reads the layout, the structure and the dictionary through the result, like `naiveBayesClassifier`. A missing dictionary is checked like an existing one, so a user without access gets `ACCESS_DENIED` for both.

#122678 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A table created `AS view(...)` with a column list creates its storage on first use with the global context, and `TableFunctionView::executeImpl` analyzed the query again there to get its structure, with full access. The check before `CREATE` analyzes the query as the user, but an expression can evaluate differently in the two contexts: with `dictGet(if(currentUser() = 'u', 'db.allowed', 'db.denied'), ...)`, `CREATE` checked `db.allowed` and the first read loaded `db.denied`. `executeImpl` now takes the given columns instead of analyzing the query, so the query is analyzed only by the queries that read the view, in their own context, and `StorageView` converts their result to the columns, as the proxy did before.

`checkDictionaryEngineAccess` also no longer runs in a replay of `ON CLUSTER` or of a `Replicated` database: the replay runs without the user, so the analysis of a `view` there evaluated the query with full access as well, and the query was checked before it was enqueued.

#122678 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ith one lookup

The check before `CREATE` analyzed the query of a table created `AS view(...)` as the user. Since the storage of such a table no longer analyzes the query when it is created, the check is not needed to keep a dictionary from loading: the query is analyzed only by the queries that read the table, in their own context. It also broke a legitimate case: `CREATE TABLE t ON CLUSTER c (x UInt64) AS view(SELECT x FROM t_on_targets)` failed with `UNKNOWN_TABLE` when the table exists only on the other servers. The analysis is removed, so such a `CREATE` again needs no access to what the query reads, as before this PR.

`ExternalDictionariesLoader::resolveDictionary` decided whether the dictionary exists with `has` and then read it with `getLoadResult`. A dictionary removed in between made it throw `Dictionary ... config not found` before the access check, which told a user without access that the name existed. It now decides with the one `getLoadResult`.

#122678 (comment)
#122678 (comment)

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

Master enabled `modernize-use-nodiscard`, so the new `ExternalDictionariesLoader` methods and `StorageDictionary::getDictionary` get `[[nodiscard]]`, and the analyzer's existence check of a dictionary named by an identifier discards the structure explicitly. `assertDictionaryStructureExists`, changed on master, stays removed: its only caller now resolves and checks the dictionary itself. Master no longer allows `CREATE TABLE ... AS loop(...)`, so its cases are removed from `05498`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e-nodiscard`

The clang-tidy build checks the hunks a pull request changes, and master enabled `modernize-use-nodiscard`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A table created `AS view(...)` with a column list builds its storage with the global context, so the query of `view` is analyzed with full access. Not analyzing it when the columns are known also changes how `StorageView` checks the result of a query with a `JOIN` against the columns, so the change and its tests move to a separate pull request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Check access rights before loading a dictionary or reading its definition
@robot-ch-test-poll3 robot-ch-test-poll3 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-bugfix Pull request with bugfix, not backported by default labels Nov 18, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! labels Nov 18, 2026
Resolve the conflicts of the cherry-pick of #122678 to 26.10.

The conflicts come from master-only changes next to the PR's hunks: the `modernize-use-nodiscard` sweep (disabled on 26.10), `getSignatureString` in the naive Bayes functions, and the `SubcolumnCodecs.h` include. They take the 26.10 side; the PR's own changes are kept, including the removal of `assertDictionaryStructureExists`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-ch-test-poll
robot-ch-test-poll merged commit 1b1ebd9 into backport/26.10/122678 Nov 19, 2026
2 checks passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.10/122678 branch November 19, 2026 14:01
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-bugfix Pull request with bugfix, not backported by default 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