Skip to content

SEM: add strict mode for next-gen builds#67945

Open
AmoebaProtozoa wants to merge 11 commits into
pingcap:masterfrom
AmoebaProtozoa:sem-strict
Open

SEM: add strict mode for next-gen builds#67945
AmoebaProtozoa wants to merge 11 commits into
pingcap:masterfrom
AmoebaProtozoa:sem-strict

Conversation

@AmoebaProtozoa
Copy link
Copy Markdown
Contributor

@AmoebaProtozoa AmoebaProtozoa commented Apr 21, 2026

What problem does this PR solve?

Issue Number: ref #67765

Problem Summary:

Next-gen deployments need tighter restrictions than the current semv2 SQL rules can express: a fixed allow-list of statements and a matching reject list for query hints that could otherwise sidestep SEM-masked sysvars.

What changed and how does it work?

Adds security.enable-strict-sem to tidb.toml (default false). Honored only when security.enable-sem=true on a next-gen build; otherwise ignored with a startup warning.

When active, the planner rejects statements outside the allow-list with ErrNotSupportedWithSem (no RESTRICTED_SQL_ADMIN bypass), strips memory_quota / read_consistent_replica / max_execution_time / resource_group hints from both user queries and SQL bindings when the matching sysvar is SEM-hidden or SEM-readonly, blocks DROP USER / RENAME USER / GRANT|REVOKE|SET ROLE on cloud_admin@% and root@%, and falls pipelined DML back to standard with a warning.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test
  • No need to test

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Summary by CodeRabbit

  • New Features
    • Added strict SEM support with automatic detection and enforcement.
    • Enforced restrictions on specific SQL statements, optimizer hints, and features when strict SEM is active.
    • Surface warnings/info when unsupported features are used or when strict SEM is ignored by configuration.
    • Pipelined DML is disabled with a fallback and warning in strict SEM deployments.

Review Change Stack

Signed-off-by: AmoebaProtozoa <8039876+AmoebaProtozoa@users.noreply.github.com>
Signed-off-by: AmoebaProtozoa <8039876+AmoebaProtozoa@users.noreply.github.com>
Signed-off-by: AmoebaProtozoa <8039876+AmoebaProtozoa@users.noreply.github.com>
Signed-off-by: AmoebaProtozoa <8039876+AmoebaProtozoa@users.noreply.github.com>
@ti-chi-bot ti-chi-bot Bot added do-not-merge/invalid-title do-not-merge/needs-linked-issue do-not-merge/needs-tests-checked release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Apr 21, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Apr 21, 2026

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 21, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a concurrency-safe strict SEM flag, implements strict Starter Edition Mode checks for statements and optimizer hints, integrates strict checks into planner and session (pipelined DML), wires strict enabling at server startup, and adds unit/integration tests and BUILD dependency updates.

Changes

Strict SEM Feature Implementation

Layer / File(s) Summary
Strict SEM flag and statement core
pkg/util/sem/v2/restricted_statement.go, pkg/util/sem/v2/restricted_statement_test.go
Introduces IsRestrictedStatement(stmt ast.Node) error, protected user/role lists, per-statement verifiers (DDL, SHOW, LOAD DATA, ADMIN, BRIE), and extensive tests validating allowed and rejected SQL under strict SEM.
Hint restrictions and tests
pkg/util/sem/v2/restricted_hint.go, pkg/util/sem/v2/restricted_hint_test.go
Adds IsRestrictedHint(hintNameLower string) error that rejects specific optimizer hints when strict SEM is enabled and relevant sysvars are hidden/readonly; unit tests cover hidden/readonly/disabled cases.
Server init and strict flag wiring
cmd/tidb-server/main.go
setupSEM() checks Starter deployment mode, selects semv2.Enable(cfg.Security.SEMConfig) vs sem.Enable(), logs when strict SEM is required but disabled, and calls semv2.EnableStrict() post-enable when appropriate.
Planner & optimizer integration
pkg/planner/core/planbuilder.go, pkg/planner/optimize.go, pkg/planner/BUILD.bazel
checkSEMStmt() invokes semv2.IsStrictEnabled() + IsRestrictedStatement before prior SEM guards; filterRestrictedHints() removes semv2-restricted hints under strict SEM and appends warnings into sessVars.StmtCtx before parsing. BUILD deps updated.
Session-level execution control
pkg/session/session.go, pkg/session/BUILD.bazel
usePipelinedDmlOrWarn() now forces pipelined DML off when semv2.IsStrictEnabled() and emits a warning; BUILD deps updated.
Integration test updates
tests/realtikvtest/pipelineddmltest/pipelineddml_test.go, tests/realtikvtest/pipelineddmltest/BUILD.bazel
Test TestPipelinedDMLNegative enables strict SEM for the test, runs an INSERT, and asserts the pipelined-DML fallback warning; test BUILD gets new semv2 dep.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested labels

ok-to-test, release-note-none

Suggested reviewers

  • hawkingrei
  • yudongusa
  • nolouch
  • winoros

Poem

🐰 I hop and guard the SEM so strict,
Hints and statements trimmed and picked,
Pipelined DML bowed to the rule,
Starter mode holds steady and cool,
—CodeRabbit, nibbling on a changelist.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.54% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description addresses the required sections: problem statement (issue ref #67765), detailed explanation of changes, and test checklist. However, the release-note section is empty despite the PR affecting user behaviors. Fill in the release-note section with a summary of the new strict SEM mode feature and its user-facing implications, following the release notes style guide.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title clearly summarizes the main change: adding a strict SEM mode feature for next-gen builds.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ti-chi-bot ti-chi-bot Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. sig/planner SIG: Planner labels Apr 21, 2026
@tiprow
Copy link
Copy Markdown

tiprow Bot commented Apr 21, 2026

Hi @AmoebaProtozoa. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@AmoebaProtozoa AmoebaProtozoa changed the title [WIP] Strict SEM *: add strict SEM mode for next-gen builds Apr 21, 2026
@ti-chi-bot ti-chi-bot Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/invalid-title release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/needs-linked-issue labels Apr 21, 2026
@AmoebaProtozoa AmoebaProtozoa changed the title *: add strict SEM mode for next-gen builds SEM: add strict SEM mode for next-gen builds Apr 21, 2026
@AmoebaProtozoa AmoebaProtozoa changed the title SEM: add strict SEM mode for next-gen builds SEM: add strict mode for next-gen builds Apr 21, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Apr 21, 2026

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot Bot added do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. and removed release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Apr 21, 2026
@AmoebaProtozoa AmoebaProtozoa marked this pull request as ready for review April 21, 2026 18:26
@ti-chi-bot ti-chi-bot Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 21, 2026
@pantheon-ai
Copy link
Copy Markdown

pantheon-ai Bot commented Apr 21, 2026

Review failed due to infrastructure/execution failure after retries. Please re-trigger review.

ℹ️ Learn more details on Pantheon AI.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/planner/optimize.go (1)

233-244: ⚠️ Potential issue | 🟡 Minor

Stripped-hint warnings may suppress the later "hints ignored, using bindSQL" notice.

originStmtHints is now parsed from the already-filtered hint list. If strict SEM strips every user hint, hint.ParseStmtHints will see len(hints) == 0 and set QueryHasHints = false, so the check at line 351 (if originStmtHints.QueryHasHints { ... "The system ignores the hints in the current query ..." }) will silently skip that note for queries whose only hints happened to be restricted. The user still gets a per-hint restricted warning, so this is minor, but worth a conscious decision: either compute QueryHasHints from the pre-filter slice, or accept the reduced note.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/planner/optimize.go` around lines 233 - 244, The current code parses
originStmtHints from the already-filtered tableHints so QueryHasHints can become
false if all user hints were stripped; to fix, preserve or inspect the
pre-filter hint slice when computing QueryHasHints: call hint.ParseStmtHints
using the original extracted hints (before filterRestrictedHints) or, after
parsing the filtered originStmtHints, explicitly set
originStmtHints.QueryHasHints = len(originalExtractedHints) > 0 (where
originalExtractedHints is the slice returned by
hint.ExtractTableHintsFromStmtNode before filterRestrictedHints). Ensure you
still append restrictedHintWarns to sessVars.StmtCtx and then assign
sessVars.StmtCtx.StmtHints = originStmtHints so downstream checks (e.g., the
QueryHasHints check) reflect whether the user actually provided hints before
filtering.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@cmd/tidb-server/main.go`:
- Around line 1165-1191: The current setupSEM enables classic sem (sem.Enable())
when SEMConfig is empty which leaves semv2 global state nil and makes
EnableStrictSEM ineffective; change setupSEM so EnableStrictSEM requires
cfg.Security.SEMConfig to be non-empty: if cfg.Security.SEMConfig == "" and
cfg.Security.EnableStrictSEM is true, log a clear warning (via
logutil.BgLogger().Warn) that strict SEM requires a semv2 config and
ignore/disable strict mode, then continue with classic sem; only call
semv2.EnableStrict() after successfully calling
semv2.Enable(cfg.Security.SEMConfig) (i.e., ensure semv2.Enable returned no
error and global semv2 state is initialized before calling semv2.EnableStrict())
and do not call semv2.EnableStrict() when semv2 was not enabled.

In `@pkg/planner/core/planbuilder.go`:
- Around line 6382-6389: The strict SEM check (semv2.IsStrictEnabled()) is being
executed before verifying SEM is enabled, causing strict mode to apply even when
SEM is disabled; update the logic so strict checks only run when
semv2.IsEnabled() is true (e.g., wrap the semv2.IsStrictEnabled() /
semv2.IsRestrictedStatement(stmt) block inside an if semv2.IsEnabled() { ... }
or change it to if semv2.IsEnabled() && semv2.IsStrictEnabled() { if err :=
semv2.IsRestrictedStatement(stmt); err != nil { return err } }).

In `@pkg/session/session.go`:
- Around line 5521-5524: Add a SQL-level test that enables strict SEM via
semv2.EnableStrict(), sets tidb_dml_type='bulk', executes a DML (e.g.,
INSERT/UPDATE) and asserts the presence of the warning text "Pipelined DML is
not supported in this deployment. Fallback to standard mode" using SHOW
WARNINGS; the test should exercise the usePipelinedDmlOrWarn code path (which
calls semv2.IsStrictEnabled()) and then disable strict mode after the test to
avoid global state leakage. Ensure the test file mirrors existing pipelined-DML
tests (e.g., pipelineddml_test.go) for setup/teardown and uses the same
assertion pattern for SHOW WARNINGS.

In `@pkg/util/sem/v2/restricted_hint.go`:
- Around line 30-31: The "resource_group" branch currently unconditionally
returns an error referencing strict SEM; update the branch in restricted_hint.go
(the case for "resource_group" in the switch handling hints) to follow the same
SEM-aware pattern as the other cases: either check IsStrictEnabled() (or the
same sysvar visibility helper used by
memory_quota/read_consistent_replica/max_execution_time) and only return the
"not supported when strict SEM is enabled" error when strict SEM is active, or
change the error text to a SEM-agnostic message that does not mention strict
SEM; pick one approach and apply it in the "resource_group" case so behavior and
messaging match the other hint checks.

---

Outside diff comments:
In `@pkg/planner/optimize.go`:
- Around line 233-244: The current code parses originStmtHints from the
already-filtered tableHints so QueryHasHints can become false if all user hints
were stripped; to fix, preserve or inspect the pre-filter hint slice when
computing QueryHasHints: call hint.ParseStmtHints using the original extracted
hints (before filterRestrictedHints) or, after parsing the filtered
originStmtHints, explicitly set originStmtHints.QueryHasHints =
len(originalExtractedHints) > 0 (where originalExtractedHints is the slice
returned by hint.ExtractTableHintsFromStmtNode before filterRestrictedHints).
Ensure you still append restrictedHintWarns to sessVars.StmtCtx and then assign
sessVars.StmtCtx.StmtHints = originStmtHints so downstream checks (e.g., the
QueryHasHints check) reflect whether the user actually provided hints before
filtering.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: eded8646-e4a3-40db-a5f7-b6b1b77d1b34

📥 Commits

Reviewing files that changed from the base of the PR and between 8121c82 and ef9fae6.

📒 Files selected for processing (13)
  • cmd/tidb-server/main.go
  • pkg/config/config.go
  • pkg/planner/BUILD.bazel
  • pkg/planner/core/planbuilder.go
  • pkg/planner/optimize.go
  • pkg/session/BUILD.bazel
  • pkg/session/session.go
  • pkg/util/sem/v2/BUILD.bazel
  • pkg/util/sem/v2/restricted_hint.go
  • pkg/util/sem/v2/restricted_hint_test.go
  • pkg/util/sem/v2/restricted_statement.go
  • pkg/util/sem/v2/restricted_statement_test.go
  • pkg/util/sem/v2/strict.go

Comment thread cmd/tidb-server/main.go
Comment on lines +6382 to 6389
// Strict SEM applies before the semv2 rule check with no admin bypass.
if semv2.IsStrictEnabled() {
if err := semv2.IsRestrictedStatement(stmt); err != nil {
return err
}
}

if !semv2.IsEnabled() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Gate strict SEM on SEM being enabled.

Line 6383 enforces strict mode before checking semv2.IsEnabled(), so a process with IsStrictEnabled()==true but SEM disabled would still reject statements, contrary to the config contract that strict SEM is ignored unless security.enable-sem=true.

Suggested fix
+	semEnabled := semv2.IsEnabled()
 	// Strict SEM applies before the semv2 rule check with no admin bypass.
-	if semv2.IsStrictEnabled() {
+	if semEnabled && semv2.IsStrictEnabled() {
 		if err := semv2.IsRestrictedStatement(stmt); err != nil {
 			return err
 		}
 	}
 
-	if !semv2.IsEnabled() {
+	if !semEnabled {
 		return nil
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/planner/core/planbuilder.go` around lines 6382 - 6389, The strict SEM
check (semv2.IsStrictEnabled()) is being executed before verifying SEM is
enabled, causing strict mode to apply even when SEM is disabled; update the
logic so strict checks only run when semv2.IsEnabled() is true (e.g., wrap the
semv2.IsStrictEnabled() / semv2.IsRestrictedStatement(stmt) block inside an if
semv2.IsEnabled() { ... } or change it to if semv2.IsEnabled() &&
semv2.IsStrictEnabled() { if err := semv2.IsRestrictedStatement(stmt); err !=
nil { return err } }).

Comment thread pkg/session/session.go
Comment thread pkg/util/sem/v2/restricted_hint.go
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

❌ Patch coverage is 7.79221% with 213 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.6064%. Comparing base (fd471d3) to head (3dfb47c).
⚠️ Report is 36 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #67945        +/-   ##
================================================
- Coverage   77.7126%   75.6064%   -2.1062%     
================================================
  Files          1991       2011        +20     
  Lines        552142     570709     +18567     
================================================
+ Hits         429084     431493      +2409     
- Misses       122138     138614     +16476     
+ Partials        920        602       -318     
Flag Coverage Δ
integration 41.6357% <7.7922%> (+1.8338%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 60.4888% <ø> (ø)
parser ∅ <ø> (∅)
br 49.6115% <ø> (-13.4820%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: AmoebaProtozoa <8039876+AmoebaProtozoa@users.noreply.github.com>
AmoebaProtozoa and others added 3 commits May 6, 2026 18:07
Inserting the StarterEssential field with a leading comment split the
Config struct's tag-alignment block, so gofmt wants narrower padding for
the post-comment fields.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ict-SEM pipelined-DML test

- IsRestrictedHint now self-gates on IsStrictEnabled, matching the
  documented contract; the four hint tests pivot to enabling strict
  before each scenario.
- Doc-comment now spells out that resource_group is rejected outright
  under starter/essential because resource group management is
  unavailable in those deployments.
- TestPipelinedDMLNegative now covers the strict-SEM fallback warning.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…IsStarter()

Upstream master added a process-wide next-gen deployment mode
(pkg/config/deploymode, pingcap#68178). Replace the temporary `starter-essential`
TiDB config flag that previously gated the strict-SEM allow-list with
`deploymode.IsStarter()`:

- Drop Config.StarterEssential (field, default, and the next-gen-only
  Valid() check) -- deploymode.IsStarter() already returns false on
  classic builds, so the kernel check is redundant.
- setupSEM() now enables strict SEM iff deploymode.IsStarter(); it still
  requires security.enable-sem=true (strict SEM layers on top of SEM) and
  still warns + no-ops when Starter mode is set but SEM is disabled.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 12, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign king-dylan for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@AmoebaProtozoa
Copy link
Copy Markdown
Contributor Author

/retest

@tiprow
Copy link
Copy Markdown

tiprow Bot commented May 18, 2026

@AmoebaProtozoa: PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test.

Details

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Signed-off-by: AmoebaProtozoa <8039876+AmoebaProtozoa@users.noreply.github.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
pkg/util/sem/v2/restricted_statement.go (1)

186-186: 💤 Low value

Dead code: AdminStmt case is unreachable.

AdminStmt is already handled directly in IsRestrictedStatement (line 94-95) before reaching verifySimple. Since AdminStmt is not in the case list at lines 102-108, this allowlist entry will never match.

♻️ Remove unreachable case
 case
     *ast.FlushStmt,
     *ast.BeginStmt,
     *ast.CommitStmt,
     *ast.SavepointStmt,
     *ast.ReleaseSavepointStmt,
     *ast.RollbackStmt,
     *ast.CreateUserStmt,
     *ast.AlterUserStmt,
     *ast.SetPwdStmt,
     *ast.SetSessionStatesStmt,
     *ast.KillStmt,
     *ast.BinlogStmt,
     *ast.DropStatsStmt,
-    *ast.AdminStmt,
     *ast.GrantStmt,
     *ast.RevokeStmt,
     *ast.NonTransactionalDMLStmt,
     *ast.UseStmt:
     return nil
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/util/sem/v2/restricted_statement.go` at line 186, The switch in
verifySimple contains an unreachable AdminStmt case because
IsRestrictedStatement already handles *ast.AdminStmt earlier; remove the
AdminStmt entry from the case list in verifySimple (or from the allowlist used
there) so the unreachable branch is deleted, leaving only the actually reachable
statement types referenced in verifySimple and ensuring no dead-case remains.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/util/sem/v2/restricted_statement.go`:
- Around line 206-219: GrantRoleStmt and RevokeRoleStmt currently only check
Roles but must also block operations that target restricted users similar to
SetDefaultRoleStmt; update the handlers for GrantRoleStmt and RevokeRoleStmt to
iterate over s.Users and call isRestrictedUser on each entry, returning
notSupported with an appropriate message if any target user is restricted. Use
the existing symbols GrantRoleStmt, RevokeRoleStmt, Users, Roles and
isRestrictedUser to locate the code and implement the additional check so
GRANT/REVOKE cannot modify restricted accounts' role sets.

---

Nitpick comments:
In `@pkg/util/sem/v2/restricted_statement.go`:
- Line 186: The switch in verifySimple contains an unreachable AdminStmt case
because IsRestrictedStatement already handles *ast.AdminStmt earlier; remove the
AdminStmt entry from the case list in verifySimple (or from the allowlist used
there) so the unreachable branch is deleted, leaving only the actually reachable
statement types referenced in verifySimple and ensuring no dead-case remains.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 04cdc6d4-3420-4138-9a0a-66f7a686f0a3

📥 Commits

Reviewing files that changed from the base of the PR and between 13a5fbe and f4b0816.

📒 Files selected for processing (3)
  • pkg/util/sem/v2/restricted_statement.go
  • pkg/util/sem/v2/restricted_statement_test.go
  • tests/realtikvtest/pipelineddmltest/pipelineddml_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/realtikvtest/pipelineddmltest/pipelineddml_test.go

Comment on lines +206 to +219
case *ast.GrantRoleStmt:
for _, role := range s.Roles {
if isRestrictedRole(role.Username, role.Hostname) {
return notSupported(fmt.Sprintf("GRANT ROLE %s", role))
}
}
return nil
case *ast.RevokeRoleStmt:
for _, role := range s.Roles {
if isRestrictedRole(role.Username, role.Hostname) {
return notSupported(fmt.Sprintf("REVOKE %s", role))
}
}
return nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if GrantRoleStmt and RevokeRoleStmt have a Users field to verify the fix is applicable
ast-grep --pattern $'type GrantRoleStmt struct {
  $$$
}'
ast-grep --pattern $'type RevokeRoleStmt struct {
  $$$
}'

Repository: pingcap/tidb

Length of output: 587


🏁 Script executed:

# Check the actual code at lines 206-219 and surrounding context
cat -n pkg/util/sem/v2/restricted_statement.go | sed -n '200,230p'

Repository: pingcap/tidb

Length of output: 1352


🏁 Script executed:

# Check SetDefaultRoleStmt handling around lines 248-252
cat -n pkg/util/sem/v2/restricted_statement.go | sed -n '240,260p'

Repository: pingcap/tidb

Length of output: 1060


🏁 Script executed:

# Check what isRestrictedUser and isRestrictedRole functions do
rg -A 5 "func isRestrictedUser|func isRestrictedRole" pkg/util/sem/v2/

Repository: pingcap/tidb

Length of output: 864


Block role operations targeting restricted users, consistent with SetDefaultRoleStmt.

GrantRoleStmt and RevokeRoleStmt only check whether the role being granted/revoked is restricted, but do not check the target users. In contrast, SetDefaultRoleStmt (lines 248-251) explicitly blocks operations targeting restricted users with the justification "so [restricted accounts'] default role set cannot be tampered with."

This gap allows operations like GRANT 'some_role'@'%' TO 'cloud_admin'@'%' to pass, directly modifying the restricted account's role set—the same protection SetDefaultRoleStmt guards against. Add target-user checks to both GrantRoleStmt and RevokeRoleStmt handlers using the existing isRestrictedUser function on the Users field.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/util/sem/v2/restricted_statement.go` around lines 206 - 219,
GrantRoleStmt and RevokeRoleStmt currently only check Roles but must also block
operations that target restricted users similar to SetDefaultRoleStmt; update
the handlers for GrantRoleStmt and RevokeRoleStmt to iterate over s.Users and
call isRestrictedUser on each entry, returning notSupported with an appropriate
message if any target user is restricted. Use the existing symbols
GrantRoleStmt, RevokeRoleStmt, Users, Roles and isRestrictedUser to locate the
code and implement the additional check so GRANT/REVOKE cannot modify restricted
accounts' role sets.

Signed-off-by: AmoebaProtozoa <8039876+AmoebaProtozoa@users.noreply.github.com>
Signed-off-by: AmoebaProtozoa <8039876+AmoebaProtozoa@users.noreply.github.com>
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 18, 2026

[FORMAT CHECKER NOTIFICATION]

Notice: To remove the do-not-merge/needs-tests-checked label, please finished the tests then check the finished items in description.

For example:

Tests <!-- At least one of them must be included. -->

- [x] Unit test
- [ ] Integration test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No code

‼️ Must keep the HTML comments <!-- At least one of them must be included. -->

📖 For more info, you can check the "Contribute Code" section in the development guide.

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 18, 2026

@AmoebaProtozoa: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-error-log-review 3dfb47c link false /test pull-error-log-review

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/needs-tests-checked do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. sig/planner SIG: Planner size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant