Skip to content

Keep the valueless SETTINGS form out of the AST JSON round trip and secret detection - #112902

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-valueless-setting-json-and-secret-parts
Aug 2, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-valueless-setting-json-and-secret-parts

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Follow-up to the two unresolved review comments on #112067 (merged as a208bbb94ab).

SETTINGS name with no value is a shorthand for = true, and #112067 made it an error for a setting whose type is not Bool. Two paths sidestepped that rule.

The AST JSON round trip dropped the flag. ASTSetQuery::writeJSON did not serialize SettingChange::shorthand and readJSON did not restore it, so the clickhouse_json dialect reconstructed the valueless form as an explicit name = true, which is accepted for a setting of any type:

$ clickhouse local -q "SELECT parseQueryToJSON('SELECT 1 SETTINGS max_threads')"
... "changes":[{"name":"max_threads","value":{"field_type":"Bool","value":true}}] ...
$ clickhouse local --dialect clickhouse_json --allow_experimental_json_ast_dialect 1 --queries-file q.json
1                       -- runs with max_threads = 1
$ clickhouse local -q "SELECT 1 SETTINGS max_threads"
Code: 53. ... Setting 'max_threads' has type MaxThreads, so it cannot be set without a value. (TYPE_MISMATCH)

The flag is now written and read back. A payload pairing it with anything other than true is rejected with BAD_ARGUMENTS: both formatImpl and hasSecretParts skip the value of a valueless setting, so without that check the flag could be used to hide a real value - a secret one in particular - from formatting and from the query log.

hasSecretParts read the value as a String. executeQueryImpl masks the query for logging before any settings validation runs, so a valueless format_avro_schema_registry_url reached safeGet<String> as Bool true:

$ clickhouse local -q "SELECT * FROM (SELECT 1 SETTINGS format_avro_schema_registry_url)"
Code: 170. DB::Exception: Bad get: has Bool, requested String. (BAD_GET)

It now reports the intended TYPE_MISMATCH. A valueless setting carries Bool true and can never be secret.

New test 04665_valueless_setting_ast_json_and_secret_parts.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Related: #112067

`SETTINGS name` with no value is a shorthand for `= true` and is rejected for
a setting whose type is not `Bool`. Two paths sidestepped that rule.

`ASTSetQuery::writeJSON` did not serialize `SettingChange::shorthand` and
`readJSON` did not restore it, so the `clickhouse_json` dialect reconstructed
the valueless form as an explicit `name = true`, which is accepted for a
setting of any type: `parseQueryToJSON('SELECT 1 SETTINGS max_threads')`
round-tripped into a query that ran with `max_threads = 1` instead of failing
with `TYPE_MISMATCH`. The flag is now written and read back, and a payload
pairing it with anything other than `true` is rejected, so it cannot be used
to hide a real value - a secret one in particular - from `formatImpl` and from
`hasSecretParts`, both of which skip the value of a valueless setting.

`ASTSetQuery::hasSecretParts` read the value of
`format_avro_schema_registry_url` with `safeGet<String>`. `executeQueryImpl`
masks the query for logging before any settings validation runs, so a valueless
setting reached it as Bool `true` and
`SELECT * FROM (SELECT 1 SETTINGS format_avro_schema_registry_url)` reported
`BAD_GET` instead of the intended `TYPE_MISMATCH`. A valueless setting is now
skipped there as well.

Follow-up to the review comments left on
#112067
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4eaa220]

Summary: ✅


AI Review

Summary

This PR preserves SettingChange::shorthand across the JSON AST round trip and fixes the BAD_GET path around format_avro_schema_registry_url, but it still accepts forged shorthand payloads for Bool settings. Because checkShorthandChange only checks the setting type while ASTSetQuery::formatImpl drops every shorthand value, a crafted JSON AST can execute setting = false while formatting and logging as the valueless SETTINGS setting form, so I cannot approve it as-is.

Findings

❌ Blockers

  • src/Parsers/ASTSetQuery.cpp:308 readJSON now keeps any value when shorthand is true and relies on checkShorthandChange to reject it later, but checkShorthandChange only rejects non-Bool settings (src/Core/BaseSettings.h:444). For a payload like {"name":"optimize_move_to_prewhere","shorthand":true,"value":{"field_type":"Bool","value":false}}, execution accepts false while formatImpl omits the value entirely (src/Parsers/ASTSetQuery.cpp:106). That makes formatQueryFromJSON, formatForLogging, and clickhouse_json logs misrepresent the executed query, so the value-hiding bypass is still present for Bool settings. Suggested fix: validate that shorthand implies value == true at the first post-deserialization point where masked logging is available, or make checkShorthandChange enforce it.
Tests
  • ⚠️ Add a focused regression for a forged Bool setting payload, for example optimize_move_to_prewhere with "shorthand":true and false, and assert both rejection and that formatting/logging do not silently collapse it to the valueless form.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: reject forged shorthand payloads for Bool settings as well, and cover that case with a regression test.

Comment thread src/Parsers/ASTSetQuery.cpp Resolved
…time

The `BAD_ARGUMENTS` rejection for a `shorthand` flag paired with a value the
parser would never produce ran inside `IAST::createFromJSON`, before
`executeQueryImpl` has an AST to mask with, so it sent the raw JSON text - the
value included - down the unmasked `wipeSensitiveDataAndCutToLength` logging
path.

The value is kept instead: `hasSecretParts` can then still see it and
`formatImpl` can still hide it, and `checkShorthandChange` rejects the setting
itself once masked logging is available. The `TYPE_MISMATCH` is unchanged.

`hasSecretParts` and `formatImpl` now read the value of
`format_avro_schema_registry_url` with `tryGet<String>` rather than being
guarded on `shorthand`. That covers the valueless form and every other `Field`
type the AST JSON path can carry, so neither one can report `BAD_GET` in place
of the setting's own error, and a crafted value that really is a String is
still detected and masked rather than skipped.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Addressed in 11df58c729c.

The blocker is right that the rejection ran inside IAST::createFromJSON, before executeQueryImpl has an AST to mask with, so the raw JSON text went down the unmasked wipeSensitiveDataAndCutToLength path. I dropped the rejection: the value is kept, so hasSecretParts can still see it and formatImpl can still hide it, and checkShorthandChange rejects the setting itself once masked logging is available. The TYPE_MISMATCH is unchanged, and there is no new error path at all now.

Instead of guarding on shorthand, hasSecretParts and formatImpl now read the value of format_avro_schema_registry_url with tryGet<String>. That is strictly more general — it covers the valueless form and every other Field type the AST JSON path can carry, so neither can report BAD_GET in place of the setting's own error, and a crafted value that really is a String is still detected and masked rather than skipped. It also fixes a second BAD_GET: formatQueryFromJSON on a payload giving that setting a UInt64 value used to throw, and now formats as = 1.

One correction on the premise, though: the raw-JSON logging leak is not introduced here. A payload with no shorthand key at all already leaks, on master, for a query that succeeds:

$ J=$(clickhouse client -q "SELECT parseQueryToJSON(\$\$SELECT 777 SETTINGS format_avro_schema_registry_url = 'http://u7:p7@localhost'\$\$)")
$ curl -s ".../?dialect=clickhouse_json&allow_experimental_json_ast_dialect=1" --data-binary "$J"
777
$ -- system.query_log:
QueryStart   {"type":"SelectWithUnionQuery", ... "value":"http:\/\/u7:p7@localhost" ...}

The plaintext password is in query_log; the equivalent SQL query is logged as http://u7:[HIDDEN]@localhost. The cause is unrelated to this PR: the clickhouse_json dialect does not unescape \/, so the deserialized value is http:\/\/u7:p7@localhost, which maskURIPassword cannot parse, and hasSecretParts therefore returns false. Passing the same payload with plain / is masked correctly.

That is a separate pre-existing bug — it also corrupts ordinary string literals (SELECT 'a/b' round-tripped through the dialect returns a\/b) — so I am reporting it on its own rather than folding a security fix into this PR.

The test transported the JSON payloads through `TSV`, which escapes the
backslash of the `\/` that `writeJSONString` emits for a `/`. The payload the
test actually executed therefore carried a doubled backslash, so its URL was
not a URL any more and could not have been masked whatever the code did.
Captured with `FORMAT TSVRaw` instead.

With faithful transport the crafted payload can be checked for what it is meant
to check: executed over HTTP, so the rejection happens server-side, the
`ExceptionBeforeStart` row in `system.query_log` is the masked AST
(`SELECT 1 SETTINGS format_avro_schema_registry_url`, value omitted) rather
than the raw JSON text, and it contains no password.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Correction to my previous comment: the "pre-existing raw-JSON leak" I described does not exist. I had captured the JSON payloads through TSV, which escapes the backslash of the \/ that writeJSONString emits for a /, so the payload I was sending carried a doubled backslash and its URL was no longer a URL — that, not a dialect bug, is why maskURIPassword did not match it. Captured with FORMAT TSVRaw, the clickhouse_json dialect masks correctly, and SELECT 'a/b' round-trips through it unchanged. Nothing to report separately.

So the blocker was right on its own terms, and the fix in 11df58c729c is what makes the difference. Verified end to end on a server with query_log enabled, sending the payload over HTTP so the rejection happens server-side:

payload logged in system.query_log
valid, explicit secret SELECT 901 SETTINGS format_avro_schema_registry_url = 'http://u9:[HIDDEN]@localhost'
crafted "shorthand":true + secret SELECT 902 SETTINGS format_avro_schema_registry_url — value omitted, TYPE_MISMATCH

With the rejection still in place the second row would have been the raw JSON text with p9 in the clear, exactly as the review said.

a0449ca216f fixes the test's transport (FORMAT TSVRaw) — without it the crafted case was passing vacuously — and adds the assertion directly: it now reads the ExceptionBeforeStart row for its own query_id and checks the logged query contains no password.

… `query_log` read

The `various` sub-check requires every `system.query_log` query to be scoped
with `current_database = currentDatabase()`, and a `SYSTEM FLUSH LOGS` after a
`curl` request to sit in a retry loop, because the HTTP response can return
before the log entry is written (#84364).
/// so throwing would send the raw JSON text - the value included - down the unmasked
/// `wipeSensitiveDataAndCutToLength` logging path. The value is kept instead, so
/// `hasSecretParts` can still see it and `formatImpl` can still hide it, and
/// `checkShorthandChange` then rejects the setting itself once logging is safe.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

checkShorthandChange only verifies that a shorthand setting's type is Bool (BaseSettings.h:444-452); it does not require the carried value to still be true. With the current code, a payload like {"name":"optimize_move_to_prewhere","shorthand":true,"value":{"field_type":"Bool","value":false}} is accepted and applies false, while formatImpl drops the value for every shorthand change (ASTSetQuery.cpp:103-107).

That means formatQueryFromJSON, masked query logging, and any other formatter built on ASTSetQuery will render SETTINGS optimize_move_to_prewhere even though execution saw = false, so the "hide a real value behind the shorthand flag" bypass is still present for Bool settings. Please validate shorthand => value == true at the first post-deserialization point where masked logging is available (or make checkShorthandChange enforce it too).

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.

🕵 Confirmed and fixed in #113025.

BaseSettings::checkShorthandChange now also requires the carried value to be Bool true, after the existing type check (so a non-Bool setting still reports TYPE_MISMATCH). In addition, ASTSetQuery::formatImpl elides the value only when it really is true, so formatQueryFromJSON and the masked logging path can no longer render a bare setting name for a change that carries something else - the value is printed through the secret-masking path in the window before the change is rejected. New test 04691_shorthand_setting_value_mismatch.

@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 20/22 (90.91%) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 2, 2026
@alexey-milovidov alexey-milovidov self-assigned this Aug 2, 2026
Merged via the queue into master with commit 55e0c76 Aug 2, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-valueless-setting-json-and-secret-parts branch August 2, 2026 00:39
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 2, 2026
pull Bot pushed a commit to AKJUS/ClickHouse that referenced this pull request Aug 3, 2026
…rue`

`SettingChange::shorthand` records that a setting was written as `SETTINGS name`
with no value, which stands for `name = true`. The parser always pairs the flag
with Bool `true`, but the AST JSON dialect can pair it with anything, and
`BaseSettings::checkShorthandChange` only verified that the setting's type is
`Bool`. A payload such as

    {"name":"optimize_move_to_prewhere","shorthand":true,
     "value":{"field_type":"Bool","value":false}}

was therefore accepted and executed with `false`, while `ASTSetQuery::formatImpl`
elided the value for every shorthand change - so `formatQueryFromJSON`,
`system.query_log` and every other formatter rendered
`SETTINGS optimize_move_to_prewhere`, under-reporting what actually ran.

`checkShorthandChange` now also requires the carried value to be Bool `true`.
It runs after the settings type check, so a non-`Bool` setting still reports
`TYPE_MISMATCH`, and it is the first point after deserialization where an
exception is logged with the AST masked rather than with the raw JSON text.

`formatImpl` now elides the value only when it really is `true`, so a formatter
can never print a bare setting name for a change that carries something else -
the value of a crafted payload is printed, through the secret-masking path, in
the window before the change is rejected.

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

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

2 participants