Skip to content

fix: support GRANT/REVOKE on VIEW with DEFINER/INVOKER security type - #24195

Merged
20 commits merged into
matrixorigin:3.0-devfrom
ck89119:issue-24187-3.0-dev
Apr 28, 2026
Merged

20 commits merged into
matrixorigin:3.0-devfrom
ck89119:issue-24187-3.0-dev

Conversation

@ck89119

@ck89119 ck89119 commented Apr 24, 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:

issue #24187

What this PR does / why we need it:

Support view-level privilege control with GRANT/REVOKE ON VIEW syntax, including:

  • Add objectTypeView to distinguish view privileges from table privileges
  • Support DEFINER and INVOKER security types for views via view_security_type session variable
  • Track view lineage (viewChain, directView) in plan nodes for privilege checking at execution time
  • Store SecurityType in view metadata (ViewData)
  • Add distributed test cases for grant/revoke on view scenarios

1. Fix view access bypass via base table ALL/OWNERSHIP: convert predefined
   TableAll/TableOwnership entries from bare General entries to Compound
   entries carrying originViews/directView, so they go through view-chain
   privilege verification.

2. Fix CREATE SQL SECURITY VIEW ignoring explicit security type: add
   SecurityType field to tree.CreateView, pass it from parser, and
   override session variable in plan layer when DDL explicitly specifies it.

3. Fix SHOW CREATE VIEW / ALTER VIEW not preserving security type: splice
   SecurityType into SHOW output when missing from Stmt; ALTER VIEW now
   inherits old view's SecurityType when not explicitly specified.

4. Fix DEFINER path not expanding role inheritance: add
   verifyPrivilegeWithRoleInheritance() to BFS-traverse mo_role_grant
   when the effective role differs from the session role.

5. Update BVT expected results for changed error message wording.
…behavior

SHOW CREATE VIEW should only replay the stored Stmt text as-is, not
auto-inject SQL SECURITY. This matches MySQL behavior where SHOW CREATE
VIEW only shows SQL SECURITY if the original DDL explicitly included it.

Remove findViewKeyword() and the splice logic from buildShowCreateView().
Update BVT tests to use explicit DDL syntax (CREATE SQL SECURITY INVOKER
VIEW) instead of session variable for SecurityType verification.
Revert the 34 result file changes from the previous commit.
…MySQL compat

1. GRANT ON VIEW with wildcard levels (db.*, *.*, *) now filters WGO
   check by obj_type (table vs view), preventing table-level WGO from
   being used to issue view wildcard grants.

2. Restore SHOW CREATE VIEW SQL SECURITY splice logic to match MySQL
   behavior: MySQL always shows SQL SECURITY in SHOW CREATE VIEW output.
   Use findViewKeyword() with whitespace boundary matching.
   Update 34 result files for SQL SECURITY DEFINER in output.

3. Add SHOW CREATE VIEW test for session-variable-created INVOKER view
   (v_invoker) to cover the implicit SecurityType path.
@LeftHandCold

Copy link
Copy Markdown
Collaborator

我这边再顺着兼容逻辑对了一遍,发现一个会影响升级场景的问题:REVOKE ... ON VIEW 现在只删 obj_type='view' 的记录,但读/WGO 路径已经显式兼容了历史上按 obj_type='table' 存的 view 权限。

具体看下来:

  • verifyViewPrivilegeForRole 会同时检查 objectTypeView 和 objectTypeTable,也就是会继续接受旧数据里的 table-typed view grant(pkg/frontend/authenticate.go 里大概在 verifyViewPrivilegeForRole / verifyViewPrivilegeForRoleWithObjectType 这一段)。
  • getRoleSetThatPrivilegeGrantedToWGOScoped 对 VIEW 也会 merge 一次 legacy objectTypeTable 的结果。
  • 但 doRevokePrivilege 这里还是只按 objType.String() 生成一条 delete SQL;当 AST 是 VIEW 时,只会删掉 obj_type='view',不会把遗留的 obj_type='table' 那条一起删掉。

