Skip to content

Do not let a setting masking rule throw - #119028

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
Algunenano:masking-must-not-throw
Sep 10, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
Algunenano:masking-must-not-throw

Conversation

@Algunenano

@Algunenano Algunenano commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Closes: #118950
Related: #117523

Follow-up to #117523.

The masking rules for nats_url, rabbitmq_address and after_processing_move_connection_string mask the credential inside a URL or a connection string, so they read the value with safeGet<std::string>. Masking runs before the setting is validated, so the value need not be a String: SETTINGS nats_url = 4222 reached the rule and threw BAD_GET.

The throw escaped hasSecretParts/formatForLogging, and executeQueryImpl then fell back to logging the raw statement text. That put every other credential in the same statement into the server log in cleartext, so writing the port where the URL goes was enough to publish the nats_password next to it:

-- What the user meant
CREATE TABLE events (a String) ENGINE = NATS
SETTINGS nats_url = 'localhost:4222', nats_subjects = 'events',
         nats_format = 'JSONEachRow', nats_password = 'hunter2';

-- What they typed: the port instead of the URL
CREATE TABLE events (a String) ENGINE = NATS
SETTINGS nats_url = 4222, nats_subjects = 'events',
         nats_format = 'JSONEachRow', nats_password = 'hunter2';

Both statements fail, but the second one leaves nats_password = 'hunter2' in system.query_log.query and in the server log, while the first one correctly stores nats_password = '[HIDDEN]'.

A masking rule must not throw. The rules now return std::optional<std::string>, and the three that need the value as a string read it with tryGet and mask nothing when it is not a String. This is the guard CoreSettings::renderSecretSettingValue already has, for exactly this reason. The rules that hide the value whole are unchanged, so a non-String value for them is still masked.

Also checked, and clean: FunctionSecretArgumentsFinderAST::tryGetString already checks the field type before safeGet; ASTSettingsProfileElement routes through the guarded core function; ASTAuthenticationData::hasSecretParts decides from the authentication type without reading a value; dictionary SOURCE(...) masking is keyed on the argument name. Two sweeps found no remaining leak: 30 masked setting names against 10 value types, and 18 table function shapes with wrong types, NULL and non-literal arguments.

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

...


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

Version info

  • Merged into: 26.9.1.1116 (included in 26.9 and later)
  • Backported to: 26.8.3.93, 26.7.7.81, 26.6.5.110, 26.3.33.60

The masking rules for `nats_url`, `rabbitmq_address` and
`after_processing_move_connection_string` mask the credential inside a URL
or a connection string, so they read the value with `safeGet<std::string>`.
Masking runs before the setting is validated, so the value need not be a
String: `SETTINGS nats_url = 4222` reached the rule and threw `BAD_GET`.

The throw escaped `hasSecretParts`/`formatForLogging`, and `executeQueryImpl`
then fell back to logging the raw statement text, which put every other
credential in the same statement in the server log in cleartext. Writing the
port where the URL goes was enough to publish the `nats_password` next to it.

A masking rule must not throw. The rules now return
`std::optional<std::string>` and the three that need the value as a string
read it with `tryGet`, masking nothing when it is not a String. This is the
guard `CoreSettings::renderSecretSettingValue` already has for the same
reason.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [371210c]

Summary: ✅


AI Review

Summary

This PR fixes the BAD_GET exception when masking malformed engine SETTINGS values before validation, so numeric wrong-type values no longer abort masking of the whole statement. The main leak that motivated the fix is only partially closed, though: AST-backed wrong-type values can still flow through the new nullopt path and be formatted back with secrets visible.

Findings

❌ Blockers

  • src/Storages/NATS/NATS_fwd.h:28 Returning nullopt for a non-String nats_url is only safe for plain scalar mistypes. ParserSetQuery accepts disk(...) for any SETTINGS value and stores it as FieldFromAST, and ASTSetQuery::formatImpl falls back to FieldVisitorToString when this rule declines to mask. FieldVisitorToString prints CustomType with show_secrets = true, so a malformed query such as ... SETTINGS nats_password = 'secret', nats_url = disk(type = 's3', secret_access_key = 'disk_secret') still logs secret_access_key in cleartext. The same bug shape exists in the matching rabbitmq_address and after_processing_move_connection_string rules. Suggested fix: keep the non-throwing behavior for scalar mistypes, but fail closed for secret-bearing CustomType values by rendering them with show_secrets = false or hiding the whole value instead of returning nullopt.
Tests
  • ⚠️ Add a focused regression that sends a malformed AST-valued engine setting through ASTSetQuery, e.g. nats_url = disk(type = 's3', secret_access_key = '...') next to another masked setting, and assert both the server log line and system.query_log.query stay redacted. The current shell test only covers numeric wrong-type values.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit 371210c.

Metric Baseline Current Δ
Lines 88.90% 88.90% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 9, 2026
Comment thread src/Storages/NATS/NATS_fwd.h Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). label Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 371210c42 with master 1e7129b5d (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

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

Job report

@Algunenano
Algunenano added this pull request to the merge queue Sep 10, 2026
Merged via the queue into ClickHouse:master with commit ae539fc Sep 10, 2026
174 checks passed
@Algunenano
Algunenano deleted the masking-must-not-throw branch September 10, 2026 10:19
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 10, 2026
@Algunenano Algunenano added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 11, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 11, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Sep 11, 2026
Cherry pick #119028 to 26.3: Do not let a setting masking rule throw
robot-ch-test-poll2 added a commit that referenced this pull request Sep 11, 2026
Cherry pick #119028 to 26.6: Do not let a setting masking rule throw
robot-ch-test-poll2 added a commit that referenced this pull request Sep 11, 2026
Cherry pick #119028 to 26.7: Do not let a setting masking rule throw
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 11, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 11, 2026
Backport #119028 to 26.8: Do not let a setting masking rule throw
clickhouse-gh Bot added a commit that referenced this pull request Sep 11, 2026
Backport #119028 to 26.6: Do not let a setting masking rule throw
clickhouse-gh Bot added a commit that referenced this pull request Sep 11, 2026
Backport #119028 to 26.7: Do not let a setting masking rule throw
alexey-milovidov added a commit that referenced this pull request Sep 12, 2026
Backport #119028 to 26.3: Do not let a setting masking rule throw
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 18, 2026
raimannma pushed a commit to raimannma/ClickHouse that referenced this pull request Oct 4, 2026
…)` arguments

`key = value` parses into `equals(key, value)`, but `equals` can also be
written explicitly with a single argument, e.g. `disk(equals(password))`.
Both consumers of `disk(...)` arguments checked only that the argument is
an `equals` call with a non-empty argument list and then indexed
`children[1]`:

- `DiskConfigurationMasker::visit` overwrote `children[1]` with
  `'[HIDDEN]'` for a secret key. It runs while the query is masked for
  logging, before the statement is interpreted, so even
  `SET max_threads = disk(equals(password))` reached it.
- `getDiskConfigurationFromASTImpl` read `children[1]` for any key on the
  `CREATE`/`ATTACH` path.

Debug builds abort on the boost bounds assertion, ASan reports a
heap-buffer-overflow, and official 26.8/26.9 builds die with a
segmentation fault on
`CREATE TABLE ... SETTINGS disk = disk(type = 'local', path = 'x/', equals(name))`.

The masker now hides every value of a secret key (an explicit
`equals(password)` has none); before, it hid only the first value, so
`'b'` in `equals(password, 'a', 'b')` was logged. It also no longer
throws on an argument that is not `key = value` (a positional value,
`equals()`, a key that is not an identifier) and hides such an argument
whole instead. The throw happened while the query was masked for
logging, so the whole query was logged unmasked, with every other
secret in it, as ClickHouse#119028 fixed for the setting masking rules. The disk
configuration parser rejects all of these arguments, and now also a key
without a value, with `BAD_ARGUMENTS`. Extra values stay accepted and
ignored, as in released versions, so table definitions stored with them
keep loading.

Because extra values stay accepted, `ServerCredentialMarkerInjector` now
strips a user-supplied `_server_credentials_allowed` marker with extra
values as well. It stripped only the two-argument form, while the parser
honors the marker's first value, so `equals(_server_credentials_allowed,
1, 'x')` was stored and exempted the disk from the S3 credential
restriction on reload.

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

comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore 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-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.

Ungated engine masking lambdas throw BAD_GET, defeating the PR's own masking

4 participants