Skip to content

Fix secret bug in explain - #118129

Merged
Fgrtue merged 7 commits into
masterfrom
fix-security-bug-in-explain-actions
Sep 10, 2026
Merged

Fgrtue merged 7 commits into
masterfrom
fix-security-bug-in-explain-actions

Conversation

@Fgrtue

@Fgrtue Fgrtue commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes: https://github.com/ClickHouse/clickhouse-private/issues/69267

Closes: https://github.com/ClickHouse/clickhouse-private/issues/69309

Closes: https://github.com/ClickHouse/clickhouse-private/issues/69186

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)
  1. format_display_secrets_in_show_and_select as a setting requires a server setting and a permission to be used https://clickhouse.com/docs/reference/settings/formats/format#format_display_secrets_in_show_and_select in order to see the secrets.

In EXPLAIN the check was only for the setting format_display_secrets_in_show_and_select. Now this is fixed.

  1. In old analyzer when we print EXPLAIN actions=1 we reveal some of the secrets. Now we traverse the actionsDAG in the appropriate place in order to eliminate the chance of leaking a secret. Users that don't have right permissions are restricted in their ability to run EXPLAIN [PLAN | ANALYZE] when they don't have a right permission AND actionsDAG has a secret AND they use old analyzer.

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

format_display_secrets_in_show_and_select works as expected in EXPLAIN.


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

Version info

  • Merged into: 26.9.1.1111 (included in 26.9 and later)
  • Backported to: 26.8.4.3, 26.7.7.85, 26.6.7.12, 26.3.33.83

@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4b0e7d2]

Summary: ✅


AI Review

Summary

This PR tightens EXPLAIN secret handling by switching to the full displaySecretsInShowAndSelect gate and by rejecting some old-analyzer plans that carry secret function arguments. I cannot approve it yet because several old-analyzer EXPLAIN carriers are still either leaking secrets or over-redacting them, so the promised behavior is still incomplete.

PR Metadata
  • The Changelog category should be Critical Bug Fix (crash, data loss, RBAC), not Bug Fix: this fixes an authorization/secret-disclosure problem.

  • The Changelog entry is required, but the current text is too vague. Suggested replacement:

    EXPLAIN now respects the full displaySecretsInShowAndSelect permission gate. When the legacy analyzer cannot safely redact secret function arguments from EXPLAIN PLAN or EXPLAIN ANALYZE, it rejects the query instead of leaking the secret.

  • Replace the three Resolves: lines with Closes::

    Closes: https://github.com/ClickHouse/clickhouse-private/issues/69267

    Closes: https://github.com/ClickHouse/clickhouse-private/issues/69309

    Closes: https://github.com/ClickHouse/clickhouse-private/issues/69186

Missing context / blind spots
  • ⚠️ The current PR discussion exposed only the Build profile diff Praktika report for commit 4b0e7d26; I could not cross-check a full stateless/integration CI run from the PR discussion itself.
Findings

❌ Blockers

  • [src/Interpreters/InterpreterExplainQuery.cpp:995-1004] [dismissed by author -- https://github.com/Fix secret bug in explain #118129#discussion_r3936367131] EXPLAIN AST optimize = 1 still rewrites the query and then calls dumpAST / dumpASTInDotFormat without any secret redaction. For a SQL SECURITY DEFINER view this still exposes the inlined secret literal through optimized AST output. Suggested fix: apply the same deny-or-redact handling here that EXPLAIN SYNTAX uses, and add a focused negative regression for EXPLAIN AST optimize = 1.
  • [src/Interpreters/InterpreterExplainQuery.cpp:1108-1110] [dismissed by author -- https://github.com/Fix secret bug in explain #118129#discussion_r3936367138] throwIfPlanHasSecrets still ignores step.getStepDescription(), but old-analyzer remote read steps still embed formattedAST(query_ast, enable_analyzer) in their description (src/Processors/QueryPlan/ReadFromRemote.cpp:178-189, src/Processors/QueryPlan/ReadFromRemote.cpp:1079-1080). That leaves EXPLAIN PLAN / EXPLAIN able to disclose the secret through the step description even when none of the scanned DAG carriers survive. Suggested fix: either scan description carriers too, or build those descriptions with the same secret-display gate.
  • [src/Interpreters/InterpreterExplainQuery.cpp:777-808] [dismissed by author -- https://github.com/Fix secret bug in explain #118129#discussion_r3937178615] The masking pass for EXPLAIN SYNTAX still only sets mask_id on folded secret constants. ConstantNode::toASTImpl continues to prefer source_expression when use_source_expression_for_constants is enabled (src/Analyzer/ConstantNode.cpp:215-216), so a secret folded from a scalar subquery or similar source expression can still re-expand to its original literal in syntax output. Suggested fix: either recurse into getSourceExpression() during masking, or suppress source-expression rendering for masked constants.
  • [src/Interpreters/ActionsVisitor.cpp:776-781] [dismissed by author -- https://github.com/Fix secret bug in explain #118129#discussion_r3968295549] The old-analyzer path now marks secret-bearing constant function nodes as is_masked_secret unconditionally. QueryPlanFormat::formatNodePretty always renders such nodes as [HIDDEN] (src/Processors/QueryPlan/QueryPlanFormat.cpp:209-210, src/Processors/QueryPlan/QueryPlanFormat.cpp:328-329), so a user who does pass the full canDisplaySecrets() gate still loses the real expression in pretty EXPLAIN PLAN / EXPLAIN ANALYZE. Suggested fix: propagate that marker only when secrets must be hidden, or make pretty formatting ignore it for authorized users.
Tests
  • ⚠️ Add a negative stateless regression for EXPLAIN AST optimize = 1 on a SQL SECURITY DEFINER view with a secret literal; the new test covers SYNTAX / PLAN / PIPELINE, but not the still-leaking parsed-AST path.
  • ⚠️ Add a regression where a secret argument is folded into a ConstantNode with a source_expression (for example via a scalar subquery) and then shown through EXPLAIN SYNTAX.
  • ⚠️ Add a positive old-analyzer regression for a user that is allowed to display secrets, to prove pretty EXPLAIN PLAN / EXPLAIN ANALYZE still show the real expression when authorized.
Final Verdict

❌ Changes requested.

LLVM Coverage Report

Measured on commit 4b0e7d2.

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: 98/141 (69.50%) · 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 4, 2026
@Fgrtue Fgrtue changed the title Fix secret bug in explain actions Fix secret bug in explain Sep 4, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 4, 2026
…now hides disk secrets

`EXPLAIN SYNTAX` now honours `canDisplaySecretsInShowAndSelect`, so the `disk`
function arguments are printed as `[HIDDEN]`, the same as in `SHOW CREATE TABLE`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…r reveals secrets

`Context::canDisplaySecretsInShowAndSelect` requires the `display_secrets_in_show_and_select`
server setting and the `displaySecretsInShowAndSelect` privilege in addition to the
`format_display_secrets_in_show_and_select` session setting. The stateless test server has
neither, so `EXPLAIN QUERY TREE` and `EXPLAIN actions = 1` keep secrets hidden after the
`SET`. The references still encoded the old analyzer behaviour that honoured the session
setting alone.

CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118129&sha=latest&name_0=PR
PR: #118129

Co-Authored-By: Claude Fable 5.1 <[email protected]>
/// redaction (which may replace a non-constant secret value with a hidden constant) can never
/// change how the query is analyzed. With run_passes = 0 the tree is dumped without analysis.
if (!query_context->getSettingsRef()[Setting::format_display_secrets_in_show_and_select])
if (!query_context->canDisplaySecretsInShowAndSelect())

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.

This still leaks through EXPLAIN SYNTAX when a secret argument has already been folded into a ConstantNode with a source_expression. Scalar subqueries definitely take that path in evaluateScalarSubqueryIfNeeded.cpp:406-419: the secret becomes ConstantNode(constant_value, node).

The dump visitor run here only masks the folded constant itself. Later, EXPLAIN SYNTAX rebuilds AST with use_source_expression_for_constants = true, and ConstantNode::toASTImpl returns source_expression->toAST(options) before looking at isMasked() (ConstantNode.cpp:215-216). So a query like EXPLAIN SYNTAX SELECT encrypt('aes-256-ofb', (SELECT 'qwerty'), '12345678901234567890123456789012') will still print the subquery literal even though the user lacks displaySecretsInShowAndSelect.

The masking pass needs to recurse into ConstantNode::getSourceExpression() as well, or masked constants must suppress source-expression rendering. Please add a focused EXPLAIN SYNTAX regression for a scalar-subquery or other folded secret argument.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. Essentially, this concern is senseless -- if the user themselves run query EXPLAIN SYNTAX SELECT encrypt('aes-256-ofb', (SELECT 'qwerty'), '12345678901234567890123456789012'), then it is not so important if we show the argument as hidden or not.
  2. If you run this query by user, then as an output you will get SELECT encrypt('aes-256-ofb', '[HIDDEN]'). It happens with turned on/off analyzer and with turned on format_display_secrets_in_show_and_select.

TL;DR this is a non-existing concern.

@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4b0e7d26b with master b666e5df6 (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.

Object file sizes

6 object files changed (+56.77 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/ActionsVisitor.cpp.o 910.96 KiB 941.91 KiB +30.94 KiB (+3.40%)
src/CMakeFiles/dbms.dir/Interpreters/InterpreterExplainQuery.cpp.o 514.71 KiB 540.64 KiB +25.92 KiB (+5.04%)

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

63 translation units recompiled, 135 s compile time in total, 60 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.11 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +8.2 s (+7%) in total before that adjustment.

Job report

@diegomestre2 diegomestre2 self-assigned this Sep 7, 2026

@diegomestre2 diegomestre2 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.

Great work on fixing the issues, I left a few comments. The main issue for me is the exception message, besides that, the rest of the code looks clear and easy to follow.

@Fgrtue

Fgrtue commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

@diegomestre2 thank you for the review! I applied the changes that you requested. Could you take a look if they are fine please?

@diegomestre2 diegomestre2 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.

No new remarks from my side, great work on adding the pass over the DAG and checking the user's access rights. I spent some time thinking about the function bool hasSecretsInStep(const IQueryPlanStep & step), trying to simplify it but I think the only way would require to have virtual calls to different points of the code, like a visitor pattern, having it centralized looks better for maintenance.

bool node_has_secret = FunctionSecretArgumentsFinderActionsDAG(node).getResult().hasSecrets();
bool child_has_secret = std::any_of(
node.children.begin(), node.children.end(), [](const ActionsDAG::Node * child) { return child->is_masked_secret; });
mutable_node.is_masked_secret = node_has_secret || (node.column && child_has_secret);

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.

QueryPlanFormat::formatNodePretty hides every node with is_masked_secret, regardless of permissions. Because this new old-analyzer path sets that bit unconditionally for every secret-bearing FUNCTION node, a user who actually passes the full canDisplaySecrets() gate still gets [HIDDEN] in pretty EXPLAIN PLAN / EXPLAIN ANALYZE output instead of the real encrypt(...) / HMAC(...) expression. The analyzer path gates the same propagation on !canDisplaySecrets(), so the two modes now diverge.

Please make this marker propagation permission-aware here, or make the pretty formatter ignore is_masked_secret when secrets may be shown, and add a focused old-analyzer positive regression for the authorized case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is out of scope for this PR

@Fgrtue Fgrtue added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 9, 2026
Resolve conflict in `src/Processors/QueryPlan/ArrayJoinStep.h`: keep the
`master` accessors `getElementFilter` (returning `const std::optional<ActionsDAG> &`)
and `getElementFilterColumnName`; the secrets check in `InterpreterExplainQuery`
works unchanged with the optional-returning form.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@Fgrtue
Fgrtue added this pull request to the merge queue Sep 10, 2026
Merged via the queue into master with commit 6b7d98e Sep 10, 2026
175 checks passed
@Fgrtue
Fgrtue deleted the fix-security-bug-in-explain-actions branch September 10, 2026 09:14
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 10, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 10, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

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

Close anything that's wrong or already addressed.

@Fgrtue

Fgrtue commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

@clickgapai I am aware of those ones, fixing them is planned as well.

@clickgapai

Copy link
Copy Markdown
Contributor

@Fgrtue Thanks — noted. #119383 (EXPLAIN AST optimize=1 leaking view-body secrets) stays out of scope here; this PR covers the cases it resolves and the remaining exposure is tracked there.

Fgrtue added a commit that referenced this pull request Sep 11, 2026
Resolve the conflicts of the cherry-pick of #118129 to 26.7:

- `src/Interpreters/InterpreterExplainQuery.cpp`: drop both conflicting lines of the
  `Setting` block. `explain_syntax_single_record` does not exist on 26.7, and the
  original PR removes `format_display_secrets_in_show_and_select` in favour of
  `canDisplaySecrets`. Drop the `ArrayJoinStep` branch of `hasSecretsInStep` too:
  `ArrayJoinStep::getElementFilter` does not exist on 26.7, where `ARRAY JOIN` carries
  no expression DAG that could hold a secret.
- `tests/queries/0_stateless/02454_create_table_with_custom_disk.reference`: keep the
  multi-line `EXPLAIN SYNTAX` shape of 26.7 (master prints one record since
  `explain_syntax_single_record` defaults to 1) with the disk path shown as `[HIDDEN]`.

PR: #119178

Co-Authored-By: Claude Fable 5.1 <[email protected]>
robot-ch-test-poll3 added a commit that referenced this pull request Sep 11, 2026
Cherry pick #118129 to 26.7: Fix secret bug in explain
clickhouse-gh Bot added a commit that referenced this pull request Sep 11, 2026
Backport #118129 to 26.7: Fix secret bug in explain
clickhouse-gh Bot added a commit that referenced this pull request Sep 13, 2026
Backport #118129 to 26.8: Fix secret bug in explain
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Sep 14, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Sep 14, 2026
Cherry pick #118129 to 26.6: Fix secret bug in explain
Fgrtue added a commit that referenced this pull request Sep 14, 2026
The setting `explain_query_plan_default` and `EXPLAIN ANALYZE` were
introduced in 26.7 and do not exist in 26.6, so the test
`05057_explain_hide_secrets_without_privilege` failed with
`UNKNOWN_SETTING`. Remove the setting from the queries, drop the two
`EXPLAIN ANALYZE` checks, and regenerate the reference: on 26.6
`EXPLAIN PLAN` and `EXPLAIN SYNTAX` print the legacy format. Secrets are
still shown as `[HIDDEN]` everywhere and the key text never appears.

Also remove six includes in `InterpreterExplainQuery.cpp` that came from
the master side of the merge and are not used in 26.6.

Backport of #118129
Backport PR: #120003

Co-Authored-By: Claude Fable 5.1 <[email protected]>
robot-ch-test-poll added a commit that referenced this pull request Sep 14, 2026
Cherry pick #118129 to 26.3: Fix secret bug in explain
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 14, 2026
Backport #118129 to 26.6: Fix secret bug in explain
PedroTadim added a commit that referenced this pull request Sep 15, 2026
Backport #118129 to 26.3: Fix secret bug in explain
@clickhouse-gh clickhouse-gh Bot added comp-explain EXPLAIN and query profiling output: EXPLAIN PLAN / PIPELINE / AST / SYNTAX / ESTIMATE, pretty EXP... and removed comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-explain EXPLAIN and query profiling output: EXPLAIN PLAN / PIPELINE / AST / SYNTAX / ESTIMATE, pretty EXP... 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.

6 participants