Skip to content

Apply query masking to the plan stored in system.query_log.query_plan - #132477

Merged
Manerone merged 3 commits into
masterfrom
manerone-mask-logged-query-plan
Nov 24, 2026
Merged

Manerone merged 3 commits into
masterfrom
manerone-mask-logged-query-plan

Conversation

@Manerone

@Manerone Manerone commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Apply query_masking_rules and secret hiding to the plan stored in system.query_log.query_plan, so it is masked like the query column of the same row.

A step renders the literals of the query into the text it describes itself with: filter constants, IN lists, output constants, primary key conditions, and the input columns a join reports in its statistics. These strings are now masked one by one when the plan is captured, before they become values of the JSON document, so the keys and the structure of the document are never touched. The step description and details are masked before they are cut to query_plan_max_step_description_length, like the query column, which is masked before it is cut.

Some queries store no plan at all, because the plan could show what the query column hides in a form masking cannot catch:

  • a query with secret arguments (for example the key of encrypt): in the plan the secret is a constant inside an expression, possibly folded into derived values;
  • a query in which a masking rule matches the text or a query parameter of the session: constant folding can turn the masked literal into a value no rule matches, like lower of it.

The query itself is not affected, and the trace log says why the plan was not stored.

The stored plan also no longer names the replicas of distributed index analysis: it keeps what each replica contributed, without its address. EXPLAIN is unchanged.

Related: #123059

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/132477]

Version info

  • Merged into: 26.12.1.364-master (included in 26.12 and later)

Mask every text of the captured plan that can carry user data with
`query_masking_rules`, as values, before the JSON document is rendered:
step descriptions and details (before they are cut to length), output
column names, index and projection descriptions and conditions, string
statistics such as join input columns, and the error text.

Store no plan for a query in which the plan could show what the `query`
column hides in a form masking cannot catch: a query with secret
arguments, a query in which a masking rule matches its text or a query
parameter of the session, and a query in which a subquery is replaced by
its result. The last one is to be lifted once the plan is logged without
such values.

Related: #123059

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6c08e0c]

Summary: ✅


AI Review

Summary

The PR applies query_masking_rules to captured plan text, omits plans for queries detected as sensitive, and removes replica addresses from logged index statistics. Reviewed head: 6c08e0c33ecf4d3272a72a45a276de409c3cc54f. Two previously reported masking bypasses still reproduce on this head's Fast test binary. Both were explicitly deferred by the author; their threads remain untouched, and there are no new inline findings.

Findings

❌ Blockers

  • QueryPlanLogCapture.cpp:134: Expressions introduced after the eligibility check can disclose masked values. [dismissed by author -- https://github.com/Apply query masking to the plan stored in system.query_log.query_plan #132477#discussion_r4605617017] The guard checks submitted text and session parameters, but expressions from SQL UDFs, views, ALIAS columns, and additional_table_filters can subsequently introduce sensitive literals. With the test rule TOPSECRET.TOPSECRET, the following logs Filter column: toString(number) != 'find_me_topsecret=topsecret'; the equivalent direct query correctly stores {}:

    CREATE FUNCTION plan_mask_udf AS (x) ->
        notEquals(toString(x), lower('find_me_TOPSECRET=TOPSECRET'));
    SELECT count() FROM numbers(10) WHERE plan_mask_udf(number)
    SETTINGS log_query_plans = 1;

    The view, ALIAS, and session-filter cases were also reproduced; the latter two expose the transformed literal in index conditions as well. The author separately deferred the column/filter cases in Apply query masking to the plan stored in system.query_log.query_plan #132477 (comment). This remains real because folding changes the sensitive value before capture-time masking can recognize it. Propagate the decision to omit a plan through expanded expressions before folding, or exclude their values from the logged representation.

  • CapturedPlan.cpp:118: Masking happens after some descriptions have already been truncated. [dismissed by author -- https://github.com/Apply query masking to the plan stored in system.query_log.query_plan #132477#discussion_r4605617026] The ReadFromMergeTree constructor calls setStepDescription with the configured length limit, so getStepDescription can already contain only part of a sensitive match. In database review_132477, a populated MergeTree table named plan_meta_TOPSECRET=TOPSECRET, read through merge(currentDatabase(), '^plan_meta_') with query_plan_max_step_description_length = 30, logs "Description":"review_132477.plan_meta_TOPSEC". With the limit set to 500, it correctly logs review_132477.plan_meta_[hidden]. This remains real because the early cut exposes a sensitive prefix that the later masker cannot match. Apply masking before the first truncation, including at description producers.

Tests
  • ⚠️ Add focused stateless regressions for expanded sensitive expressions and for a table-name match crossing the first description-truncation boundary. The current 05817 test passes locally and matches all 10 reference rows, but does not exercise these bypasses.
Final Verdict

Status: ❌ Block. Close the two remaining masking bypasses and add the focused regressions before treating the logged plan as honoring the advertised masking guarantee.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 17, 2026
@Manerone
Manerone marked this pull request as ready for review November 17, 2026 20:25
@clickhouse-gh clickhouse-gh Bot added the comp-system-table system.* tables (introspection, system metadata, performance visibility). label Nov 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6c08e0c33 with master 42d692a43 (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

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

Job report

Revert the decline of plans for queries in which a subquery is replaced by
its result: `log_query_plans` is at an early stage, and the values in the
logged plan are to be handled separately. Such a value is still masked
like any other text of the plan.

The plan stored in `system.query_log.query_plan` no longer names the
replicas of distributed index analysis: capture clears the addresses and
the logged plan omits `Address`, keeping the per-replica counts. `EXPLAIN`
is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@diegomestre2 diegomestre2 self-assigned this Nov 18, 2026
Comment thread src/Interpreters/executeQuery.cpp Resolved

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

Looks good to me, I left a few comments that should be checked. Once it is done I can do another round!

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

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

LGTM.

# Conflicts:
#	src/Processors/QueryPlan/Profiling/Capture/CapturedPlan.cpp
@nickitat

Copy link
Copy Markdown
Member

🕵 Merged master into this branch and resolved the conflicts caused by #130369 (the switch to the -WithMemoryTracking containers). This update was made automatically.

@clickhouse-gh clickhouse-gh Bot added comp-explain EXPLAIN and query profiling output: EXPLAIN PLAN / PIPELINE / AST / SYNTAX / ESTIMATE, pretty EXP... and removed comp-system-table system.* tables (introspection, system metadata, performance visibility). labels Nov 20, 2026
@Manerone
Manerone added this pull request to the merge queue Nov 24, 2026
Merged via the queue into master with commit 1c69de5 Nov 24, 2026
200 checks passed
@Manerone
Manerone deleted the manerone-mask-logged-query-plan branch November 24, 2026 10:14
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 24, 2026
Manerone added a commit that referenced this pull request Nov 24, 2026
#132477 added the query text to
`QueryPlanLogCapture::canCapturePlan`. The conflict kept the constructor taking
`CollectedStatistics` and took master's signature of `canCapturePlan`.

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

4 participants