Skip to content

Cherry pick #118021 to 26.7: Add an opt-in access control improvement so that readonly restricts SET ROLE - #125310

Merged
robot-clickhouse merged 8 commits into
backport/26.7/118021from
cherrypick/26.7/118021
Oct 13, 2026
Merged

robot-clickhouse merged 8 commits into
backport/26.7/118021from
cherrypick/26.7/118021

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #118021

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

groeneai and others added 7 commits September 1, 2026 01:44
…ET ROLE

`readonly > 0` refuses ordinary setting changes but `SET ROLE` succeeds, so a
caller that hands out a deliberately confined session (gateway, BI proxy, HTTP
`GET`, which implies `readonly = 2`) cannot rely on the session's role set
staying put: the client can run `SET ROLE NONE`, `SET ROLE <other granted role>`
or `SET ROLE DEFAULT`.

Root cause: `InterpreterSetRoleQuery::setRole` performs no access check at all.
The readonly gate lives in `ContextAccess::checkAccessImplHelper` and only fires
when the query requests a bit in `not_readonly_flags`; `SET ROLE` requests no
access flags, so it never reaches that gate. Ordinary `SET` is refused on a
different path, `SettingsConstraints::getChecker`, which is why the two diverge.
`SET ROLE` is the only identity-changing statement that is not refused under
readonly: `SET DEFAULT ROLE` checks `ALTER_USER` and `EXECUTE AS` checks
`IMPERSONATE`, both of which sit under `ACCESS_MANAGEMENT`, which is inside
`not_readonly_flags` (measured: both return code 164 under `--readonly 1` with
the required grant held).

The escape is bounded by the existing grant set, not an escalation beyond it
(`SET ROLE <non-granted>` still returns code 512), and `readonly` is not cleared
afterwards. The consequence is that a restrictive row policy attached to the
role being left is dropped with it. Measured: with SELECT granted via
`other_role` and a restrictive policy `USING x = 1` on `ro_role`, the default
role returns `[1]`, and after `SET ROLE other_role` under `readonly = 1` the
same query returns `[1,2,3]`.

The check goes in the interpreter that performs none, next to where its two
siblings do theirs, behind a new `access_control_improvements` boolean
`readonly_restricts_set_role` that defaults to `false`, so no existing
deployment changes behaviour on upgrade. `readonly` is read from the query
context because the reported repro is the client's `--readonly 1` flag, which
raises it there only; a query context inherits session settings, so a
mid-session `SET readonly = 1` is covered by the same read. The check precedes
the `Kind` branch, so it covers `SET ROLE <role>`, `SET ROLE NONE` and
`SET ROLE DEFAULT`, and it is placed after `getSessionContext` so a `SET ROLE`
over HTTP without a session keeps returning `THERE_IS_NO_SESSION`.

Four statements in the tree asserted unconditionally that `readonly` does not
block `SET ROLE`. One of them is the generator source of the published
`CREATE HANDLER` page, so they are corrected here. The SQL-defined-handler fence
itself is deliberately unchanged: `ASTSetRoleQuery::getQueryKind` and
`ASTSetQuery::getQueryKind` both return `QueryKind::Set`, so at that layer
`SET ROLE` is not distinguishable from plain `SET`, and the check runs at
CREATE/ALTER time with no live `readonly` value.

Related: #115691
The suite passed `readonly` only as a per-query setting, so the session half of
the promise ("a session or query whose `readonly` setting is not zero") was
measured by hand but not guarded. If the interpreter later read a context that
does not inherit session settings, every test would have stayed green while the
headline property was gone. No case asserted that a refusal leaves the active
roles alone either, so a variant that threw after `Context::setCurrentRoles`
would also have passed.

Two cases are added, over an HTTP `session_id` because that is the only way to
read the roles back after a refusal: a single `clickhouse-client` invocation
aborts at the first error and separate invocations do not share a session. Both
requests are POST, which contributes no `readonly` of its own, leaving the
session's own value as the only source. Reverting the interpreter check makes
the new refusal case fail with no error at all; moving the `throw` to after the
role assignment leaves the three original refusal cases green and is caught only
by the roles-unchanged assertion.

`queryKindRequiresMutatingMethod` held a fifth unconditional "readonly does not
block SET ROLE" statement, and is the site a reader consults first because its
predicate is the `QueryKind::Set` arm. The `queryKindHasSideEffectsUnderReadonly`
header said both "which `readonly` blocks only when the setting is enabled" and
"none of which `readonly` blocks"; the latter clause now reads "by default".
Neither predicate changes behaviour: `QueryKind::Set` still cannot distinguish
`SET ROLE` from plain `SET`, so the handler fence stays setting-independent.

