Skip to content

Refuse granting a role in readonly mode, like granting a privilege - #132330

Merged
PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/readonly-role-grant
Nov 18, 2026
Merged

PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/readonly-role-grant

Conversation

@groeneai

@groeneai groeneai commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #132005
Closes: https://github.com/ClickHouse/clickhouse-private/issues/87667

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

GRANT and REVOKE of a role are now refused in readonly mode, like granting a privilege. Previously a read-only session (including any HTTP GET request) of a user holding a role WITH ADMIN OPTION could grant that role to another user or revoke it from one. Closes #132005.

Description

On 26.8.38.9, a readonly = 1 session of a user holding r WITH ADMIN OPTION runs GRANT r TO b and the grant persists, while GRANT SELECT ON system.one TO b in the same session is refused with READONLY. An HTTP GET request (which runs with readonly = 2) does the same.

Root cause: readonly is enforced for grant changes only in the grant-option check of ContextAccess::checkAccessImplHelper, which privilege grants pass through. Role grants are authorized by ContextAccess::checkAdminOption, whose admin-option branch does not look at readonly. With ON CLUSTER, a role-only statement leaves nothing for executeDDLQueryOnCluster to re-check, so it was queued and executed.

Change: ContextAccess::checkAdminOptionImplHelper, which authorizes every role GRANT and REVOKE (locally, and per role before the ON CLUSTER dispatch), now refuses under readonly with the same READONLY error as a privilege grant, next to the authentication_grants guard. A local REVOKE of a privilege and GRANT CURRENT GRANTS are unchanged. Under readonly, a role statement that was already refused may now report READONLY instead of ACCESS_DENIED. The "Permissions for queries" docs now mention roles.

Validation: the new test 05820_readonly_role_grant fails on master (the role rows read OK / 1) and passes with the fix, 50/50 with randomized settings. A readonly 0/1/2 matrix of role and privilege GRANT/REVOKE, local and ON CLUSTER, matches the updated docs, and a refused ON CLUSTER statement leaves nothing in the DDL queue.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.2704-master (included in 26.11 and later)

Under `readonly`, `GRANT SELECT ON system.one TO b` is refused with READONLY,
but `GRANT r TO b` by a user holding `r WITH ADMIN OPTION` was accepted and
persisted (also over HTTP GET, which runs with readonly = 2; reproduced on
26.8.38.9 and master). `readonly` is enforced for grant changes only in the
grant-option check of ContextAccess::checkAccessImplHelper. Role grants are
authorized by ContextAccess::checkAdminOption, whose admin-option branch does
not look at `readonly`, and an ON CLUSTER role-only statement passes an empty
`access_to_check` to executeDDLQueryOnCluster, so role GRANT and role REVOKE
ON CLUSTER were queued and executed unchecked.

InterpreterGrantQuery::execute now states the documented privilege rule for
roles before the ON CLUSTER branch: under readonly, granting a role is refused
(locally and ON CLUSTER) and revoking a role is refused ON CLUSTER, with the
same error as for privileges. A local REVOKE of a role stays allowed, like a
local REVOKE of a privilege. The check is done here and not in
checkAdminOptionImplHelper because that helper serves local REVOKE too.
`full_access` contexts are skipped, as both ContextAccess helpers do.

Docs: "Permissions for queries" now mentions roles.

Closes ClickHouse#132005

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

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

Pre-publication review by an independent model (engine: codex; 0 findings), plus my own cold review (2 nits).

# Sev Finding Verdict Evidence / action
1 💡 Under readonly, a session without the admin option now gets READONLY instead of ACCESS_DENIED for a role GRANT (privileges report ACCESS_DENIED first) DISAGREE Both refuse the statement; role names are resolved before the check, so nothing is disclosed. Stated in the description.
2 💡 permissions-for-queries.mdx still says readonly does not restrict named collection DDL, which #130952 changed DISAGREE Out of scope: that sentence is not touched here.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261117-114200

@mintlify

mintlify Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 4abb20d.

