Skip to content

Add an opt-in access control improvement so that readonly restricts SET ROLE - #118021

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:groeneai/115691-readonly-gate-set-role
Oct 12, 2026
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:groeneai/115691-readonly-gate-set-role

Conversation

@groeneai

@groeneai groeneai commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #115691
Related: #115697
Related: #115551

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Added a server setting access_control_improvements.readonly_restricts_set_role, false by default. When enabled, SET ROLE is refused with error code READONLY in a session or query whose readonly setting is not zero. Previously SET ROLE was the only identity-changing statement that readonly did not refuse.

Description

@ ringerc asked for a concrete draft as decision-making input: #115691 (comment). @ pufit's ruling on whether to do it is still open, so this is not a merge request. @ PedroTadim confirmed the report, not this shape.

readonly refuses ordinary setting changes at 1, and only readonly itself at 2; neither refuses SET ROLE:

$ clickhouse-client --user probe --readonly 1 -q "SET ROLE other_role; SELECT currentRoles()"
['other_role']
$ clickhouse-client --user probe --readonly 1 -q "SET max_threads = 3"
Code: 164. ... Cannot modify 'max_threads' setting in readonly mode.

InterpreterSetRoleQuery::setRole performs no access check, and the readonly gate in ContextAccess::checkAccessImplHelper only fires for a query requesting a not_readonly_flags bit, which SET ROLE never does. SET DEFAULT ROLE and EXECUTE AS request ACCESS_MANAGEMENT flags; I measured both at 164.

The escape stays inside the granted set (SET ROLE <non-granted> still returns 512), so it is confinement escape, not escalation. It matters because a restrictive row policy on the role being left is dropped with it: measured, a query returning [1] under the default role returns [1,2,3] after SET ROLE other_role under readonly = 1.

The check goes in that interpreter, behind the new flag, so nothing changes on upgrade; flipping the default is a separate backward-incompatible decision. The integration test covers both flag states. Two SQL-defined-handler files and one test comment are in the diff only because they assert that readonly cannot block SET ROLE; the fence itself is unchanged.

Not covered: the HTTP role query parameter, clickhouse-local and embedded.xml (not wired, like throw_on_unmatched_row_policies), and the SET profile hole. The role parameter reaches the same escape, measured; the docs now state the setting's scope and a test pins it. This should land before or with a fix for #115551, whose session-start path would reintroduce the bypass.


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

Version info

  • Merged into: 26.10.1.2178-master (included in 26.10 and later)
  • Backported to: 26.9.18.9, 26.8.26.9, 26.7.29.14

…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: ClickHouse#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`.
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 4, 2026
@groeneai

groeneai commented Sep 4, 2026

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

Three rounds of pre-publication review by an independent model, plus my own cold read of the
resulting code in each round: 12 findings, 7 agreed (6 fixed over two rework rounds, 1 recorded as
not blocking) and 5 disagreed with recorded evidence. The statements that change the active roles
were enumerated independently before the plan's list was read; the two matched, eight for eight.

Three deliberate omissions, listed because each is a judgement a maintainer may want to overturn:

  • tests/config/config.d/enable_access_control_improvements.xml is left alone, so the stateless
    suite keeps exercising the shipped default. That file is a curated subset rather than an
    enable-all set: two of the five access_control_improvements keys that default to false are
    already absent from it.
  • programs/local/LocalServer.cpp and programs/server/embedded.xml are left alone, matching
    throw_on_unmatched_row_policies, which appears in neither.
  • The check sits after getSessionContext, so SET ROLE on a context with no session keeps
    returning THERE_IS_NO_SESSION instead of changing to READONLY.

Session id: cron:clickhouse-review-slot-9:20260904-082300

@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [da1549e]

Summary: ✅


AI Review

Summary

This PR adds the opt-in access_control_improvements.readonly_restricts_set_role server setting, enforces it in InterpreterSetRoleQuery for both query-scoped and session-scoped readonly, and narrows the docs to say explicitly that the HTTP role query parameter stays outside this setting's scope. I did not find any unresolved correctness, compatibility, or safety issues in the current diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 4, 2026
@groeneai

groeneai commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator Author

cc @pufit, could you say whether this should land at all? SET ROLE requests no access flag, so the readonly gate in ContextAccess::checkAccessImplHelper never fires for it; this adds an opt-in access_control_improvements.readonly_restricts_set_role, false by default, checked in InterpreterSetRoleQuery::setRole, so upgrades are unaffected. @ PedroTadim raised the question on #115691 and the reporter asked for a concrete draft to read.

`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.
@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing da1549eb8 with master b13e8f865 (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

85 translation units recompiled, 569 s compile time in total, 85 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 Sep 4, 2026
@ringerc

ringerc commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

@groenai

from the pr draft

The role query parameter is not affected by the
access_control_improvements.readonly_restricts_set_role server setting, which refuses only the SET ROLE statement.

This seems reasonable at face value. The idea is to stop the session swapping roles. Discuss how the user, role and readonly parameters interact and the possible implications of this, and for GET where readonly=2 is set by default. Consider principle of least surprise, and the goal of being able to stop the active user/role from escaping confinement. Discuss how stateful http sessions might be affected.

This restriction is intended to stop a readonly=1 or readonly=2 user escaping their role to a more-privileged role and (once #115551 is fixed) to stop them escaping settings profiles.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Measured on master 26.10.1.1; the HTTP handler and session code are identical to this branch, which only adds the gate inside InterpreterSetRoleQuery::setRole. Fixture: row policy x = 1 on the default role r_locked, r_free also granted, table holds 1,2,3, so [1] is confined and [1,2,3] is escaped. u has readonly = 1 CONST in its profile, u2 has none.

1. How user, role and readonly interact

user/password authenticate first. The role parameter is applied at HTTPHandler.cpp:462-464, which is before setReadOnlyIfHTTPMethodIdempotent (:469) and before the URL settings are constraint-checked and applied (:480-481). The comment at :457 says why: the roles decide which settings profile and constraints apply, so they have to be resolved first.

request readonly in effect currentRoles() rows
POST ?user=u2 0 ['r_locked'] [1]
POST ?user=u2&readonly=1&role=r_free 1 ['r_free'] [1,2,3]
POST ?user=u&role=r_free 1 ['r_free'] [1,2,3]
POST ?user=u&readonly=0 Code: 164 READONLY
POST ?user=u&role=r_ungranted Code: 512 SET_NON_GRANTED_ROLE

The parameter is never an escalation: the role must already be granted. But for the same user, lowering readonly is refused while the role switch is accepted, and it takes effect for a request that then runs at readonly = 1.

2. GET, where the transport applies readonly = 2

All as u2, which has no readonly of its own:

GET result
SELECT getSetting('readonly') 2 (same request over POST: 0)
?role=r_free readonly 2, ['r_free'], [1,2,3]
?query=SET ROLE r_free with session_id accepted, and the role persists into the next request of that session

Two things follow. With the flag on, that last line is refused for every user, since the gate reads exactly the value in the first line (InterpreterSetRoleQuery.cpp:44-45); that one is derived from the measured value plus the gate condition, not measured with the flag on. readonly = 2 there is transport-imposed, not admin-imposed, and the same caller can send the same statement over POST at readonly = 0, so nothing was confined but GET-based SET ROLE stops working. The mirror case is the one you quoted: a user whose profile pins readonly = 1 still switches roles per request through the parameter.

One ordering note if the parameter is to be gated: at :462 the only readonly in effect is the profile-imposed one, i.e. exactly the value a caller cannot lift. PrometheusRequestHandler applies the two in the opposite order (:206, then :208-210), so the same one-line check dropped at both call sites would refuse a GET ?role= on the Prometheus endpoint and accept it on /.

3. Least surprise against the confinement goal

readonly reaches a query from three places, and they are not equally binding:

  • profile or constraint (readonly = 1 CONST): admin-imposed, cannot be lifted (?readonly=0 refused above).
  • session state (SET readonly = 1 under a session_id): persists, and a later SET readonly = 0 in that session is refused Code: 164, so it is sticky. Imposed by whoever composes the URL, which in a middleware deployment is the proxy and not the SQL author.
  • transport (GET): per request, and the same caller can simply POST.

On that reading the statement is the half worth gating, being the only one of the two that changes state outliving the request, and role= is a URL-authoring capability whose holder is the same party that supplied readonly. The exception worth naming is a proxy that pins readonly=1 and forwards unrecognised client query parameters: there role= is reachable by the confined party, and that is the strongest argument for gating it too.

4. Stateful HTTP sessions

SET ROLE writes to the session context (InterpreterSetRoleQuery.cpp:52-54). The parameter writes to the query context, which is a fresh copy of the session context made per request (Session.cpp:732). Measured in one session that has confined itself with SET readonly = 1:

step in session s2 readonly currentRoles() rows
after SET readonly = 1 1 ['r_locked'] [1]
same session, &role=r_free 1 ['r_free'] [1,2,3]
next request, no parameter 1 ['r_locked'] [1]
same session, SET ROLE r_free (accepted on master) 1 ['r_free'] [1,2,3]

So the statement is the persistent escape and the flag closes it, including inside a session already sitting at readonly = 1; the parameter escapes one request and leaves the session's own roles untouched. Sessions are keyed on (user id, session name) (Session.cpp:55), which matches the measurement: the same session_id sent with a different user neither reads nor changes the first session's roles or readonly. For #115551 the same asymmetry decides it, and in the same direction: a session that switched role keeps the new profile for every later request, while a parameter-switched request gives it up when the request ends.

I have not widened the diff for this. The docs on both carriers were narrowed in 6ca3a05, and whether the parameter should also be gated is the RBAC owner's call, together with the still-open question of whether the statement gate lands at all. If that call is to gate it, I will implement it.

@ringerc

ringerc commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

@groeneai That was a huge wall of text that didn't say a lot. Succinctly get to the point in natural language.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Short version.

The two carriers differ in lifetime, not in power: both are confined to roles the user already holds. ?role= writes the per-request copy of the session, so it lasts one request and the session's own roles are unchanged afterwards. SET ROLE writes the session, and the new roles apply to every later request under that session_id. The flag targets the statement because that is the half whose effect outlives the request.

user authenticates and picks the profile that supplies readonly. ?role= is applied next, before the transport sets readonly = 2 for a GET and before URL settings are constraint-checked, deliberately, because roles decide which profile and constraints apply. That ordering is also useful: at that point the only readonly in effect is the profile-imposed one, the value a caller cannot lift, so it is where a check on the parameter belongs if one is wanted.

On least surprise the current keying is uneven in both directions. With the flag on, a GET SET ROLE is refused for every user, not only for a user an admin confined, because the gate reads the effective value and a GET is readonly = 2. I would keep that: a GET carrying session_id that changes the active roles is a side effect surviving a safe method, which this repo already refuses for SQL-defined handlers, and the same caller can still POST. The uncovered direction matters more: a proxy that pins readonly in the URL and forwards unrecognised client parameters leaves role= reachable by the confined party.

Stateful sessions are where the flag earns its keep. A session can confine itself, since SET readonly = 1 sticks and a later SET readonly = 0 is refused, and on master SET ROLE in such a session is accepted and persists. ?role= there escapes for one request and leaves the session's roles intact.

Both calls are the RBAC owner's (@ pufit): whether the statement gate lands at all, and whether role= gets the same check. I will implement either. The measurements behind this are in my previous comment.

@ringerc

ringerc commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

@groeneai

The uncovered direction matters more: a proxy that pins readonly in the URL and forwards unrecognised client parameters leaves role= reachable by the confined party.

That's a "don't do that" situation that can be handled with appropriate documentation.

With the flag on, a GET SET ROLE is refused for every user, not only for a user an admin confined, because the gate reads the effective value and a GET is readonly = 2

This might be a decent argument for restricting SET ROLE only for readonly=1 and permitting it on readonly=2, as there could be legitimate (if niche) uses for a session-bound GET series to explicitly switch role. Other session-state-changing SETs of actual variables are permitted on readonly=2, so if we think of "role" like a "variable" there's some argument for making it changeable in readonly=2.

There will be a gap where it is possible to then possible for a readonly=2 session to SET ROLE to a role that has a readonly=0 enforced by a settings profile, but that's a "don't do that" operator-misconfiguration issue IMO. Document that readonly=2 does NOT prevent escaping to readonly=0 via SET ROLE if the active user has access to roles that explicitly set readonly=0 in a settings profile. This is unlikely to arise in practice anyway as setting readonly=0 in a settings profile makes little sense.

Confining SET ROLE really makes most sense for readonly=1 anyway. NOT confining it for readonly=2 doesn't break the gateway privilege dropping use case this fix is intended for.

Do "you" see strong objections to this?

@groeneai

Copy link
Copy Markdown
Collaborator Author

One objection, yes. All of this is measured on master 26.10.1.1 (618a561).

A correction first, and it helps your proposal: the caveat you want to document is not reachable. SET ROLE never changes the effective readonly. A role's settings profile is applied once, at authentication, from the user's default roles; SET ROLE re-derives access rights and row policies and leaves the session's settings alone. Measured: a user at readonly = 0 who runs SET ROLE r_locked, where that role carries SETTINGS readonly = 1, gets the role active and its row policy applied, and readonly is still 0. The reverse direction behaves the same way. So there is no readonly = 0 escape through SET ROLE to document. I do agree the URL-forwarded-parameter case is a documentation matter.

The objection is about what SET ROLE actually moves. It is not a variable: it moves grants and row policies, which is read authority, and readonly = 2 permits reads. Measured, all at readonly = 2: a proxy opens a session, runs SET ROLE r_locked, then SET readonly = 2, and hands the session id to the client. The client cannot lift the confinement (both SET readonly = 0 and SET readonly = 1 fail with code 164) but it can still run SET ROLE r_free, and the visible row count goes from 1 to 3. That is the privilege-dropping case this fix is for, and a gateway that wants its clients to be able to tune settings has to use readonly = 2, because readonly = 1 forbids setting changes entirely. Narrowing the gate to readonly = 1 leaves that shape unprotected.

That readonly = 2 refuses readonly itself is the part I read the other way: the rule it encodes is not "session state is mutable", it is "session state is mutable except your own authority". Role membership is authority.

Your GET point is still right, though, and I think it is the real defect in the current keying. The fix I would pick is to key the gate on the session's readonly rather than on the effective per-request value. The transport value never reaches the session: measured, a GET inside a session runs at readonly = 2 while the next POST in that same session reports 0. So that keying refuses exactly the values a caller cannot lift (profile-imposed, or a session that confined itself) and leaves an ordinary GET by an unconfined user alone, without reopening the case above.

Both are one-predicate changes and I will push either. Which of them, and whether the statement gate lands at all, is the RBAC owner's call, so I am not changing the diff on my own judgement.

@ringerc

ringerc commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

@groeneai I want to fix the ineffectiveness of SET ROLE for applying profile changes in #115551; I don't think the current state should be relied upon for that.

I'm hesitant about playing games about enforcement based on where a setting came from (GET vs session/profile).

Decent argument re readonly=2, gateways, and wanting to be able to allow other setting twiddling though.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Agreed on both points, and the first one changes my answer rather than yours.

On provenance: dropping that candidate. Keying the gate on where readonly came from is exactly the kind of subtlety that is hard to document and easy to get wrong, so the only two shapes left are the one in the PR (any non-zero effective readonly) and yours (readonly == 1). The cost of mine, stated plainly: an HTTP GET carrying a session_id runs at readonly = 2 by the interface's own convention, so it would refuse SET ROLE. I think that is right on safe-method grounds, and this repo already refuses session-state side effects for SQL-defined handlers, but it is a real behaviour change and your variant does not have it.

On #115551: you are right that I should not lean on today's behaviour, and once you fix it my "the caveat is unreachable" finding inverts into an argument for keeping the gate at any non-zero value. Profile application deliberately skips constraint checking. Context::setUser calls setCurrentProfilesWithLock(*enabled_profiles, /* check_constraints= */ false, lock) under the comment "A profile can specify a value and a readonly constraint for same setting at the same time, so we shouldn't check constraints here" (src/Interpreters/Context.cpp:2232-2234). If the #115551 fix re-applies role profiles through that path on SET ROLE, then SET ROLE becomes a way to write readonly itself without the check that makes SET readonly = 0 fail with code 164. A session confined at readonly = 2 could then lower its own confinement by activating a role it already holds. Gating at any non-zero value closes that before it exists; gating at readonly == 1 opens it, and the caveat you wanted documented stops being theoretical.

The two changes also compose the way step 4 of #115551 wants: a gateway at readonly = 0 activates the per-request role, so the gate does not fire, your fix applies that role's readonly = 1 CONST, and the gate then stops forwarded client SQL from stepping back out of it.

Both variants remain a one-predicate change and I will push whichever the RBAC owner prefers. The diff stays as it is until then.

@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 31 queries analysed

The PR adds the access_control_improvements.readonly_restricts_set_role server setting, which is off by default, and a readonly check that runs only when a SET ROLE statement executes. The rest of the diff is documentation, comments, an error-message rewording for SQL-defined HTTP handlers, and tests, so none of it touches the SELECT execution path the benchmarks use. The only flagged row, clickbench Q28 (+5.4%, 1016 ms to 1071 ms), is already not_sure: the two tests gave mixed signals, CPU time is flat, and the measurements were noisy on both sides. No performance impact can be attributed to this change.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 28 not_sure 1016 1071 +5.4% 0.0041 This query never runs SET ROLE, and the PR only adds a config-gated readonly check inside the SET ROLE interpreter, so it cannot account for the +5.4% shift. CPU time is essentially flat and the measurements on both sides were noisy.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: a3137965-2146-4fea-a3e1-fad00f5c8c77
  • MIRAI run: d8faee57-55e8-424d-8d47-644cc4e9a93d
  • PR check IDs:
    • clickbench_936832_1791757448
    • clickbench_936845_1791757448
    • clickbench_936851_1791757448
    • tpch_adapted_1_official_936858_1791757448
    • tpch_adapted_1_official_936890_1791757448
    • tpch_adapted_1_official_936904_1791757448

@ringerc

ringerc commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Depending on the outcome of #115695 (comment), this might want to be re-visited in favour of a separate enhancement for session-level control over / irrevoccable application of SET ROLE

@groeneai

groeneai commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

Agreed. I'll keep this PR open and unchanged until the direction on #115695 is settled, and if a session-level role lock is chosen there, I'll replace this PR with one for the lock rather than keep both.

@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 12, 2026
Merged via the queue into ClickHouse:master with commit 39e8259 Oct 12, 2026
163 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 12, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 12, 2026
alexey-milovidov pushed a commit that referenced this pull request Oct 13, 2026
Resolve the cherry-pick of #118021 to 26.9.

The conflict in `InterpreterSetRoleQuery.cpp` comes from #118429, which
is not on 26.9 and has no backport label: on master `setRole` also
refuses a context without a user, next to the new `readonly` check.
Only the `readonly` check is added.

The new integration test merges cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 13, 2026
Cherry pick #118021 to 26.9: Add an opt-in access control improvement so that readonly restricts SET ROLE
robot-clickhouse added a commit that referenced this pull request Oct 13, 2026
alexey-milovidov pushed a commit that referenced this pull request Oct 13, 2026
Resolve the cherry-pick of #118021 to 26.8.

The conflict in `InterpreterSetRoleQuery.cpp` comes from #118429, which
is not on 26.8 and has no backport label: on master `setRole` also
refuses a context without a user, next to the new `readonly` check.
Only the `readonly` check is added.

`ParserCreateHandlerQuery.cpp` has no `getDocumentation` on 26.8, so its
documentation change is dropped.

The new integration test merges cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Oct 13, 2026
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 pushed a commit that referenced this pull request Oct 13, 2026
Resolve the cherry-pick of #118021 to 26.3.

The conflict in `InterpreterSetRoleQuery.cpp` comes from #118429, which
is not on 26.3 and has no backport label: on master `setRole` also
refuses a context without a user, next to the new `readonly` check.
Only the `readonly` check is added.

`docs/concepts/features/interfaces/http.mdx` and the SQL-defined handler
files (`SQLDefinedHandlerFromAST.cpp`, `ParserCreateHandlerQuery.cpp`,
`04824_handler_safe_method_session_state.sh`) do not exist on 26.3, so
their comment and documentation changes are dropped. The server setting
row is added to 26.3's `_server_settings_outside_source.md`.

The new integration test merges cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse added a commit that referenced this pull request Oct 13, 2026
Cherry pick #118021 to 26.3: Add an opt-in access control improvement so that readonly restricts SET ROLE
robot-clickhouse added a commit that referenced this pull request Oct 13, 2026
robot-clickhouse added a commit that referenced this pull request Oct 13, 2026
Cherry pick #118021 to 26.7: Add an opt-in access control improvement so that readonly restricts SET ROLE
robot-clickhouse added a commit that referenced this pull request Oct 13, 2026
robot-clickhouse added a commit that referenced this pull request Oct 13, 2026
Cherry pick #118021 to 26.8: Add an opt-in access control improvement so that readonly restricts SET ROLE
robot-clickhouse added a commit that referenced this pull request Oct 13, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 13, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 13, 2026
Backport #118021 to 26.9: Add an opt-in access control improvement so that readonly restricts SET ROLE
clickhouse-gh Bot added a commit that referenced this pull request Oct 13, 2026
Backport #118021 to 26.8: Add an opt-in access control improvement so that readonly restricts SET ROLE
clickhouse-gh Bot added a commit that referenced this pull request Oct 13, 2026
Backport #118021 to 26.7: Add an opt-in access control improvement so that readonly restricts SET ROLE
PedroTadim added a commit that referenced this pull request Oct 13, 2026
Backport #118021 to 26.3: Add an opt-in access control improvement so that readonly restricts SET ROLE
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. groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants