Repository navigation
Hide secret function arguments in EXPLAIN when the value comes from another plan step - #120371
Conversation
…utput `is_masked_secret` is set by the planner only on the constant nodes it sees while building a function's arguments. When the secret argument of `HMAC`, `encrypt` etc. is a column of a derived table or of the opposite side of a JOIN, the argument is an INPUT node at planning time; `mergeExpressions` (via `deduplicateSubtrees`) or the join filter push-down (via `inlineConstantInputs`) later bind it to an unflagged constant, and `QueryPlanFormat::formatConstant` printed its value in cleartext. A user with only `SELECT` on a `SQL SECURITY DEFINER` view could read a decrypted key out of `EXPLAIN PLAN actions = 1`. Re-derive the secret argument positions in `formatNodePretty` from the function node itself with `FunctionSecretArgumentsFinderActionsDAG`, and render everything inside such a slot fail-closed: any node carrying a value is `[HIDDEN]`, a column reference shows its name only (never the expression a child step computes it from), nested functions keep their structure. Related: ClickHouse/clickhouse-private#72525 Co-Authored-By: Claude Fable 5.1 <[email protected]>
The analyzer folds `decrypt('mode', ciphertext, key)` with constant
arguments into a `ConstantNode` holding the plaintext. The arguments are
masked, but the folded result was not, so the planner named the action
node after its value (`_CAST('plaintext'_String, 'String'_String)`) and
EXPLAIN showed it: in the legacy `actions = 1` dump as the node name, in
the pretty dump as the constant value, and in `EXPLAIN QUERY TREE` as the
constant. A `SQL SECURITY DEFINER` view that only uses the decrypted value
internally leaked it to any user with `SELECT` on the view.
Mask the folded result whenever the function has secret arguments and
secrets are hidden, so it is named and displayed as `[HIDDEN id: N]` like
the arguments it was derived from. Execution is unaffected.
Related: ClickHouse/clickhouse-private#72525
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Review feedback: the flag only matters inside the recursion, so it lives on a static `formatNodePrettyImpl`, and the declared `formatNodePretty` keeps its original signature and forwards with `false`. Also document why the flag is inherited by the whole subtree of a secret argument. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…nition The declaration moved into the .cpp kept the old five-argument signature, so it was a second, undefined overload. Declare the single function with `in_secret_slot = false` as default. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…aliases A masked constant leaves its plan step as a header column, which carries only name, type and value, so the node the next step rebuilds from it had no mask. `Project names` then aliased it to the user's name and `removeUnusedActions` folded the alias into a plain COLUMN, and EXPLAIN printed the plaintext in `Output:` for `SELECT decrypt(...) AS k`. - `addInput(ColumnWithTypeAndName)` and the `duplicate_const_columns` constructor restore the mask from the placeholder name the planner gives every masked constant (`isMaskedSecretName`). - `createAlias` inherits the mask; `project` and `addAliases` build their aliases through it instead of by hand. - A function folded to a constant inherits the mask from its children. Only one hop is covered: once an alias renamed the constant, the header name no longer carries the placeholder, so a projected secret behind a subquery or JOIN still shows in `Output:`. Carrying the mask in `ColumnWithTypeAndName` itself would close that. Related: ClickHouse/clickhouse-private#72525 Co-Authored-By: Claude Fable 5.1 <[email protected]>
…n their aliases" This reverts commit b53356d.
This reverts commit 39ddc1c.
`05213` gains the report's exact JOIN shape (the `HMAC` key is
`decrypt('aes-128-cbc', unhex(...), key, iv)` on the opposite side) and
its `WITH` alias control.
`05218` reproduces the privilege scenario: the four views from the report
are created by a definer user, a second user holding only `SELECT` on them
runs every EXPLAIN flavour and must not see the plaintext, `SHOW CREATE`
keeps hiding the arguments, `SELECT` stays usable, and a user without the
grant gets `ACCESS_DENIED`. Only leak counts and the presence of the
`[HIDDEN]` placeholder go into the reference, so it does not depend on the
plan layout.
Related: ClickHouse/clickhouse-private#72525
Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [1f071a4] Summary: ✅
AI ReviewSummaryThis PR hardens pretty Findings
Tests
Final Verdict
LLVM Coverage ReportMeasured on commit 1f071a4.
Changed lines: Changed C/C++ lines covered: 174/176 (98.86%) · Uncovered code |
The pretty formatter hid the values in secret argument positions unconditionally, while the legacy format, `SHOW CREATE` and `EXPLAIN SYNTAX` follow `canDisplaySecrets`. Thread a `show_secrets` option from `InterpreterExplainQuery` into `buildPrettyNamesPerPlan`; every other caller keeps the default and hides. The test uses clickhouse-local, since the stateless server does not enable `display_secrets_in_show_and_select`. Co-Authored-By: Claude Fable 5.1 <[email protected]>
| auto result = std::make_unique<AnalyzedInnerQuery>(); | ||
|
|
||
| result->query_plan_options = checkAndGetSettings<QueryAnalyzeSettings>(ast.getSettings()).query_plan_options; | ||
| result->query_plan_options.show_secrets = canDisplaySecrets(getContext()); |
There was a problem hiding this comment.
getAnalyzedInnerQuery snapshots show_secrets from getContext() here, before the EXPLAIN query's own SETTINGS are applied to planning_context above. That makes the new pretty-plan behavior diverge by entrypoint: EXPLAIN PLAN ... SETTINGS format_display_secrets_in_show_and_select = 1 uses the updated query_context and shows secrets, but EXPLAIN ANALYZE reuses this cached false and still builds precomputed_pretty_names with [HIDDEN] (and the reverse SETTINGS ... = 0 override is ignored too).
Please derive this from planning_context after InterpreterSetQuery::applySettingsFromQuery, or from result->context once the interpreter is built, and add coverage for the query-local SETTINGS path in EXPLAIN ANALYZE. The current 05219 test only exercises the session-level clickhouse-local flag, so it would not catch this mismatch.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes72 object files changed (+131.25 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units304 translation units recompiled, 2578 s compile time in total, 304 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.07 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
`EVP_MD_fetch` returns a reference-counted handle that `FunctionHMAC::executeImpl` never released. Under ASan the leaked handle keeps the OpenSSL provider state alive past `OPENSSL_cleanup`, and LeakSanitizer prints a "Suppressions used" summary at exit of every `clickhouse-local` process that executed `HMAC`, including the empty-block execution done by `EXPLAIN`. The stateless harness treats that summary as stderr output, which failed `05219_explain_show_secrets_with_privilege` in the ASan flaky check. CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120371&sha=39f9c0d2399f78e7f651b864d7d0fafcb025c645&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29 PR: #120371 Co-Authored-By: Claude Fable 5.1 <[email protected]>
Fgrtue
left a comment
There was a problem hiding this comment.
Since in the future we plan to add more printers for EXPLAIN, I would suggest we find a place in the code, somewhere after the optimizations of the plan, where we check all the nodes of the plan, and mark the ones that have secrets as secrets. This would allow us to hide the secrets uniformly.
| static std::vector<bool> getSecretArgumentSlots(const ActionsDAG::Node & function) | ||
| { | ||
| std::vector<bool> slots(function.children.size(), false); | ||
| auto secret_arguments = FunctionSecretArgumentsFinderActionsDAG(function).getResult(); | ||
| if (!secret_arguments.hasSecrets()) | ||
| return slots; | ||
|
|
||
| for (size_t i = secret_arguments.start; i < secret_arguments.start + secret_arguments.count && i < slots.size(); ++i) | ||
| slots[i] = true; | ||
| for (const auto & [index, _] : secret_arguments.masked_arguments) | ||
| if (index < slots.size()) | ||
| slots[index] = true; | ||
| for (const auto & [index, _] : secret_arguments.replaced_arguments) | ||
| if (index < slots.size()) | ||
| slots[index] = true; | ||
| return slots; | ||
| } |
There was a problem hiding this comment.
This logic decides which nodes of a DAG are secret, and it is placed inside one printer. A future printer of DAG values would have to copy it, and would leak until someone remembers to.
I think we should have a dedicated method next to ActionsDAG so we can reuse the methods at least.
Something like:
ActionsDAG::markSecretArguments(), that setsis_masked_secreton those nodes. Run once at the end ofQueryPlan::optimizewhenis_explainis set.
Then the printers only check the flag, and the flag-based and slot-based paths collapse into one.
hasSecretsInActionsDAG in InterpreterExplainQuery.cpp:338 could then also use the same helper instead of a third copy of the walk.
| for (size_t i = secret_arguments.start; i < secret_arguments.start + secret_arguments.count && i < slots.size(); ++i) | ||
| slots[i] = true; | ||
| for (const auto & [index, _] : secret_arguments.masked_arguments) | ||
| if (index < slots.size()) | ||
| slots[index] = true; | ||
| for (const auto & [index, _] : secret_arguments.replaced_arguments) | ||
| if (index < slots.size()) | ||
| slots[index] = true; | ||
| return slots; | ||
| } |
There was a problem hiding this comment.
This is the third copy of "is argument n secret?": the same loop over start/count, masked_arguments and replaced_arguments exists in markFoldedSecretConstants
(PlannerActionsVisitor.cpp:1246) and in forEachSecretArgumentNode (FunctionSecretArgumentsFinderTreeNode.cpp:44).
Result owns this knowledge, so the check belongs on it. One method in Parsers/FunctionSecretArgumentsFinder.h:
bool isSecretArgument(size_t n) const
{
return (start <= n && n < start + count)
|| masked_arguments.contains(n)
|| replaced_arguments.contains(n);
}Then all three callers become one call each. forEachSecretArgumentNode keeps its nested_maps branch, since only table functions have those. If a fourth kind of masking is ever added to Result, it is fixed in one place.
| { | ||
| ShowAll, | ||
| HideSecrets, | ||
| InSecretSlot, |
There was a problem hiding this comment.
This allows us to hide arguments withing a function. I.e. it improves the granularity of what we are actually hiding and what we don't. I am thinking -- why don't we just fail closed, if the argument of the function (like HMAC or similar) is a secret? I think this would be safer.
…n it themselves The plan of a `SQL SECURITY DEFINER` or `NONE` view runs with the view's privileges and can hold values folded from data the invoker cannot read: a scalar subquery over a private table, a key decrypted on the other side of a JOIN. `EXPLAIN` printed them to any user with `SELECT` on the view. `ReadFromSealedViewStep` is now opaque in `EXPLAIN` unless the user could have created the same view (the definer, a user with `SET DEFINER` on the definer, a user with `ALLOW SQL SECURITY NONE` for a `NONE` view) or may display secrets (the server setting `display_secrets_in_show_and_select`, the format setting and the `displaySecretsInShowAndSelect` privilege). The child plan is hidden from the plan and pipeline dumps only; the optimizations keep seeing it through `getChildPlans`. In the hidden case the inner processors are attributed to the sealed step, so `EXPLAIN PIPELINE graph = 1` and `system.processors_profile_log` do not show the inner steps either. Related: ClickHouse/clickhouse-private#72525 Related: ClickHouse#120371 Co-Authored-By: Claude Fable 5.1 <[email protected]>
# Conflicts: # src/Interpreters/InterpreterExplainQuery.cpp
A secret argument position (the key of `HMAC`, everything after the mode of `encrypt`) is
rendered as a single `[HIDDEN]` instead of its structure with the constants hidden, as the
AST formatter does for `SHOW CREATE`. Rendering the structure had to catch every place a
value can surface, and the `IN` operator formatted its literal set directly, so
`HMAC('sha256', msg, if(x IN ('a', 'b'), k1, k2))` printed the set elements in cleartext.
The `InSecretSlot` rendering state is gone. `05213` pins the whole-slot rendering for a
column reference and for the `encrypt` span and adds the `IN` set shape; `05057` changes
accordingly, the plaintext column of `encrypt` is hidden together with the key.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
The check "is argument `n` hidden" over the span, `masked_arguments` and `replaced_arguments` was written three times: in the pretty plan formatter, in `markFoldedSecretConstants` of the planner and in `forEachSecretArgumentNode` of the query tree. `Result` owns that knowledge, so the three callers become one call each, and a new kind of masking added to `Result` is handled in one place. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Resolve the conflicts of the cherry-pick of #120371 (`InterpreterExplainQuery.cpp`): the EXPLAIN options `time` do not exist in 26.9, so only `show_secrets` is added next to the release's fields. `BlockNestedLoopJoinStep.cpp` does not exist in 26.9 and is dropped. The sealed-view check of `05218_explain_hide_secrets_definer_view_join` is removed: `ReadFromSealedView` does not exist in 26.9, so the plan of a `SQL SECURITY DEFINER` view is shown and the test only asserts that no secret reaches the output. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Resolve the conflicts of the cherry-pick of #120371 (`InterpreterExplainQuery.cpp`, `QueryPlan.cpp`, `QueryPlanFormat.h`): the EXPLAIN options `inside_explain_analyze` do not exist in 26.8, so only `show_secrets` is added next to the release's fields. `BlockNestedLoopJoinStep.cpp` does not exist in 26.8 and is dropped. The sealed-view check of `05218_explain_hide_secrets_definer_view_join` is removed: `ReadFromSealedView` does not exist in 26.8, so the plan of a `SQL SECURITY DEFINER` view is shown and the test only asserts that no secret reaches the output. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Resolve the conflicts of the cherry-pick of #120371 (`InterpreterExplainQuery.cpp`, `QueryPlan.cpp`, `QueryPlan.h`, `QueryPlanFormat.h`): the EXPLAIN options `inside_explain_analyze`, `estimates`, `time` and `matches` do not exist in 26.7, so only `show_secrets` is added next to the release's fields. `BlockNestedLoopJoinStep.cpp` does not exist in 26.7 and is dropped. The sealed-view check of `05218_explain_hide_secrets_definer_view_join` is removed: `ReadFromSealedView` does not exist in 26.7, so the plan of a `SQL SECURITY DEFINER` view is shown and the test only asserts that no secret reaches the output. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Cherry pick #120371 to 26.7: Hide secret function arguments in EXPLAIN when the value comes from another plan step
…hen the value comes from another plan step
Cherry pick #120371 to 26.8: Hide secret function arguments in EXPLAIN when the value comes from another plan step
…hen the value comes from another plan step
Cherry pick #120371 to 26.9: Hide secret function arguments in EXPLAIN when the value comes from another plan step
…hen the value comes from another plan step
Edited by the nightly changelog CI job following .claude/skills/edit-changelog/SKILL.md. Changelog-revert: 119270 115343 Revert "Resume a NATS JetStream subscription after a reconnect" Changelog-revert: 119301 119270 Revert "Revert "Resume a NATS JetStream subscription after a reconnect"" Changelog-revert: 121075 120923 Revert "Revert "Join reordering: FK->PK heuristic for equi joins without NDV statistics"" Changelog-revert: 124272 120371 Revert "Hide secret function arguments in EXPLAIN when the value comes from another plan step" Changelog-deleted-entry: 120371 [Bug Fix (user-visible misbehavior in an official stable release)] [section] * Fix `EXPLAIN PLAN actions = 1` printing a secret function argument (for example the key of `HMAC` or `encrypt`) in cleartext when its value came from a subquery or from the other side of a `JOIN`. [#120371](#120371) ([Vladimir Cherkasov](https://github.com/vdimir)).
Backport #120371 to 26.9: Hide secret function arguments in EXPLAIN when the value comes from another plan step
Backport #120371 to 26.8: Hide secret function arguments in EXPLAIN when the value comes from another plan step
Backport #120371 to 26.7: Hide secret function arguments in EXPLAIN when the value comes from another plan step
EXPLAIN PLAN actions = 1masks the secret arguments of functions likeHMAC,encryptanddecryptby flagging the constant nodes the planner sees while building the function. When the secret argument is a column of a derived table or of the opposite side of a JOIN, the argument is anINPUTnode at planning time, and later optimizations (mergeExpressionsviadeduplicateSubtrees, the join filter push-down viainlineConstantInputs) bind it to a fresh, unflagged constant. The pretty formatter then printed its value in cleartext. A non-constant argument in the same position was rendered through the expression its child step computed it from, which could contain the secret as well.The pretty formatter now re-derives the secret argument positions from the function node itself, using the existing
FunctionSecretArgumentsFinderActionsDAG, and renders everything inside such a position fail-closed: any node carrying a value prints[HIDDEN], a column reference prints its name only, nested expressions keep their structure. This does not depend on which optimization rewrote the node. Theis_masked_secretflag keeps working as before for constants the planner flags directly.Scope: only the secret argument positions are masked. The result of a function like
decryptis treated as an ordinary computed value and is still displayed, in line withSHOW CREATEwhich hides the arguments only.Tests:
05213_explain_hide_secrets_across_stepscovers the derived-table, JOIN-side constant, JOIN-side column and nested-expression shapes withviewExplainassertions.05218_explain_hide_secrets_definer_view_join.shreproduces the reported scenario verbatim:SQL SECURITY DEFINERviews whoseHMACkey is adecryptresult computed on the other side of a JOIN or in a derived table, queried by a user holding onlySELECTon the view, across everyEXPLAINflavour.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
EXPLAIN PLAN actions = 1printing a secret function argument (for example the key ofHMACorencrypt) in cleartext when its value came from a subquery or from the other side of a JOIN.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/120371]
Version info
26.10.1.1666-master(included in26.10and later)26.9.13.16,26.8.22.6,26.7.26.4