Skip to content

Hide secret arguments in EXPLAIN AST output - #120313

Merged
harikrishnan94 merged 9 commits into
ClickHouse:masterfrom
harikrishnan94:explain-ast-hide-secrets
Sep 29, 2026
Merged

harikrishnan94 merged 9 commits into
ClickHouse:masterfrom
harikrishnan94:explain-ast-hide-secrets

Conversation

@harikrishnan94

@harikrishnan94 harikrishnan94 commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Related: #118129

EXPLAIN AST optimize = 1 inlines the definition of a view the user can SELECT from and dumps the result through DumpASTNode. ASTLiteral::getID embeds the literal value, so nothing masks it. A user with only GRANT SELECT on a SQL SECURITY DEFINER view reads the definer's encrypt key in clear text, while SHOW CREATE VIEW prints '[HIDDEN]' for the same user. Reproduces at defaults. #118129 fixed the plan, pipeline, query tree and syntax legs and did not touch the AST branch.

The dump cannot mask while formatting the way ASTFunction::formatImpl does, so the tree is edited before the dump: every literal inside a secret argument found by FunctionSecretArgumentsFinderAST becomes a '[HIDDEN]' literal, structure kept. An encrypt key built as leftPad('...', 16, '*') shows as leftPad('[HIDDEN]', '[HIDDEN]', '[HIDDEN]'). A named argument keeps its key, a positional one is hidden whole, and a nested map (headers(...), extra_credentials(...)) hides all of its values. The gate is canDisplaySecrets, same as SHOW CREATE. Applied for optimize = 0 and 1 and for graph = 1.

Masking depends only on the gate, so EXPLAIN AST of the user's own encrypt(...) prints '[HIDDEN]' now as well. That matches EXPLAIN QUERY TREE run_passes = 0 and system.query_log.

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

EXPLAIN AST printed secret function arguments, such as the key of encrypt, in clear text, and with optimize = 1 it did so for the body of a view the user may only SELECT from. The dump now prints '[HIDDEN]' in their place, under the same gate as SHOW CREATE.


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

Version info

  • Merged into: 26.10.1.960-master (included in 26.10 and later)
  • Backported to: 26.9.9.22, 26.8.16.26, 26.7.20.15, 26.3.39.11

`EXPLAIN AST optimize = 1` inlines the definition of a view the user may `SELECT` from and dumps
the result through `DumpASTNode`, which prints every literal verbatim. A user holding only
`SELECT` on a `SQL SECURITY DEFINER` view could therefore read the definer's `encrypt` key that
`SHOW CREATE VIEW` masks for the same user. The secret arguments found by
`FunctionSecretArgumentsFinderAST` are now replaced with `'[HIDDEN]'` literals before the dump,
under the same gate as `SHOW CREATE`, for the tree and the `graph = 1` output alike and for
`optimize = 0` as well.

Closes: ClickHouse/clickhouse-private#72669
Related: ClickHouse#118129
The visitor and call-site comments now say in short sentences what the dump cannot do and why the
tree is edited instead. The test header reads the same way. No behaviour change.
The dump looked for a `key = value` pair inside every secret argument and kept the key visible.
`SHOW CREATE` does that only for a named argument and hides a positional one whole. A positional
secret written as a comparison, such as `encrypt('aes-128-ecb', 'plain', 'k1' = 'k2')`, therefore
showed its left operand. The dump now peels the value only when the finder reports the argument
as named, or when it is an entry of a nested map such as `headers(...)`.
@harikrishnan94 harikrishnan94 added comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Sep 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7d2aa10]

Summary: ✅


AI Review

Summary

This PR correctly moves EXPLAIN AST closer to the SHOW CREATE secret gate by rewriting secret function arguments before dumpAST, and the new fail-closed cases for unreadable URLs and comparisons are the right direction. The remaining problem is completeness: the redaction pass only touches ASTFunction, while EXPLAIN AST can wrap any statement and dumpAST bypasses every other node's show_secrets formatting, so plaintext secrets still leak through non-function AST carriers.

Findings

❌ Blockers

  • [dismissed by author -- https://github.com/Hide secret arguments in EXPLAIN AST output #120313#discussion_r4072087782] [src/Interpreters/InterpreterExplainQuery.cpp:423-489, 1100-1109] The new canDisplaySecrets branch only rewrites ASTFunction, but EXPLAIN AST is parsed by the full ParserQuery and then dumped via dumpAST, which ignores formatImpl/show_secrets for every other AST node. That still leaves plaintext leaks in secret-bearing non-function carriers such as ASTSetQuery (SETTINGS ...), ASTAuthenticationData (CREATE/ALTER USER ... IDENTIFIED BY ...), named collections, and dictionary ASTPair values even though those nodes already hide the same secrets in normal formatting. Suggested fix: make the pre-dump rewrite generic for all secret-bearing AST nodes, or extend it to the non-function carriers instead of stopping at ASTFunction.
Final Verdict

Changes requested: the AST redaction is still incomplete, so EXPLAIN AST can bypass the same secret gate it is trying to honor on non-function nodes.

LLVM Coverage Report

Measured on commit 7d2aa10.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.50% -0.10%

Changed lines: Changed C/C++ lines covered: 45/74 (60.81%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 16, 2026
@Fgrtue Fgrtue self-assigned this Sep 16, 2026
@Fgrtue Fgrtue added the can be tested Allows running workflows for external contributors label Sep 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7d2aa109e with master 026dca38e (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 7264abe5d; 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.

Object file sizes

9 object files changed (+22.28 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/InterpreterExplainQuery.cpp.o 521.09 KiB 541.57 KiB +20.48 KiB (+3.93%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

@Fgrtue Fgrtue left a comment

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.

I guess this is one real concern that worth adding: #120313 (comment)

This would be consistent with how EXPLAIN SYNTAX does it.

Other than that looks good.

alexey-milovidov and others added 2 commits September 19, 2026 18:02
`HideSecretArgumentsMatcher` ran `hideLiteralsInSubtree` on every secret slot, so a slot that
`markSecretArgument` marks because the finder cannot read it (a url built by `concat(...)`, an
identifier in a password slot, a positional secret written as a comparison) still dumped its
expression node by node: `Function concat`, `Function equals`, `Identifier ...`. That is part of a
`SQL SECURITY DEFINER` view body.

Such a slot is now one `'[HIDDEN]'` literal, as `ASTFunction::formatImpl` prints it. A partially
masked argument (a URL with its credentials removed, the reconstructed `S3(...)` of a `Backup`
database) is re-parsed from the formatter text instead of walked. `hideLiteralsInSubtree` is kept
only for the `encrypt` / `HMAC` key, where the expression shape is not a secret.

The test prints the full AST for the fail-closed shapes so the collapse to a single node is visible.

Review: ClickHouse#120313 (comment)
@CLAassistant

CLAassistant commented Sep 22, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Keep `EXPLAIN AST` secret masking and the upstream removal of obsolete plan-secret checks.

Co-authored-by: Copilot App <[email protected]>
@harikrishnan94
harikrishnan94 force-pushed the explain-ast-hide-secrets branch from 98fbe80 to dd27abc Compare September 22, 2026 12:48

@Fgrtue Fgrtue left a comment

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.

Looks good to me!

The upgrade check failed on the old base because master has since disabled the AI function quotas by default, so the previous release and this branch disagreed on the defaults of `ai_function_max_*_per_query`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings September 28, 2026 07:35

Copilot AI left a comment

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.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

Review effort: Lite
Findings: 4 Medium severity

Open (4)
What changed in this PR

Prevent EXPLAIN AST from leaking secrets (e.g., encrypt keys) by masking secret function arguments in the AST dump under the same canDisplaySecrets gate used by SHOW CREATE.

Changes:

  • Add an AST visitor in InterpreterExplainQuery that rewrites secret argument subtrees/literals to '[HIDDEN]' before dumping.
  • Parse and replace certain secret arguments with pre-masked textual forms (e.g., URL credentials stripping) and fail-closed to '[HIDDEN]' if parsing fails.
  • Add a stateless regression test validating masking for optimized AST, graph output, inlined view bodies, URL cases, and nested maps.
File Description
src/​Interpreters/​InterpreterExplainQuery.cpp Masks secret arguments by mutating the AST before DumpASTNode/graph dumping when secrets cannot be displayed.
tests/​queries/​0_stateless/​05219_explain_ast_hide_secrets.sh Adds a regression test scenario covering view inlining and secret masking behavior in multiple EXPLAIN AST modes.
tests/​queries/​0_stateless/​05219_explain_ast_hide_secrets.reference Captures expected masked AST output ('[HIDDEN]') for the new regression test.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

harikrishnan94 and others added 2 commits September 28, 2026 13:18
…t` / `HMAC`

The visitor treated every unnamed secret span without a replacement as the
`encrypt` / `HMAC` span and hid only the literals inside it. `mongodb` with
two arguments produces the same shape when the finder cannot read the url,
so `EXPLAIN AST` of `mongodb(concat('mongodb://user:', password_column, '@host/db'), 'c')`
still dumped `Function concat` and `Identifier password_column`.
`SHOW CREATE` prints `'[HIDDEN]'` for the whole argument there.

Keep the structure only for the functions the finder masks as
`encrypt` / `HMAC`, and hide any other such slot whole.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@harikrishnan94
harikrishnan94 added this pull request to the merge queue Sep 29, 2026
Merged via the queue into ClickHouse:master with commit 0a7853b Sep 29, 2026
294 of 295 checks passed
@harikrishnan94
harikrishnan94 deleted the explain-ast-hide-secrets branch September 29, 2026 04:52
@clickgapai

Copy link
Copy Markdown
Contributor

@harikrishnan94 @Fgrtue — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 1, 2026
…he sealed one

A `SQL SECURITY DEFINER` view over a Memory table is sealed, so the legacy rewrite
behind `EXPLAIN AST optimize = 1` no longer inlines its body and the reference from
PR ClickHouse#120313 stopped matching. The test table becomes a `MergeTree`, which keeps the
view transparent and the masking of the inlined body under test, and a second view
with a WHERE pins that a sealed view is printed as a `TableIdentifier`.

CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122330&sha=7d01b1f68237e134cc8ca7d650d4f2df49915d4b&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29

Co-Authored-By: Claude Fable 5.1 <[email protected]>
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Cherry pick #120313 to 26.3: Hide secret arguments in `EXPLAIN AST` output
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Cherry pick #120313 to 26.7: Hide secret arguments in `EXPLAIN AST` output
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Cherry pick #120313 to 26.8: Hide secret arguments in `EXPLAIN AST` output
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Cherry pick #120313 to 26.9: Hide secret arguments in `EXPLAIN AST` output
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 2, 2026
Backport #120313 to 26.7: Hide secret arguments in `EXPLAIN AST` output
clickhouse-gh Bot added a commit that referenced this pull request Oct 2, 2026
Backport #120313 to 26.8: Hide secret arguments in `EXPLAIN AST` output
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Backport #120313 to 26.3: Hide secret arguments in `EXPLAIN AST` output
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Backport #120313 to 26.9: Hide secret arguments in `EXPLAIN AST` output
PedroTadim added a commit that referenced this pull request Oct 3, 2026
The prerequisite #120313 (`EXPLAIN AST` secret hiding) reached 26.3 through
#123453 after this cherry-pick was created, so merge 26.3 first. The only
remaining conflict is the include list: keep 26.3's includes and add
`Parsers/ASTWithAlias.h`, which the fix needs. `ASTSubquery.h` and
`ASTTablesInSelectQuery.h` are master-only context and are not needed here.

Backport of #122870

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

can be tested Allows running workflows for external contributors comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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.

9 participants