Skip to content

Do not abort the secret arguments finder on mixed named and positional arguments - #107818

Merged
evillique merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-secret-args-finder-mixed-named-positional-crash
Jun 18, 2026
Merged

evillique merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-secret-args-finder-mixed-named-positional-crash

Conversation

@groeneai

@groeneai groeneai commented Jun 18, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a server abort (Logical error: 'index >= result.start') when formatting a malformed query that mixes the positional and named secret-argument forms of the s3/gcs table functions, e.g. s3('url', 'a', 'b', secret_access_key = 'c').

Description

Found by the AST fuzzer on PR #106189 (AST fuzzer (amd_debug, targeted) and AST fuzzer (amd_debug, targeted, old_compatibility), STID 2508-6d1e). The crash is in pre-existing trunk code, not in that PR's changes; the coverage-driven "targeted" fuzzer just selected the secret-args masking test because the PR touches FunctionSecretArgumentsFinder.h.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106189&sha=d28eef2e29f7ca4e1c5dfbd225e74fecb712dd9c&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29

Root cause: markSecretArgument() asserted chassert(index >= result.start), i.e. that arguments are always marked in non-decreasing index order. The s3/gcs finders first scan the whole argument list for the named secret form (secret_access_key = '...') and mark it, then unconditionally mark the positional secret argument at an earlier index. When a malformed query supplies both forms, the named argument (higher index) is marked first, then the positional one (lower index) trips the assertion and aborts the server. The finder runs over arbitrary user AST while masking secrets for error messages, query_log and SHOW CREATE, so it must never abort on adversarial input.

Fix: markSecretArgument() now widens the masked range to cover the requested index instead of asserting on the order. Over-masking a malformed query is the safe direction; the secret is still hidden. For well-formed queries the marked range is unchanged. Because the fix is in the shared markSecretArgument() primitive, it also hardens any other finder that might mark out of order. A stateless regression test (04343_secret_args_finder_mixed_named_positional) reproduces the abort without the fix and passes with it.

The azureBlobStorage finders are not affected by this abort: they never scan the named account_key form in the positional path (only one argument is ever marked), so they cannot mark out of order. azureBlobStorage is therefore dropped from the changelog claim. The pre-existing gap that the named account_key is not masked for azureBlobStorage mixed forms is a separate masking-completeness issue, out of scope for this abort fix.

Version info

  • Merged into: 26.6.1.979

…l arguments

markSecretArgument() asserted that arguments are always marked in
non-decreasing index order (chassert(index >= result.start)). That holds
for well-formed queries, but the s3/gcs and azureBlobStorage finders first
scan the whole argument list for the named secret form (secret_access_key =
'...') and only afterwards mark the positional secret argument. A malformed
query that mixes both forms, e.g.

  SELECT * FROM s3('url', 'a', 'b', secret_access_key = 'c')

marks the named argument (a higher index) first, then asks to mark the
earlier positional one, tripping the assertion and aborting the server.

The finder runs over arbitrary user AST while masking secrets for error
messages, query_log and SHOW CREATE, so it must never abort on adversarial
input. Make markSecretArgument() widen the masked range to cover the
requested index instead of asserting on the order. Over-masking a malformed
query is the safe direction: the secret is still hidden.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. clickhouse-local --query "SELECT * FROM s3('url', 'a', 'b', secret_access_key = 'c')" aborts every time (SIGABRT). Server-side: the same query crashes the server (confirmed via server log: Logical error: 'index >= result.start' at FunctionSecretArgumentsFinder.h:85).
b Root cause explained? findS3FunctionSecretArguments first calls findSecretNamedArgument("secret_access_key", url_arg_idx), which scans the whole arg list and marks the named secret_access_key='c' at the highest index (here 3), so result.start=3, count=1. It then unconditionally calls markSecretArgument(url_arg_idx + 2) (index 2). In markSecretArgument, result.count != 0 so result.start is not reset, and chassert(2 >= 3) fails -> abortOnFailedAssertion. The abort fires while formatting the AST to hide secrets (error messages / query_log / SHOW CREATE).
c Fix matches root cause? Yes. The invariant the assertion encodes (arguments marked in non-decreasing order) is simply false for malformed queries mixing the named and positional secret forms. The fix makes markSecretArgument widen the masked range to cover the requested index instead of asserting on the order, so the finder never aborts on adversarial input.
d Test intent preserved / new tests added? New stateless regression test 04343_secret_args_finder_mixed_named_positional added. It asserts the malformed s3/gcs mixed-form queries return a normal BAD_ARGUMENTS error instead of crashing the server. Existing masking behaviour for well-formed s3/mysql/remote/encrypt queries is unchanged (verified: s3('h','id','mysecret') still formats as s3('h', 'id', '[HIDDEN]')).
e Both directions demonstrated? Yes. Without the fix: server SIGABRTs with Logical error: 'index >= result.start' and the regression test fails (server dies). With the fix: the test passes 21/21 (1 + 20 randomized runs) and the server stays alive.
f Fix is general, not a narrow patch? Yes. The fix is in the shared primitive markSecretArgument, not in one caller. The same scan-named-then-mark-positional pattern exists in findS3FunctionSecretArguments, findS3StorageTableEngine..., findAzureBlobStorageFunctionSecretArguments and findAzureBlobStorageTableEngineSecretArguments, so all of them are covered. The fix targets the root cause (a false ordering invariant), not a symptom.

Session id: cron:clickhouse-worker-slot-2:20260618-012800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @evillique @alexey-milovidov — could you review this? It stops markSecretArgument from aborting the server (Logical error: 'index >= result.start') when masking a malformed query that mixes the named (secret_access_key = ...) and positional secret-argument forms of s3/gcs/azureBlobStorage; the finder now widens the masked range instead of asserting on argument order. Found by the AST fuzzer on #106189.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5cad35c]

Summary: ✅


AI Review

Summary

This PR changes markSecretArgument so malformed s3/gcs calls that mix positional and named secret arguments widen the masked interval instead of hitting the old debug assertion. The current code matches the stated contract, and the strengthened regression test now checks the release-visible system.query_log masking surface rather than only the error code. I found no remaining review findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 18, 2026
arm_tidy flagged the two if-branches that widen the masked range
(FunctionSecretArgumentsFinder.h:91,93) under -warnings-as-errors.
Rewrite them with std::max/std::min (equivalent for the size_t operands)
and include <algorithm>.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The chassert in markSecretArgument is compiled out in release builds, so
the original abort is debug-only. In a release build the old code set count
back to 0 for this exact argument order, and the malformed query still
returned BAD_ARGUMENTS regardless of the fix; the release-visible regression
was that the formatted/logged query no longer hid the secret. The existing
test only checked the error code, which both old and new code return, so it
did not catch the release-visible leak.

Extend the test to flush system.query_log and assert the masked query text
contains [HIDDEN] and not the literal secret values for the mixed
positional + named s3/gcs forms. The query is filtered by currentDatabase()
so the test stays parallel-safe (no no-parallel tag).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (test-strengthening commit 5cad35c)

# Question Answer
a Deterministic repro? Yes. SELECT * FROM s3('url', 'a', 'b', secret_access_key = 'c') (and the gcs / 3-arg-mixed variants), then SYSTEM FLUSH LOGS query_log and read the ExceptionBeforeStart row. 15/15 sequential and 6/6 concurrent runs produced identical output.
b Root cause explained? The S3/GCS function finder marks the named secret (secret_access_key = ..., higher index) first, then asks to mark the earlier positional secret. Old markSecretArgument did count = index + 1 - start, which underflows to 0 for this order (debug: chassert(index >= start) aborts; release: chassert compiled out, count silently 0). The query still fails BAD_ARGUMENTS either way, so the release-visible effect is purely that the secret is no longer masked in the logged/formatted query.
c Fix matches root cause? The code fix (already in this PR) widens the masked range with std::min/std::max instead of underflowing. This commit only strengthens the test to assert the actual release-visible surface (masked query text), not the error code that both versions return.
d Test intent preserved / new tests added? The three serverError BAD_ARGUMENTS checks are kept (verified the client enforces the code: a wrong expected code is reported as a mismatch). Added a system.query_log masking assertion that catches the release-visible leak the error-code check missed.
e Both directions demonstrated? Yes, on a local debug build with the chassert removed to get release semantics. Old logic: masked=0, leaked_named=3, leaked_literal=3 (secret visible). Fixed logic: masked=3, leaked_named=0, leaked_literal=0 ([HIDDEN]).
f Fix is general, not a narrow patch? The code fix lives in the shared markSecretArgument primitive, so it covers every finder (s3/gcs/azure function and table-engine paths) that marks arguments out of order, not just the reproduced case. Azure was dropped from the user-visible claim earlier in this PR because its finder produces at most one positional mark and so cannot hit this path.

Session id: cron:clickhouse-worker-slot-1:20260618-032400

@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.30% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.60% 77.50% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 8/8 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@evillique evillique self-assigned this Jun 18, 2026
@evillique
evillique added this pull request to the merge queue Jun 18, 2026
Merged via the queue into ClickHouse:master with commit ce57fd3 Jun 18, 2026
165 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 18, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this looks like a backport candidate for 26.1, 26.2, 26.3, 26.4, 26.5. I ran this PR's own regression test against each branch's binary and the bug reproduces there (the fix isn't present yet).

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 pr-bugfix Pull request with bugfix, not backported by default 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