这样在升级后的集群里,如果某个 view 的旧授权记录还是 obj_type='table':

  1. REVOKE SELECT ON VIEW db.v FROM r 执行成功;
  2. 旧 row 其实还在;
  3. 后面的 SELECT / WITH GRANT OPTION 检查还是会通过 legacy fallback 命中这条旧 row。

这会让“兼容旧授权读路径”与“新语法 revoke 写路径”不一致。这里看起来需要在 VIEW revoke 时把 legacy table-typed row 一起删掉,或者先做一次迁移/归一化;否则 upgrade case 下 revoke 实际上不生效。建议补一个回归:先种一条 legacy obj_type='table' 的 view grant,再跑 REVOKE ... ON VIEW,然后验证访问和 WGO 都失效。

@LeftHandCold

Copy link
Copy Markdown
Collaborator

我再补一个更直接的 blocker:现在新的 view 执行期校验其实只会在 extractPrivilegeTipsFromPlan() 抽到了基表/写表节点时触发,所以不读任何基表的 view 会完全绕过 GRANT SELECT ON VIEW。

代码链路大概是这样:

  • determineUserCanExecStatementWithObjectTypeTableAndDatabase() 里先跑 arr := extractPrivilegeTipsFromPlan(p)。
  • extractPrivilegeTipsFromPlan() 只给 TABLE_SCAN / PRE_INSERT / INSERT / DELETE / MULTI_UPDATE 这些节点产出 tip。
  • 如果 len(arr) == 0,当前逻辑直接 return true。

这对普通 SELECT 1 没问题,但对 view 不成立。比如:

create view v_const as select 1;
grant select on view db.v_const to r;

这里 plan 里没有任何 TABLE_SCAN,arr 会是空的;后面既不会把 v_const 本身塞进 privilege item,也不会走 resolveViewChainPrivilegeContext(),结果就是 SELECT * FROM v_const 根本不受新的 view privilege 逻辑约束。

也就是说,这个 PR 现在实际保护到的是“通过 view 去访问基表”的场景,但对 constant/expression-only view 直接漏检了。这里看起来至少需要在“statement 来自 view、但没有抽到 base-object tips”时,补一条针对 directView 的显式 privilege item;否则 GRANT/REVOKE ON VIEW 在这类 view 上是形同虚设的。建议补个很小的回归:create view v_const as select 1,不给用户任何 view grant,确认 select * from v_const 应该失败。

@LeftHandCold

Copy link
Copy Markdown
Collaborator

我这边还看到一个 WITH GRANT OPTION 的作用域问题:ON VIEW * / ON TABLE * 在继承角色那条校验路径里把“当前数据库”丢掉了。

现在两条路径不一致:

  • 直接校验 formSqlFromGrantPrivilege() 对 PRIVILEGE_LEVEL_TYPE_STAR 会按 ses.GetDatabaseName() 生成 current-db 的 db.* 检查;
  • 但继承角色/WGO 这条 getRoleSetThatPrivilegeGrantedToWGOScopedWithObjectType() 只把 DATABASE_TABLE / TABLE / DATABASE_STAR 当成 object-scoped,STAR 会直接走 getRoleSetThatPrivilegeGrantedToWGOWithObjType(),最后只按 obj_type 过滤,不带数据库/object id。

这样如果用户当前 use db2,但某个继承角色只在 db1.* 上有 view/table 的 WGO,那么 GRANT ... ON VIEW * ... 这条 secondary-role 路径还是会把它算成可授权角色;也就是把 db1.* 的 WGO 错复用到了 db2.*。

这个和 direct path 的 current-db 语义是冲突的。这里看起来应该把 PRIVILEGE_LEVEL_TYPE_STAR 也按“当前数据库上的 db.*”处理,而不是退化成 obj_type-only 的全库匹配。建议补个回归:让用户默认角色没有 WGO,只通过继承角色拿到 db1.* 的 WGO,然后 use db2 执行 GRANT ... ON VIEW *,这里应该失败。

ck89119 added 2 commits April 28, 2026 18:25
# Conflicts:
#	pkg/frontend/authenticate.go
#	pkg/pb/plan/plan.pb.go
#	pkg/sql/parsers/dialect/mysql/mysql_sql.go
This pull request was closed.

This branch was previously deployed

1 inactive deployment
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/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants