Skip to content

Read a SQL SECURITY DEFINER view transparently when its definer queries it - #130552

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:definer-view-read-by-definer
Nov 11, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:definer-view-read-by-definer

Conversation

@groeneai

@groeneai groeneai commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #130316
Related: #122330

Changelog category (leave one):

  • Performance Improvement

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

A SQL SECURITY DEFINER view queried by its own definer is no longer read as an opaque step, so the optimizations of the query apply to it again, as before 26.10 (for example, ORDER BY ... LIMIT reads in order through it). Other users, and sessions of the definer that are limited by the GRANTS of their authentication method or use other roles, still read such a view sealed, and such sessions of the definer no longer bypass the check that hides its plan in EXPLAIN.

Description

On 26.10, ORDER BY ... LIMIT over a SQL SECURITY DEFINER view that filters rows reads the whole table: 3,001,000 rows instead of 43,688 in #130316. Since #122330, every DEFINER / NONE view that can hide rows is read through the opaque ReadFromSealedView step, so read-in-order, top-N and the outer predicate for index analysis cannot see through it. The seal keeps hidden rows away from the invoker, but in the report the invoker is the definer.

StorageView::isReadByDefiner holds when the query runs with exactly the identity of the view's own query: an initial query of the definer (compared by UUID), with the definer's default roles (current and external), no GRANTS limit from the authentication method, and no row policy on the view itself. Such a query can read everything the view hides, so readImpl and the ORDER BY ... LIMIT pushdown into views no longer seal the view for it. The same check replaces the name-only definer shortcut that decides whether EXPLAIN shows the hidden plan. Secondary queries, other users, other role sets and NONE views stay sealed. The parallel-replicas carrier, the Distributed pushdown and analyzer_inline_views keep the structural check.

Reverting #122330 is not proposed: it would reopen the row leak it fixed.

Validation on a debug build: the issue's query reads 43,688 rows as the definer (3,001,000 on master) and still 3,001,000 as another user. The new test fails on master, and each of the six conditions was removed in a separate build, each caught by the test. All 58 tests with DEFINER / NONE views pass; 05257, 05407 and 05331 now test the sealed path through a non-definer or a NONE view. A performance test is added.


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

Version info

  • Merged into: 26.11.1.1914-master (included in 26.11 and later)
  • Backported to: 26.10.5.10

groeneai and others added 2 commits November 10, 2026 16:05
…es it

Since ClickHouse#122330 every DEFINER / NONE view that can hide rows is read through
the opaque ReadFromSealedViewStep, so an outer ORDER BY ... LIMIT cannot read
in order through it and reads the whole table (ClickHouse#130316: 3,001,000 rows
instead of 43,688 on 26.8). The seal keeps the rows the view hides away from
the invoker, but it ignored who reads the view: a query that runs with the
definer's own identity can read every one of those rows anyway.

StorageView::isReadByDefiner holds when the query runs with exactly the
identity getSQLSecurityOverriddenContext gives the view's own query:
setUser(definer) means the definer by UUID (users of different storages may
share a name), its default roles and no authentication-method GRANTS limit.
A secondary query does not carry the initiator's credential limit (with a
cluster secret the shard re-authenticates the initial user by name), so only
an initial query qualifies. A row policy on the view itself restricts even
the definer, so it keeps the view sealed. readImpl's seal decision and
pushOrderByIntoView use it; the parallel-replicas carrier, the Distributed
pushdown and analyzer_inline_views keep the structural isSealed (the inlined
path drops view-keyed additional_table_filters).

canSeeViewPlan's definer shortcut compared only the user name, so a session
of the definer limited by the GRANTS of its authentication method saw the
hidden plan; it now uses the same predicate and falls back to the regular
grant checks otherwise.

Tests: new 05616 (each of the six conditions is caught by a build without
it). 05257 runs its Distributed loop as a non-definer and its remote() line
with prefer_localhost_replica = 0 (a local replica runs the plan as the
initiator, the definer); 05407 uses SQL SECURITY NONE; 05331 gains a NONE
twin. Performance test added.

Closes ClickHouse#130316

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the citation of getSQLSecurityOverriddenContext from the by-ID definer comment.

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 rounds, 3 findings) plus my own review (4 findings).

