Skip to content

Do not let SET <name> = DEFAULT bypass the settings constraints - #115334

Merged
alexey-milovidov merged 5 commits into
masterfrom
set-default-bypasses-settings-constraints
Aug 22, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
set-default-bypasses-settings-constraints

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 18, 2026 •

Copy link
Copy Markdown
Member

SET <name> = DEFAULT resets a setting rather than assigning to it, so it does not produce a SettingChange and never reached SettingsConstraints. Any constraint could therefore be escaped by resetting the setting instead of assigning to it:

CREATE SETTINGS PROFILE p SETTINGS max_query_size = 1000 MAX 1000;
SET max_query_size = 2000;      -- SETTING_CONSTRAINT_VIOLATION, as expected
SET max_query_size = DEFAULT;   -- silently restored the default of 262144

A CONST constraint was just as escapable, and so was readonly mode, which is enforced by the same check:

SET readonly = 2;
SET readonly = 0;               -- READONLY, as expected
SET readonly = DEFAULT;         -- silently left readonly mode

This describes the reset as the change it effectively is — to the value declared in the source code, which is what Context::resetSettingsToDefaultValue restores — and runs the usual check on it before anything is applied, so that a violation leaves the whole statement without effect. The in-query SETTINGS <name> = DEFAULT form goes through the same interpreter and is covered too.

A reset that does not change the value is still allowed, because the check drops changes whose new value equals the current one. Resetting a setting that was never touched therefore keeps working under a CONST constraint and in readonly mode — which is what CI profiles that pin a setting to its declared default rely on (max_parser_backtracks in limit-recursion-settings.xml, for example). Settings without a declared default are unaffected: a custom setting is dropped rather than reset and cannot carry a constraint, and an unknown name stays silently ignored. The client only mirrors the reset into its own settings when the server accepted the statement, so its view does not drift.

One consequence worth calling out: a profile that caps max_threads below the machine's core count now rejects SET max_threads = DEFAULT, because the declared default resolves to auto(<cores>). That is the intended reading of the constraint, but it means the CI profiles that cap max_threads at 128 would reject that reset on a machine with more than 128 cores.

Verified locally against a build of this change: the four rejection paths above are rejected, the rejected statement leaves the value alone, and the no-op reset, custom-setting reset and unknown-setting reset all keep working. 04202_set_default_preserves_max_threads_auto and 04078_settings_constraints_compatibility_const still pass, and a sweep of every stateless test that uses = DEFAULT plus every test whose name mentions constraints/readonly/settings profiles produced no constraint or readonly error attributable to this change.

Related: #115328

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fixed a bypass of settings constraints: SET <name> = DEFAULT reset a setting without checking the constraints from the user's profile, so a MIN/MAX/CONST constraint could be escaped by resetting the setting instead of assigning to it, and a session could leave readonly mode with SET readonly = DEFAULT.


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

Version info

  • Merged into: 26.8.1.1914 (included in 26.8 and later)
  • Backported to: 26.7.6.22, 26.6.4.40, 26.3.23.5

`SET <name> = DEFAULT` resets a setting rather than assigning to it, so it does not
produce a `SettingChange` and never reached `SettingsConstraints`. Any constraint
could therefore be escaped by resetting the setting instead of assigning to it:

    CREATE SETTINGS PROFILE p SETTINGS max_query_size = 1000 MAX 1000;
    SET max_query_size = 2000;      -- SETTING_CONSTRAINT_VIOLATION, as expected
    SET max_query_size = DEFAULT;   -- silently restored the default of 262144

A `CONST` constraint was just as escapable, and so was readonly mode, which is
enforced by the same check:

    SET readonly = 2;
    SET readonly = 0;               -- READONLY, as expected
    SET readonly = DEFAULT;         -- silently left readonly mode

Describe the reset as the change it effectively is - to the value declared in the
source code, which is what `Context::resetSettingsToDefaultValue` restores - and run
the usual check on it before anything is applied, so that a violation leaves the
whole statement without effect. The in-query `SETTINGS <name> = DEFAULT` form goes
through the same interpreter and is covered too.

A reset that does not change the value is still allowed, because the check drops
changes whose new value equals the current one. Resetting a setting that was never
touched therefore keeps working under a `CONST` constraint and in readonly mode,
which is what the CI profiles that pin a setting to its default rely on. Settings
without a declared default are unaffected: a custom setting is dropped rather than
reset and cannot carry a constraint, and an unknown name stays silently ignored.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b1c89e2]

Summary: ✅


AI Review

Summary

This PR closes the SET <name> = DEFAULT constraint bypass by validating resets through the same settings-constraint machinery as explicit assignments, and it carries that fix through both the SQL SET path and Arrow Flight SetSessionOptions. The current code matches that contract, the earlier inline issues are addressed in the present diff, and the added stateless plus Arrow Flight regressions cover the main user-facing surfaces, so I did not find a remaining issue that warrants an inline review comment.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 30/30 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Aug 18, 2026
@PedroTadim

Copy link
Copy Markdown
Member

@alexey-milovidov we should check for query parameters as well. They can be used to set setting values since 26.7 #108760

@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing b1c89e2d3 with master d3d7dc9bb (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
Binary Master PR Δ
programs/clickhouse-stripped 705.16 MiB 702.14 MiB -3.02 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

Centralize reset validation so SQL and Arrow Flight enforce constraints consistently, including readonly checks for existing custom settings.

#115334
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Reviewed query-parameter handling: both standalone SET and in-query SETTINGS resolve placeholders through replaceQueryParametersInSettingsChanges before checkSettingsConstraints, so parameterized setting values already take the constrained path.

@PedroTadim

Copy link
Copy Markdown
Member

@alexey-milovidov two more cases. The client can set settings via command line options. Is that also covered? What about distributed plans? I think settings are sent to remote nodes?

The `readonly = 1` case of `05023_set_default_bypasses_settings_constraints`
failed in CI: `readonly = 1` rejects every setting change, and the native
client sends `send_logs_level` (and the test runner its randomized settings and
`log_comment`) along with each statement, so one of those was rejected before
the reset under test and the expected message never appeared.

Do that check over HTTP with a session and a bare URL that carries nothing but
the session id, so the only setting change in the request is the reset itself.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115334&sha=5df0e26be9f9c6bacd206edbcd2b7f265303ecad&name_0=PR&name_1=Fast%20test
PR: #115334
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Checked both, and neither can carry a reset.

Client command-line options. There is no DEFAULT spelling on that path at all: a command-line setting value is parsed as a value of the setting's type, so clickhouse-client --max_query_size=DEFAULT fails with Cannot parse input: expected 'eof' before: 'DEFAULT' (CANNOT_PARSE_INPUT_ASSERTION_FAILED), and the same holds for an HTTP URL parameter ?max_query_size=DEFAULT. What the client does send is a SettingsChange with a concrete value, and that goes through the regular checkSettingsConstraints in TCPHandler — verified against a build of this change, --max_query_size=2000 for a user with max_query_size = 1000 MAX 1000 is rejected with SETTING_CONSTRAINT_VIOLATION.

Distributed plans. The wire format for settings is a list of name/value pairs; a reset has no representation there, so nothing resets a setting on a remote node. The initiator sends the values it ended up with, and after this change a reset that a constraint forbids never produces such a value in the first place — the statement is rejected before anything is applied.

So the reset path is only reachable through SET <name> = DEFAULT and the in-query SETTINGS <name> = DEFAULT, both of which go through InterpreterSetQuery and are covered here, plus the Arrow Flight SET action, which the second commit covers.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master into this branch (b1c89e2) — the branch was ~1200 commits behind and the only red job was unrelated.

The single failure on d3aebc26 was Upgrade check (amd_release) → Logical error '!part.empty()'. It is not caused by this change: the same failure shows up on master itself (pull_request_number = 0) and across many unrelated pull requests over the last month, per the CIDB history for that test name.

@groeneai, please investigate the Logical error '!part.empty()' failure in Upgrade check (amd_release) (report for this run: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115334&sha=d3aebc26e4a1e605d6e99f6380d247cef5ed6d63&name_0=PR&name_1=Upgrade%20check%20%28amd_release%29) and provide a fix in a separate pull request. If a fix is already in progress, please link it here.

@groeneai

Copy link
Copy Markdown
Collaborator

The fix is already merged: #114826 (a5d8725, 2026-08-21 02:23 UTC).

The abort is not on the new server. fatal.log for d3aebc26 puts it on the old server of the upgrade pair, 26.7.4.58 (git hash 9c16c358):

Logical error: 'Part patch-4811bd937f1be8e9d5ec5374e149fefa-all_0_0_0_1 doesn't exist'
MergeTreeData.h:1710      modifyPartState
 <- MergeTreeData.cpp:9739 Transaction::commit
 <- ReplicatedMergeTreeSink.cpp:997/:1187 commitPart
 <- ReplicatedMergeTreeSinkPatch.cpp:58 finishDelayed
query: UPDATE t_lwu_block_number SET c2 = 'xx' WHERE id = 1;  (03100_lwu_drop_update_race)

A concurrent DROP TABLE clears the data parts index while the lightweight UPDATE pipeline is still committing its patch part. #114826 extends the table's share lock across the pipeline and ships a deterministic failpoint test (04905_lwu_drop_race_table_lock).

Two consequences for this run:

  • It started 2026-08-20 08:43, about 18h before the fix merged, so the red predates it.
  • Master alone will not clear this check. The old server is the previous stable release, and neither v26.7.4.58 nor v26.7.5.10 has the fix: Transaction::commit, Transaction::rollback and all three modifyPartState overloads are byte-identical between v26.7.4.58 and master.

Could you apply v26.7-must-backport to #114826? I do not hand-author release-branch PRs, and all three touched sites still have their anchors on 26.7, so the cherry-pick should be straightforward.

Two corrections to the CIDB reading. The master rows for that test name are all llvm_coverage jobs whose newest is 2026-07-26; Upgrade check (amd_release) itself has 0 master rows in 30 days (6 rows, 6 unrelated PRs). And the title !part.empty() is a crash-reporter misattribution, query-echoed from 04367_lambda_arg_backquoted_dot_no_logical_error, so that STID pools several distinct defects and the test name overstates the breadth of this one.

I have seen no recurrence since the merge (0 hits across 256 commits that ran this check), but that is ~24h against a base rate of about 9 events per 90 days here, so it is not yet meaningful.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

robot-clickhouse added a commit that referenced this pull request Aug 25, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Aug 25, 2026
Cherry pick #115334 to 26.5: Do not let SET <name> = DEFAULT bypass the settings constraints
robot-clickhouse added a commit that referenced this pull request Aug 25, 2026
robot-ch-test-poll4 added a commit that referenced this pull request Aug 25, 2026
Cherry pick #115334 to 26.3: Do not let SET <name> = DEFAULT bypass the settings constraints
robot-clickhouse added a commit that referenced this pull request Aug 25, 2026
robot-ch-test-poll4 added a commit that referenced this pull request Aug 25, 2026
Cherry pick #115334 to 26.6: Do not let SET <name> = DEFAULT bypass the settings constraints
robot-clickhouse added a commit that referenced this pull request Aug 25, 2026
robot-ch-test-poll4 added a commit that referenced this pull request Aug 25, 2026
Cherry pick #115334 to 26.7: Do not let SET <name> = DEFAULT bypass the settings constraints
robot-clickhouse added a commit that referenced this pull request Aug 25, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 25, 2026
alexey-milovidov added a commit that referenced this pull request Aug 26, 2026
Backport #115334 to 26.3: Do not let SET <name> = DEFAULT bypass the settings constraints
alexey-milovidov added a commit that referenced this pull request Aug 26, 2026
Backport #115334 to 26.5: Do not let SET <name> = DEFAULT bypass the settings constraints
tempel-it added a commit that referenced this pull request Aug 26, 2026
Master gained an equivalent `SET <name> = DEFAULT` constraint check in
#115334, under different names, so
this branch no longer carries its own:

* `SettingsConstraints::checkCanReset` is dropped for master's
  `SettingsConstraints::checkResetToDefault`.
* `Context::checkSettingsConstraintsForDefaults` is dropped for master's
  `Context::checkSettingsConstraintsForSettingsReset`.
* `Settings::getDefaultValue` is dropped; master reads the declared default from
  a local `Settings` instance instead.
* The Arrow Flight reset branch and its test are master's.

`src/Access/SettingsConstraints.*`, `src/Core/Settings.*`,
`src/Interpreters/Context.*` and `src/Server/ArrowFlight/ArrowFlightServer.cpp`
are now identical to master, and what is left is what master does not cover:

* The constraint check on a `SETTINGS` clause nested in a subquery, a CTE or a
  view's inner query, in `QueryTreeBuilder::buildSelectExpression`.
* The nested `SETTINGS <name> = DEFAULT` form, now checked through master's
  `checkSettingsConstraintsForSettingsReset`.
* A setting assigned or reset after a `profile` change in the same `SET`
  statement. This is now `checkSettingsConstraintsAfterProfileChange`, a check
  on a scratch context that applies nothing, so it sits next to the two checks
  master already runs before the statement is applied and keeps the property
  that a rejected statement leaves the session untouched.

Master's `05023_set_default_bypasses_settings_constraints` covers the top-level
reset only, so `05019`, `05020` and `05021` stay.
alexey-milovidov added a commit that referenced this pull request Aug 27, 2026
Backport #115334 to 25.8: Do not let SET <name> = DEFAULT bypass the settings constraints
clickhouse-gh Bot added a commit that referenced this pull request Aug 29, 2026
Backport #115334 to 26.6: Do not let SET <name> = DEFAULT bypass the settings constraints
clickhouse-gh Bot added a commit that referenced this pull request Aug 29, 2026
Backport #115334 to 26.7: Do not let SET <name> = DEFAULT bypass the settings constraints
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
ACCESS: `SET <name> = DEFAULT` resets a constrained setting (e.g. readonly) to its built-in default -> security: settings constraints bypassed.
Effect in the production build a394e92: reproduced by hand: `SET readonly = 2; SET readonly = DEFAULT` leaves readonly = 0 on the production build and 2 on official 26.8.8.8.
Upstream patch unchanged, with its test(s): 05023_set_default_bypasses_settings_constraints.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
…ustom settings prefixes, reviewed dispatch sites, two open defects)

The CAND4 (e7432ca) release gate reported, besides the open KD-D512-MIXED-GROUPBY-NULLABLE:
- 3 skipped tests (04303, 04305, 04357: "no shard"): run_checks.sh passed --no-shard, a leftover from before the
  isolated server listened on 127.0.0.1-3 with the test clusters. Dropped, so clickhouse-test detects shard support
  from the server config as the triage runs do.
- 05023 (ClickHouse#115334's test): the isolated server allowed only the custom_ settings prefix; it now allows SQL_,custom_
  like the upstream test config (tests/config/config.d/custom_settings_prefixes.xml). isolated_ch.sh is vendored from
  the skill draft; VENDORED.sha256 updated.
- 2 dispatch-scan candidates: the Int512 fast path of 7581066 (bothFitInt256/mulInt256Range); reviewed, not a gap.
- 04411 (ClickHouse#108607's test): its three-index section recurses in equals() on every 26.3 build, including production and
  builds without the backport (FILE_DOESNT_EXIST, the bug ClickHouse#108607 fixes, is gone). New open KD-UP-TEXTINDEX-DIRECTREAD-3IDX
  (pre-existing upstream 26.3 bug, upstream fix not identified; workaround query_plan_direct_read_from_text_index = 0).
- 04881 (ClickHouse#114624's test): its only differing line is the reference with the host name 'build' replaced by 'localhost'
  by clickhouse-test; the output is otherwise identical. New open KD-ENV-HOSTNAME-SUBSTITUTION (environment); the test
  and its reference are not edited.
The fifth failure, 04409, is the missing dependency ClickHouse#106905 (previous commit). Registry: 147 entries, 3 open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 7, 2026
SET x = DEFAULT restored the compiled default of x and ignored an active
compatibility setting, so the session reported the current version's default
while compatibility still claimed the old era - a state no new session with
the same compatibility could reach. That contradicts the documented contract
of compatibility ("only settings that have not been modified are affected"),
since after a reset the setting is by definition not modified.

SET x = DEFAULT is not a SettingChange: the parser puts the bare name into
ASTSetQuery::default_settings, and the reset reaches
BaseSettings::resetToDefault, which is not virtual. The whole chain therefore
bypasses SettingsImpl::set, the only compatibility-aware mutator and the sole
override of the single virtual in BaseSettings, so the derivation never re-runs.

SettingsImpl::resetToDefaultRespectingCompatibility resets and then calls the
existing applyCompatibilitySetting. Reading compatibility after the reset is
what makes SET compatibility = DEFAULT revert every setting it derived, with
no special case, because the value read is then the empty default. Calling the
existing walk rather than duplicating it filtered to one name keeps the reset
path from diverging from the fresh-session path, which matters because ClickHouse#114190
is editing that walk.

Only the four surfaces that implement SET <name> = DEFAULT are repointed;
every internal caller that wants the declared default keeps the raw reset.
Settings::setDefaultValue itself is deliberately not repointed: Connection.cpp
resets a setting purely to mark it unchanged so it is not resent and asserts
exactly that on the next line, and re-deriving assigns a value and so sets
changed, which was measured to fire that assert on an ordinary query whenever
the user profile activates compatibility. AsynchronousInsertQueue resets
async_insert to let the remote part decide, and async_insert has a live history
row, so re-deriving would take that decision away under compatibility <= 26.1.
MySQLHandler rewrites = DEFAULT to SQL text and re-enters the first surface, so
it is fixed with no edit.

SettingsConstraints::checkResetToDefault had to move with it. It was
centralized in ClickHouse#115334 on the premise that the defaults do not depend on the
session, which this change makes false: with a profile that both pins and
constrains a setting, the pin keeps it changed, compatibility leaves it alone,
and a reset would install the era value the constraint forbids. Measured on a
build carrying the reset change without this half, such a reset was accepted
silently and landed below the profile's declared minimum. The check now
replays the statement on a throwaway copy of the context the resets will
mutate and validates the values it lands on; enforcement stays on the checking
context, matching the ordinary-change check one line earlier, and the whole
statement is still left without effect on a violation.

Behaviour changes: a reset under an active compatibility now yields the era
value; SET compatibility = DEFAULT now reverts what it derived; and a reset
can now be rejected by a settings constraint where it previously succeeded.

Not addressed, pre-existing and wider: applyCompatibilitySetting writes through
BaseSettings::set, so SET compatibility = '<version>' installs era values for
every unchanged setting with no constraint check at all.

Closes: ClickHouse#116883
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix 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.

7 participants