Repository navigation
Read a SQL SECURITY DEFINER view transparently when its definer queries it - #130552
Conversation
…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]>
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).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261110-162500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261109-161400 |
|
Workflow [PR], commit [7074039] Summary: ✅
AI ReviewSummaryPR #130552, reviewed at Missing context / blind spots
Final VerdictStatus: ✅ Approve. No new inline comments were posted. |
|
cc @vdimir @pufit, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units27 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. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Tbh, this is totally unnecessary.
24477c6
Cherry pick #130552 to 26.10: Read a `SQL SECURITY DEFINER` view transparently when its definer queries it
…rently when its definer queries it
Backport #130552 to 26.10: Read a `SQL SECURITY DEFINER` view transparently when its definer queries it
…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]>
Closes: #130316
Related: #122330
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
SQL SECURITY DEFINERview 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 ... LIMITreads in order through it). Other users, and sessions of the definer that are limited by theGRANTSof 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 inEXPLAIN.Description
On 26.10,
ORDER BY ... LIMITover aSQL SECURITY DEFINERview that filters rows reads the whole table: 3,001,000 rows instead of 43,688 in #130316. Since #122330, everyDEFINER/NONEview that can hide rows is read through the opaqueReadFromSealedViewstep, 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::isReadByDefinerholds 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), noGRANTSlimit from the authentication method, and no row policy on the view itself. Such a query can read everything the view hides, soreadImpland theORDER BY ... LIMITpushdown into views no longer seal the view for it. The same check replaces the name-only definer shortcut that decides whetherEXPLAINshows the hidden plan. Secondary queries, other users, other role sets andNONEviews stay sealed. The parallel-replicas carrier, theDistributedpushdown andanalyzer_inline_viewskeep 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/NONEviews pass; 05257, 05407 and 05331 now test the sealed path through a non-definer or aNONEview. A performance test is added.Workflow [PR]
Sync PR [sync-upstream/pr/130552]
Version info
26.11.1.1914-master(included in26.11and later)26.10.5.10