# Sev Finding Verdict Evidence / action
1 ❌ A view-keyed additional_table_filters should keep the view sealed for its definer too DISAGREE The reader has the definer's full identity, so it can read the base table directly, where a filter keyed on the view does not apply. On #122330 that setting was ruled not an access-control boundary (r4155364400, r4159482092), and isSealed does not seal for it on master.
2 ⚠️ The read_rows pair checks in the new test pass if one query of a pair is missing from query_log DISAGREE Over the native protocol onFinish logs the query before EndOfStream (TCPHandler.cpp), and a query that fails prints an error instead of 0, which breaks the reference. The checks turn red when a condition is removed (both rows present).
3 ⚠️ The tests do not prove that the outer predicate reaches index analysis for a view read by its definer DISAGREE Forwarding is gated by the same sealed value as the opaque step; the new test asserts that value both ways and fails for each of the six removed conditions.
4 ⚠️ The branch did not merge into current master (include block of StorageView.cpp) AGREE, fixed @ 3fc8583 rebased
5 💡 The comment on isReadByDefiner said every reader checks it, but four isSealed callers keep the structural check on purpose AGREE, fixed @ 3fc8583 sentence removed
6 💡 The changelog entry described the EXPLAIN change only for GRANTS-limited sessions; it also applies to other role sets AGREE, fixed entry reworded
7 💡 05407 switches its view to SQL SECURITY NONE instead of adding a twin as 05331 does DISAGREE the test is about the sealed path, which a definer read no longer takes

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261110-162500

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The setup from #130316 (3,001,000 rows) read 3 times as the definer: read_rows 3,001,000 on master, 43,688 with the fix (another user: 3,001,000 on both). The new test fails on master on 6 lines.
b Root cause explained? Since #122330 every DEFINER / NONE view that can hide rows is read through the opaque ReadFromSealedView step, whoever reads it. Read-in-order and top-N cannot look through that step, so the ORDER BY ... LIMIT reads and sorts the whole table. In the report the reader is the view's definer, who can read every row the view hides.
c Fix matches root cause? Yes. The seal decision now checks the reader: a query that runs with exactly the identity of the view's own query reads it as a subquery. Nothing changes for any reader who cannot read the hidden rows.
d Test intent preserved / new tests added? New test 05616_sql_security_definer_view_read_by_definer (plan, read_rows, secondary queries, row policy, pushdown, GRANTS-limited credential, other roles, analyzer_inline_views). 05257, 05407 and 05331 keep testing the sealed path through a non-definer or a NONE view. Performance test added.
e Both directions demonstrated? Yes: the repro above, plus the new test red on master and green with the fix. Each of the six conditions was removed in its own build, and the test reddens for each.
f Fix is general across code paths? All callers of isSealed were reviewed. The two that read the view in its own context (readImpl, the ORDER BY ... LIMIT pushdown) use the new check. The parallel-replicas carrier, the Distributed pushdown, analyzer_inline_views and the old interpreter keep the structural check. The EXPLAIN visibility shortcut uses the same predicate.
g Fix generalizes across inputs (params/datatypes/wrappers)? The identity matrix is covered: definer, another user, a GRANTS-limited credential, SET ROLE NONE, a row policy on the view, secondary queries with and without an interserver secret, serialize_query_plan 0 and 1, analyzer_inline_views = 1. NONE views stay sealed.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or protocol change. Only the plans of DEFINER views read by their exact definer identity change, back to the plans before 26.10.
i Invariants and contracts preserved? A view is read transparently only by a query that can read everything it hides. Secondary queries stay sealed; the local plan of remote() runs as the initiator on master too; nested views are decided per view. 58 tests with DEFINER / NONE views pass, and 200/200 randomized runs pass.

Session id: cron:clickhouse-impl-slot-6:20261109-161400

@clickhouse-gh

clickhouse-gh Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7074039]

Summary: ✅


AI Review

Summary

PR #130552, reviewed at 707403996645e809cf89130fd1a01e14c91fe4cd, restores query optimizations through SQL SECURITY DEFINER views when the reader matches the definer's identity, default roles, and unrestricted authentication. Other readers retain the opaque boundary, and EXPLAIN uses the stricter identity check. No actionable findings remain.

Missing context / blind spots
  • ⚠️ No local ClickHouse binary or build was available, so runtime tests were not rerun. The fast-test report confirms the modified totals tests pass; the authentication and secondary-query cases in 05616_sql_security_definer_view_read_by_definer are excluded from that job. Their execution and the before/after performance measurements rely on the debug-build validation documented in the PR. A normal stateless CI result would independently confirm that matrix.
Final Verdict

Status: ✅ Approve. No new inline comments were posted.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir @pufit, could you review this and add the can be tested label so CI runs? A SQL SECURITY DEFINER view is no longer read through ReadFromSealedView when the query runs with exactly the identity of the view's own query (the definer by ID, its default roles, an initial query, no GRANTS limit from the authentication method, no row policy on the view), so ORDER BY ... LIMIT reads in order through it again; every other reader stays sealed.

@clickhouse-gh clickhouse-gh Bot added can be tested Allows running workflows for external contributors comp-view Logical (non-materialized) views. labels Nov 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 707403996 with master 2170ef336 (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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse-keeper ×1.12 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 9.1 s 21.6 s +12.7 s (+144%)
Compile time of recompiled translation units

27 translation units recompiled, 242 s compile time in total, 27 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 +16.5 s (+7%) in total before that adjustment.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 10, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tbh, this is totally unnecessary.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 10, 2026
Merged via the queue into ClickHouse:master with commit 24477c6 Nov 11, 2026
369 of 370 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 11, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 13, 2026
robot-ch-test-poll4 added a commit that referenced this pull request Nov 14, 2026
Cherry pick #130552 to 26.10: Read a `SQL SECURITY DEFINER` view transparently when its definer queries it
robot-clickhouse added a commit that referenced this pull request Nov 14, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 14, 2026
Backport #130552 to 26.10: Read a `SQL SECURITY DEFINER` view transparently when its definer queries it
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 18, 2026
…w-defer-plan-optimization

Since ClickHouse#130552 a DEFINER view read by its definer is not sealed, so the views of
the clone cases in 05257 now have another definer: the default user still reads
them sealed with the plan visible. ReadFromSealedViewStep takes the comment on
getBuiltChildPlans that ClickHouse#132608 added to the class in StorageView.cpp, and
[[nodiscard]] for modernize-use-nodiscard (ClickHouse#126158).

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-view Logical (non-materialized) views. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

26.10: ORDER BY ... LIMIT over a SQL SECURITY DEFINER view reads the whole table (regression from #122330)

6 participants