Approve preview

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The issue's SQL as a user holding r WITH ADMIN OPTION with --readonly 1: GRANT r TO b is accepted and persists on master and on 26.8.38.9, every time; refused with the fix.
b Root cause explained? readonly gates grant changes only in the grant-option check of ContextAccess::checkAccessImplHelper. Role grants are authorized by ContextAccess::checkAdminOption, whose admin-option branch skips readonly, and an ON CLUSTER role-only statement leaves access_to_check empty, so nothing re-checks it.
c Fix matches root cause? Yes. InterpreterGrantQuery::execute applies the privilege rule to roles before the ON CLUSTER dispatch: role GRANT refused, role REVOKE refused ON CLUSTER, same READONLY error.
d Test intent preserved / new tests added? New test 05820_readonly_role_grant.sh: privilege control, role GRANT at readonly 1 and 2, ON CLUSTER GRANT/REVOKE, local REVOKE allowed, readonly 0 positive case. No existing test changed.
e Both directions demonstrated? Yes. Master fails the test (role rows OK/1 instead of (READONLY)/0), the fix passes; 50/50 with randomized settings.
f Fix is general across code paths? Measured: WITH ADMIN OPTION, WITH REPLACE OPTION, REVOKE ADMIN OPTION FOR, REVOKE ALL ... ON CLUSTER, HTTP GET, admin-option and ROLE ADMIN holders. ALTER USER ... DEFAULT ROLE, SET DEFAULT ROLE, CREATE USER ... DEFAULT ROLE were already refused. A refused ON CLUSTER statement queues nothing.
g Fix generalizes across inputs (params/datatypes/wrappers)? Axes: readonly 0/1/2 and HTTP GET, local and ON CLUSTER, role and privilege statements, two kinds of principal. No data types involved.
h Backward compatible? (maintainer-approved exception only) Intended change: a read-only session can no longer grant a role or revoke one ON CLUSTER, as for privileges; readonly = 0 unchanged; a local role REVOKE stays allowed. Already-refused role statements under readonly may now report READONLY instead of ACCESS_DENIED.
i Invariants and contracts preserved? The check runs before any access-storage update and before the DDL dispatch, so the error path changes nothing. full_access contexts are skipped, as in both ContextAccess helpers.

Session id: cron:clickhouse-impl-slot-4:20261117-103001

@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4abb20d]

Summary: ✅


AI Review

Summary

The change rejects role GRANT and REVOKE under readonly through ContextAccess::checkAdminOptionImplHelper, including authorization before ON CLUSTER dispatch. It updates the permissions documentation and adds focused regression coverage for refusal and unchanged writable behavior. Reviewed PR head 4abb20dc3d826a0a77e6e8fd9e8293d8505be252 and all prior discussion; no actionable findings remain.

Missing context / blind spots
  • ⚠️ No local ClickHouse binary or build is available, so the regression test was not independently executed. Runtime evidence comes from the author's reported current-head validation; a completed CI run of 05820_readonly_role_grant would independently confirm it.
Final Verdict

Status: ✅ Approve

@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 labels Nov 17, 2026
@PedroTadim PedroTadim self-assigned this Nov 17, 2026
@PedroTadim

Copy link
Copy Markdown
Member

@groeneai any other possible similar issue in readonly mode?

@PedroTadim

Copy link
Copy Markdown
Member

🕵️ @groeneai the GRANT fix looks correct to me: checkAdminOption never looks at readonly, and the check sits before the ON CLUSTER branch, so both paths are covered. One concern about the REVOKE side.

A local role REVOKE still runs under readonly, and the PR pins that in the test and the docs. The test asserts that REVOKE $R FROM $B succeeds with --readonly 1 (OK, then granted → 0), and the docs now say a read-only session can revoke a role it holds WITH ADMIN OPTION. The reason given is "as it is for privileges", but I don't think that holds up:

  • The privilege exemption looks like a side effect of the code rather than a rule. The sentence was added in Docs: fix the "Permissions for queries" page (HTTP readonly default, lightweight UPDATE and DELETE, mutations, RBAC) #118184, which described the implementation ("only the local form is ungated"). The cause is the REVOKE fallback in checkGrantOption: containsWithGrantOption on the raw AccessRights skips ContextAccess, and with it the readonly check.
  • It contradicts isQueryKindRejectedUnderReadonly, which lists Revoke next to Grant as kinds that never run under readonly.
  • readonly exists so that, for example, an HTTP GET cannot change state. With this PR a GET can still remove roles from other users. It can't escalate privileges, but it is a persistent change to access control.

Suggested fix: refuse a local role REVOKE too. The smallest change is to drop !query.cluster.empty() && from the new condition, then update the test reference and the docs. A sturdier option is to put the check in ContextAccess::checkAdminOptionImplHelper, next to the existing authentication_grants guard. It would then cover every caller, and the Cannot change grants in readonly mode. message would stay in one place instead of being copied into the interpreter. The same gap for a local privilege REVOKE could be a follow-up.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Yes. I ran a census on master 9b107508 as a user holding the needed grants, with --readonly 1 unless noted. These are still accepted, and the change persists:

  1. ALTER DATABASE db MODIFY COMMENT '...': accepted at readonly = 1 and 2, ON CLUSTER, over HTTP GET and with allow_ddl = 0. ALTER DATABASE db MODIFY SETTING max_tables = N is accepted too. MODIFY COMMENT is also accepted on 26.8.38.9.
  2. KILL TRANSACTION WHERE ...: accepted, and it rolled back a running transaction of another user.
  3. INSERT INTO FUNCTION file(...) / url(...) at readonly = 2, which is what HTTP GET runs with: creates or overwrites a file in user_files (also over GET, and on 26.8.38.9), or sends the POST. remote() is refused by the remote side.
  4. SYSTEM RELOAD DELTA KERNEL TRACING: there is no access check at all, so a user with no grants changes the server-wide level at readonly = 1. Already reported in SYSTEM RELOAD DELTA KERNEL TRACING missing checkAccess call bypasses readonly mode #103400.

1-3 have the same cause. The readonly gate in ContextAccess::checkAccessImplHelper is a deny-list of access types (not_readonly_flags, and ddl_flags for allow_ddl), and ALTER DATABASE, KILL TRANSACTION and the source WRITE grant are not in it. UNDROP TABLE (#131892), CREATE TEMPORARY VIEW (#132016) and named collection DDL (#130952) were the same gap, closed in the last week. An allow-list of read-only access types would make every new access type fail closed.

Refused, as expected: INSERT, ALTER TABLE, SYSTEM FLUSH LOGS, SET DEFAULT ROLE, EXECUTE AS, CREATE NAMED COLLECTION, INSERT INTO FUNCTION remote(...). Documented or opt-in: SET ROLE (access_control_improvements.readonly_restricts_set_role, #118021), BACKUP, RESTORE at readonly = 2, GRANT CURRENT GRANTS, and temporary tables (one with ENGINE = File writes into user_files at readonly = 2). The docs paragraph still says named collection DDL is not restricted, which is stale since #130952.

I'll send one PR that adds the three missing access types to the gate. If you would rather have the allow-list, I'll do that instead.

@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4abb20dc3 with master ab23bdd4e (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.

Compile time of recompiled translation units

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

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. label Nov 17, 2026
Move the readonly check for role grants from InterpreterGrantQuery into
ContextAccess::checkAdminOptionImplHelper, next to the authentication_grants
guard, so every admin-option check refuses under readonly with the same
"Cannot change grants in readonly mode." error as privilege grants.

This now also covers a local REVOKE of a role (and REVOKE ADMIN OPTION FOR),
which used to be allowed. ON CLUSTER is still covered before dispatch:
checkAdminOptionForExecutingOnCluster checks each role with checkAdminOption,
and REVOKE ALL with ROLE ADMIN, which readonly refuses as well.

A REVOKE of a role the grantee does not have is still a no-op, as for
privileges. A local REVOKE of a privilege and GRANT CURRENT GRANTS are
unchanged.

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

Copy link
Copy Markdown
Collaborator Author

Thanks, done in 4abb20dc. I took the sturdier option: the check now lives in ContextAccess::checkAdminOptionImplHelper, next to the authentication_grants guard, and the check in the interpreter is gone. A local role REVOKE (and REVOKE ADMIN OPTION FOR) is now refused with READONLY too. ON CLUSTER stays covered before the dispatch, because checkAdminOptionForExecutingOnCluster checks each role with checkAdminOption, and REVOKE ALL with ROLE ADMIN. The test, the docs and the description now describe the refusal.

Measured on the fix build: the role matrix (readonly 0/1/2, a holder of the admin option and a ROLE ADMIN holder, local and ON CLUSTER, HTTP GET) refuses every role GRANT and REVOKE under readonly, and readonly = 0 is unchanged. A REVOKE of a role the grantee does not have stays a no-op, as for privileges.

I'll send the local privilege REVOKE as a follow-up, together with GRANT CURRENT GRANTS, the other exception in that docs sentence.

@groeneai

Copy link
Copy Markdown
Collaborator Author

The three items from my census above are fixed in #132424: ALTER DATABASE and KILL TRANSACTION join the readonly deny-list, and INSERT INTO FUNCTION is refused under any readonly before the table function is resolved.

@groeneai

Copy link
Copy Markdown
Collaborator Author

The local privilege REVOKE and GRANT CURRENT GRANTS follow-up is #132450: both are now refused in readonly mode, locally and ON CLUSTER.

@groeneai

Copy link
Copy Markdown
Collaborator Author

One more gap my census above did not list: a SELECT from file() with rename_files_after_processing set renamed the files it read at readonly = 2 and over HTTP GET. It is refused with READONLY in #132517.

@PedroTadim
PedroTadim added this pull request to the merge queue Nov 18, 2026
Merged via the queue into ClickHouse:master with commit df4ca86 Nov 18, 2026
194 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 18, 2026
PedroTadim added a commit to groeneai/ClickHouse that referenced this pull request Nov 18, 2026
…efuse-privilege-revoke-current-grants

Resolve the conflict in the "Permissions for queries" docs with ClickHouse#132330, which refuses granting and
revoking a role in readonly mode. Its sentence listed a local privilege `REVOKE` and
`GRANT CURRENT GRANTS` as exceptions, which this PR removes, so the merged sentence names both
privileges and roles and has no exceptions left. The C++ changes merged without conflicts: the role
check of ClickHouse#132330 in `ContextAccess::checkAdminOptionImplHelper` and `checkCanChangeGrants` here
apply to different statements.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Algunenano pushed a commit to Algunenano/ClickHouse that referenced this pull request Nov 18, 2026
…mode

Under readonly = 1 or 2 (every HTTP GET runs with readonly = 2), a session
of a user holding a privilege WITH GRANT OPTION could still revoke it from
another user with a local REVOKE, or propagate its grants with GRANT CURRENT
GRANTS, and the change persisted. A plain GRANT is refused there, and so is
the same REVOKE ON CLUSTER.

readonly refuses a change of grants only in the grant-option branch of
ContextAccess::checkAccessImplHelper. A privilege REVOKE fails that check
under readonly and then falls back to the "special case for REVOKE" in
checkGrantOption, which narrows the revoke to what the grantees hold and
authorizes it with a raw AccessRights::containsWithGrantOption, so the
readonly branch is never reached. GRANT CURRENT GRANTS clamps the granted
rights to getGrantableRights() instead of checking the grant option (ClickHouse#60304),
so ContextAccess is not consulted at all.

Add ContextAccess::checkCanChangeGrants() (READONLY, the same message as the
grant-option branch, skipped for full_access; next to the existing
session-level guard checkCanAdministerDefaultRoles) and call it once in
InterpreterGrantQuery::execute after the grant-option checks whenever there
is a privilege to grant or, after narrowing, to revoke. A REVOKE of a
privilege no grantee holds stays a no-op and is accepted, as for roles in
ClickHouse#132330. readonly = 0 is unchanged; the containment and clamp semantics are
unchanged.

An element-wise checkGrantOption under readonly was rejected: with partial
revokes it reports a false ACCESS_DENIED for a revoke the user is entitled
to (rows 1-2 of the new test turn ACCESS_DENIED), and it still lets through
the elements that return before the readonly branch (CLUSTER when
on_cluster_queries_require_cluster_grant = false, the temporary database).

Under readonly, GRANT CURRENT GRANTS to a grantee outside the GRANTEES list
now reports READONLY instead of ACCESS_DENIED, the order a plain GRANT has.

The "Permissions for queries" docs listed both statements as exceptions;
they now say GRANT, GRANT CURRENT GRANTS and REVOKE of a privilege are
refused.

Related: ClickHouse#132330

Co-Authored-By: Claude Opus 5.5 <[email protected]>
qoega pushed a commit to qoega/ClickHouse that referenced this pull request Nov 19, 2026
`rename_files_after_processing` renames the files a `SELECT` from `file()`
has read. ClickHouse#117895 authorizes that write by requesting `WRITE` on the `FILE`
source in `StorageFile::read` and on the `fileCluster` initiator, but the
readonly gate in `ContextAccess::checkAccessImplHelper` is a deny-list of
access types that does not contain `WRITE`. So a read-only session
(`readonly = 2`, which every HTTP GET request implies) holding
`WRITE ON FILE` renamed the files it read in `user_files` (measured on
master and on 26.10.2.103).

`WRITE` cannot join the deny-list: it also authorizes `BACKUP` destinations,
which `readonly` is documented not to restrict, and reads from an `S3`
database. So the two identical checks become one helper,
`StorageFile::checkAccessForRenamingFiles`, which keeps the `WRITE` check
and then refuses a read-only session with the gate's own `READONLY` error,
including its HTTP GET hint. The grant check stays first, so a user without
the grant still gets `ACCESS_DENIED`; `full_access` is exempt as in the
gate. `DESCRIBE` builds no reader and stays allowed.

Related: ClickHouse#132330
Related: ClickHouse#117895

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jkartseva pushed a commit to lazerg/ClickHouse that referenced this pull request Nov 19, 2026
…eadonly mode

`readonly` and `allow_ddl` are enforced in `ContextAccess::checkAccessImplHelper`
by a deny-list of requested access types. Three statements that change
persistent state were missing from it:

- `ALTER DATABASE ... MODIFY COMMENT` / `MODIFY SETTING` request the children
  of `ALTER_DATABASE`, which `table_ddl` did not contain, so they ran at
  `readonly = 1`, `readonly = 2`, over HTTP GET and with `allow_ddl = 0`.
  `ALTER_DATABASE` is now part of `table_ddl`, like `ALTER_TABLE` and
  `ALTER_VIEW`.
- `KILL TRANSACTION` requests `KILL_TRANSACTION`, which was not in
  `not_readonly_flags` (`KILL_QUERY` is), so a read-only session could roll
  back another user's transaction.
- `INSERT INTO FUNCTION` never requests `INSERT`; its only gated flag is
  `CREATE_TEMPORARY_TABLE` (forced for inserts by ClickHouse#44290), which `readonly = 2`
  allows on purpose for session temporary tables. So at `readonly = 2`, which is
  what HTTP GET runs with, `file()` wrote and overwrote files and `url()` sent
  the POST. The source `WRITE` flag is not added to the deny-list because it is
  also requested by `BACKUP` destinations (documented as not restricted by
  `readonly`), by `SELECT` from a `DatabaseS3` table and by `file()` reads with
  `rename_files_after_processing`, and a filtered `WRITE` grant does not reach
  the gate at all. Instead `InterpreterInsertQuery::getTable` refuses a table
  function insert under any `readonly`, with the gate's error text, before
  anything is resolved or written. Every table function insert (synchronous,
  asynchronous, parallel replicas) goes through it.

The English documentation of these settings is updated, including two stale
sentences (named collection management and `UNDROP TABLE` have been gated
since ClickHouse#130952 and ClickHouse#131892).

Related: ClickHouse#132330

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-bugfix Pull request with bugfix, not backported by default 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.

GRANT role TO user is accepted in read-only mode while GRANT privilege is refused

3 participants