The refusal message said "This is enabled by ...", which reads as though
`readonly` mode were what the setting enables.
The `CREATE HANDLER` refusal for queries with session side effects said the
read-only mode of safe HTTP methods "does not prevent these side effects". With
`readonly_restricts_set_role` enabled that is false for a `SET ROLE` handler,
since `InterpreterSetRoleQuery` refuses it. Reword to "cannot be relied on to
prevent these side effects", matching the wording already used elsewhere in the
file, so the sentence holds whether the setting is on or off. The guard itself
is unchanged and stays independent of the setting: it runs at `CREATE`/`ALTER`
time, where no live `readonly` value exists.

In `test_readonly_restricts_set_role`, `http_query_and_get_answer_with_error`
returns `None` for the error on success, so `assert "READONLY" in error` raised
`TypeError` instead of failing as an assertion when the interpreter check
regressed. Guard it with `assert error and ...`.

Correction to the earlier message in this branch: `readonly > 0` refusing
ordinary setting changes holds for `readonly = 1` only. `readonly = 2` permits
changing settings other than `readonly` itself (`SettingsConstraints.cpp`).
Neither mode refuses `SET ROLE`.
`docs/concepts/features/interfaces/http.mdx` calls the `role` query parameter
"the equivalent of executing `SET ROLE` before the statement". With
`access_control_improvements.readonly_restricts_set_role` enabled that
equivalence no longer holds, so the page and the setting's own description now
say which carrier the setting covers.

The parameter itself stays ungated here, and not because its per-request scope
protects anything. Measured on master, whose `HTTPHandler.cpp` role handling is
byte-identical to this branch: a user with a restrictive row policy on its
default role and `readonly = 1 CONST` in its profile reads `[1]` without the
parameter and `[1,2,3]` with `?role=`, so one request is enough to leave an
administrator's confinement. On that same URL `readonly=0` and `max_threads=2`
are refused with `Code: 164 READONLY` while `role=` is accepted.

Gating it is a separate decision rather than a copy of the same check. The
parameter is applied in `HTTPHandler` before `setReadOnlyIfHTTPMethodIdempotent`
and before the URL settings are checked, so a `readonly` read at that point sees
`0` both for a `GET` request that then runs at `readonly = 2` and for
`?readonly=1&role=`, and the ordering there carries an explicit rationale in the
code: the roles decide which settings profile and constraints apply.
`PrometheusRequestHandler` applies the two in the opposite order, so the same
request shape would gate differently on the two interfaces.
`ClickHouseCluster` copies `programs/server/config.xml` into every instance, and this
branch adds `readonly_restricts_set_role` to that file, so `node_off` was running with an
explicit `false` rather than with the element absent. The `getBool` fallback that an
upgraded deployment relies on was therefore not exercised: had it been written `true`,
every off-arm assertion would still have passed. `node_off` now removes the element.

The new HTTP case pins the scope the two documentation pages state. A restriction moved
into `Context::setCurrentRoles` would keep every statement test green while making both
pages false; this case fails in that situation. Its last two requests are the control that
the parameter, not the session, is what changed the roles.
…e-set-role

Add an opt-in access control improvement so that readonly restricts SET ROLE
@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 labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

The conflict in InterpreterSetRoleQuery.cpp comes from #118429, which is not on 26.7 and has no backport label, so the bot's retry will not clear it: on master, setRole also refuses a context without a user, next to the new readonly check. I added only the readonly check; nothing from #118429 is imported.

26.7 has no SQL-defined handlers (CREATE HANDLER), so the comment, error-message and documentation changes to SQLDefinedHandlerFromAST.cpp, ParserCreateHandlerQuery.cpp and 04824_handler_safe_method_session_state.sh are dropped. All other files merge cleanly.

What I checked:

@alexey-milovidov, I have triage but not push on this repository, so I cannot update cherrypick/26.7/118021 myself. Applying it takes two commands:

git fetch https://github.com/groeneai/ClickHouse resolve-26.7-cherrypick-118021
git push origin FETCH_HEAD:cherrypick/26.7/118021

After that the robot merges this PR and opens the 26.7 backport PR itself.

Resolve conflicts for the 26.7 backport of #118021:
- `InterpreterSetRoleQuery.cpp`: keep only the `readonly` check; the
  missing-user check comes from #118429, which is not on 26.7.
- `SQLDefinedHandlerFromAST.cpp`, `ParserCreateHandlerQuery.cpp`,
  `04824_handler_safe_method_session_state.sh`: removed, 26.7 has no
  SQL-defined handlers.

Resolution matches the one proposed by @groeneai in
#125310 (comment)

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

Copy link
Copy Markdown
Member

🕵 Pushed the merge of backport/26.7/118021 into this branch (c5c2e00). Its tree is identical to the resolution @groeneai proposed above: only the readonly check in InterpreterSetRoleQuery.cpp, and the SQL-defined-handler files dropped because 26.7 does not have them. Thanks, @groeneai.

@robot-clickhouse
robot-clickhouse merged commit 35b7b48 into backport/26.7/118021 Oct 13, 2026
1 check passed
@robot-clickhouse
robot-clickhouse deleted the cherrypick/26.7/118021 branch October 13, 2026 00:20
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