Skip to content

fix(frontend): enforce and revoke view privileges across stored spellings - #32573

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-32444-main-da2dac61
Nov 12, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-32444-main-da2dac61

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #32444

What this PR does / why we need it:

Root cause

  1. GRANT SELECT ON TABLE <view> (and the bare GRANT SELECT ON <db>.<view> spelling) stores the privilege in mo_catalog.mo_role_privs under the table object class against the view's relation id, but verifyViewPrivilegeForRole only matched view-class rows, so a granted, role-activated SELECT was still rejected at query time with internal error: do not have privilege to execute the statement (issue [Bug]: SELECT privilege on a VIEW is stored but never enforced #32444).
  2. The revoke counterpart had two asymmetries:
    • the relation delete templates matched a single stored privilege_level ("d.t" or "t"), while enforcement honors both, so a revoke written in one qualification spelling (qualified vs unqualified) left the row stored under the other spelling and the object stayed authorized for fresh sessions;
    • the cross-class relation delete was selected by object type alone, so wildcard scopes (db.*) also used it and wrongly removed independent wildcard grants of the other object class on the same database, and REVOKE ALL only deleted the table-all row instead of every privilege row of the exact object.

Exact change

  • pkg/frontend/authenticate.go
    • verifyViewPrivilegeForRole re-probes the legacy table-class grant at the exact-relation privilege levels (exactRelationPrivilegeLevels: "d.t"/"t") after the view-class sweep misses, and caches the honored result under the view class; table-class wildcard grants (db.*, *, *.*) stay distinct from view access.
    • New relation delete templates (deleteRelationRolePrivsFormat, deleteRelationAllRolePrivsFormat) delete rows of both stored object classes (obj_type in ("table","view")) and both qualification spellings (privilege_level in ("d.t","t")) for the resolved relation id; REVOKE ALL on an exact relation drops the privilege_id filter and clears every privilege row on that identity; wildcard scopes and account/database-class scopes keep the legacy class-specific delete.
  • pkg/frontend/authenticate_test.go / pkg/frontend/select_lock_privilege_test.go — focused UT regressions: exact-relation TABLE/VIEW revokes (SELECT and ALL, both spellings), unqualified exact revoke, class-specific legacy deletes for db.*/*.*/account with negative asserts, and catalog-lock fixtures.
  • test/distributed/cases/zz_accesscontrol/grant_view_table_spelling.sql (+ generated .result) — user-visible BVT: grants through every accepted spelling authorize a fresh session; a revoke in the other qualification spelling or the other object wording ends authorization in a fresh session; REVOKE ALL removes layered SELECT+ALL grants on views and base tables; wildcard grants of one object class survive the other class's wildcard revokes and end only at the same-class revoke; denials recorded as the documented privilege error. The case snapshots the sys-account enable_privilege_cache global at entry with a value oracle and restores the genuine captured original value at cleanup (the tenant/privilege/bare_object_grant.sql pattern), so running the case with the cache already OFF no longer persists ON into later sessions and cases.

Proof (issue to test)

  • Original reproduction (issue [Bug]: SELECT privilege on a VIEW is stored but never enforced #32444): the exact reported sequence (view granted via ON TABLE to an activated role, root can query the view, SELECT * FROM the view by the granted user fails) fails on the assigned base binary and passes with this change. The stored authorization row is visible in mo_catalog.mo_role_privs with obj_type='table' against the view's obj_id, and the granted user's SELECT returns the view's rows.
  • UT proof: Test_doRevokePrivilege (pkg/frontend) asserts the emitted catalog delete SQL for every revoke routing — exact-relation TABLE/VIEW SELECT and ALL at both qualification spellings, the unqualified table-select case, and the unchanged legacy class-specific path for db.*, *.* and account scopes, with explicit negative asserts that the wildcard deletes contain neither the cross-class nor the both-level in-clause; focused run PASS (137 convey assertions), full pkg/frontend suite PASS.
  • BVT proof: grant_view_table_spelling.sql passes 110/110 with this change; the same BVT fails 7 rows on the true-base binary (5 rows = the original view-enforcement defect, 2 rows = authorization surviving REVOKE ALL through a leftover row). The regenerated .result was inspected statement-by-statement: authorizations, fresh-session denials and preserved wildcard grants match the intended semantics. Review-cleanup revision re-validated locally: the case is green 110/110 twice in a row on one instance, and under the review's counterexample (sys-account enable_privilege_cache already OFF) the pre-fix cleanup left the global ON while the fixed case restores the captured OFF, which its restore oracle records.
  • Independent black-box challenge replays (cache lifecycle across enable_privilege_cache off/on and after an off-to-on switch): fail pre-repair, pass post-repair; pre-PR review counterexample probe (cross-spelling and cross-class revokes) reproduces both defects pre-fix and shows both fixed post-fix.
  • Adjacent-behavior guards on the revised binary: zz_accesscontrol/revoke_privs_role.sql 58/58, tenant/privilege/role3.sql 200/200, both green.

Test commands

  • UT: cd pkg/frontend && go test -run 'Test_doRevokePrivilege' -count=1 (focused), go test -count=1 (full package) — both PASS on macOS arm64 with the repository CGo environment.
  • BVT: test/distributed/cases/zz_accesscontrol/grant_view_table_spelling.sql runs in CI via mo-tester against the committed .result.
  • Static: gofmt -l clean, go vet ./pkg/frontend/... exit 0, golangci-lint run -c .golangci.yml --new-from-rev HEAD ./pkg/frontend 0 issues.

Residual risks

  • Deliberate tightening: an exact-relation revoke now removes the grant whichever qualification spelling or object wording stored it, and REVOKE ALL clears every privilege row on the object identity; anyone relying on a cross-spelling leftover to keep a role authorized will lose that access.
  • Wildcard revokes remain class-specific by ON TABLE / ON VIEW wording (documented); an exact-relation revoke does not remove database- or account-level grants.
  • SHOW GRANTS rendering for view grants (literal d.t) is a separately reported display issue and is unchanged here.

Base update (2026-11-12)

Rebased onto main at 4760bcaa88 to pick up the merged CI-stabilization fixes for #32424 and #32563 (PR #32574); this PR's own diff is unchanged (same five files, 968 insertions / 2 deletions). Focused UT re-validated on the new base via mo-cgo-test -count=1 -run ... ./pkg/frontend (macOS arm64, repository CGo wrapper): TestVerifyViewPrivilegeForRoleHonorsTableSpellingGrant, TestVerifyViewPrivilegeForRoleTableSpellingGrantCachePromotion, the extended TestSelectLockViewAlternativesAndErrors, and the Test_doRevokePrivilege revoke-routing case set all PASS.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Queue requested — the merge queue status continues in this comment ↓.

@XuPeng-SH

XuPeng-SH commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 98f864c1186eb6c511455cc8d646d3c91e41c00d

Review completed. Read the assessment.

@gouhongshen

Copy link
Copy Markdown
Contributor Author

Review P2 addressed (test/distributed/cases/zz_accesscontrol/grant_view_table_spelling.sql:350, unconditional set global enable_privilege_cache = on cleanup): valid finding, fixed in dfdb0cd.

The case now follows the tenant/privilege/bare_object_grant.sql snapshot/restore pattern exactly:

  • entry: set @saved_enable_privilege_cache = @@global.enable_privilege_cache; + select @saved_enable_privilege_cache; (value oracle)
  • cleanup: set global enable_privilege_cache = @saved_enable_privilege_cache; + select @@global.enable_privilege_cache; (restore oracle)
  • .result records the two statements with the default-state 1 value rows, byte-format identical to the upstream case.

Local proof on head dfdb0cd (issue-owned LOG+TN+CN launch of this head, mo-tester normal comparison mode):

  • Default entry state (sys-account global ON): case green 110/110, twice in a row on the same instance; post-case global ON, acc_vt dropped.
  • Review counterexample (sys-account global already OFF): the pre-fix case pair (head 98f864c) runs 107/107 but leaves the global ON — the reported leak reproduced. The fixed case's restore oracle records the restored OFF and the post-case global is OFF, so the incoming mode no longer leaks into later sessions/cases. (The recorded 1 expectations assume the CI-default ON entry state — the same accepted recording as bare_object_grant.sql.)
  • Production code is unchanged in this revision; this is a BVT-only edit.

…ings

A SELECT granted on a view with the ON TABLE spelling (or the bare
MySQL spelling) is stored in mo_catalog.mo_role_privs under the table
object class against the view's relation id, but query-time view
enforcement only matched view-class rows, so the granted privilege
never authorized the SELECT (issue matrixorigin#32444).

Honor the legacy table-class grant at the exact-relation privilege
levels ("d.t" and "t"), which resolve the view by database, name and
object identity; wildcard table-class grants (db.*, *, *.*) stay
distinct from view access, and the honored result is cached under the
view class.

Make the revocation path symmetric with that storage model: an
exact-relation revoke written with any qualification spelling or any
object wording removes the granted rows at both stored levels and both
object classes of the resolved relation, and REVOKE ALL clears every
privilege row on the exact object identity instead of only the
table-all row. Wildcard scopes (d.*, *, *.*) keep the class-specific
delete so a wildcard revoke of one object class cannot remove an
independent wildcard grant of the other class.

Related to matrixorigin#32444
…BVT cleanup

The case cleanup persisted `set global enable_privilege_cache = on`
unconditionally, so running the case while the sys-account global was
already OFF leaked ON into subsequent sessions and cases (review finding
on PR matrixorigin#32573).

Mirror the tenant/privilege/bare_object_grant.sql pattern: snapshot the
sys-account global into @saved_enable_privilege_cache with a value oracle
at entry, and restore the genuine original value with a restore oracle at
cleanup; record the new statements and their default-state oracle rows in
the .result. Every pre-fix behavioral expectation stays unchanged.
@XuPeng-SH

XuPeng-SH commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: d03ab9c7536257c835dfabfbaa4a9db9a8fbc3be

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: d03ab9c7536257c835dfabfbaa4a9db9a8fbc3be

Assessment: approve

The fix reuses existing authorization, cache and transaction owners while preserving exact-relation and wildcard boundaries. The prior BVT global-state leak is resolved. No actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed all five changed files, affected consumers, seven discussion entries and linked issue context.

  • Source identity, Go formatting and extracted SQLite predicates passed: 240-row revoke matrix, 240 grant-boundary checks and 110 matching BVT commands.

  • Final evidence audit passed: checks/1794463992492240934-4.json and assessment-audit.log. Source remains unchanged.

  • Reused frozen current-head CI successes: UT, SCA, coverage, shared build and active BVT lanes. Overall snapshot: 14 successful, 14 skipped, 2 neutral.

  • Considered author-reported focused tests, two 110/110 BVT runs and OFF-entry cleanup verification.

Limitations:

  • No local Go/native, MatrixOne service, race or performance execution; SQLite checks establish predicate semantics only.

  • Raw runtime and CI logs were unavailable for independent inspection.

  • git diff --check reports nonblocking whitespace warnings inside the new raw SQL literals.

@mergify mergify Bot added the queued label Nov 12, 2026
@mergify
mergify Bot merged commit bbfc12b into matrixorigin:main Nov 12, 2026
@mergify

mergify Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

✅ Merged into main as bbfc12b at 06:15 UTC

Less than a minute in the queue, with no time running CI.

So far

  • ✅ Entered the queue Thu 12 Nov, 06:15 · rule main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged 06:15 · bbfc12b · squash
3 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection
  • any of · required by GitHub branch protection:
    • check-success = CI Required
    • check-neutral = CI Required
    • check-skipped = CI Required

Times in UTC · dashboard · how the merge queue works

This branch was previously deployed

1 inactive deployment
ci — d03ab9c7 Deployed Nov 12, 2026 by gouhongshen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants