Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(mcp): mark the two MCP-origin refusals so their audit rows read WARNING (#21519)
Vincent Huang7f9c69fd3a44
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.goindex 00b98430ff..909e089701 100644--- a/backend/api/v1/issue_service.go+++ b/backend/api/v1/issue_service.go@@ -592,7 +592,8 @@ func linkedIssueForCreate(planUID int64, existing, issue *store.IssueMessage) (*}// rejectMCPOriginatedGrantIssue refuses an MCP session creating an issue of any-// type but the database change an agent exists to compose. A ROLE_GRANT issue+// type but the database change an agent exists to compose (an unset type is+// left to the handler, as the gate leaves it). A ROLE_GRANT issue// completes on creation whenever the workspace approval rule produces no// template, and completing it writes the project IAM binding for whichever// grantee the request named — which is ProjectService/SetIamPolicy, a method@@ -618,10 +619,13 @@ func rejectMCPOriginatedGrantIssue(ctx context.Context, issueType v1pb.Issue_Typif !ok || authCtx.DelegatedGrant == nil {return nil}- if issueType == v1pb.Issue_DATABASE_CHANGE {+ if issueType == v1pb.Issue_DATABASE_CHANGE || issueType == v1pb.Issue_TYPE_UNSPECIFIED {+ // An unset type is the handler's invalid argument, not a policy+ // verdict: buildIssueMessage refuses it, and the gate admits it for the+ // same reason (refuseGrantIssueCreation).return nil}- return connect.NewError(connect.CodePermissionDenied, errors.Errorf(+ return permissionDeniedError(ctx, errors.Errorf("an MCP session may not create a %v issue: that issue type completes on creation whenever the "+"workspace approval rule produces no template, which grants access with no human step. "+"Create it signed in to the Bytebase console instead", issueType))diff --git a/backend/api/v1/mcp_gate_test.go b/backend/api/v1/mcp_gate_test.goindex 4bdb33629a..88efe3d828 100644--- a/backend/api/v1/mcp_gate_test.go+++ b/backend/api/v1/mcp_gate_test.go@@ -1604,6 +1604,7 @@ func TestRejectMCPOriginatedGrantIssue(t *testing.T) {"an MCP session may not create an access grant": {mcpSession, v1pb.Issue_ACCESS_GRANT, true},"an MCP session may not create an unknown type": {mcpSession, v1pb.Issue_Type(9999), true},"an MCP session composes database changes": {mcpSession, v1pb.Issue_DATABASE_CHANGE, false},+ "an unset type is left to buildIssueMessage": {mcpSession, v1pb.Issue_TYPE_UNSPECIFIED, false},"the console creates a role grant": {console, v1pb.Issue_ROLE_GRANT, false},"a request with no auth context at all": {context.Background(), v1pb.Issue_ROLE_GRANT, false},} {diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex 100021d26f..6ce7cfde8d 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -1454,7 +1454,7 @@ func rejectMCPOriginatedIssuelessRollout(ctx context.Context, project *store.Pro// and it refuses an unapproved one.return nil}- return connect.NewError(connect.CodePermissionDenied, errors.Errorf(+ return permissionDeniedError(ctx, errors.Errorf("an MCP session may not %s for a plan with no issue: this project requires issue approval, and a "+"rollout created without an issue never meets that gate. Create an issue for the plan and have it "+"approved, or perform this action signed in to the Bytebase console instead", action))diff --git a/backend/tests/audit_sinks_test.go b/backend/tests/audit_sinks_test.goindex 1590a759c4..000918fd53 100644--- a/backend/tests/audit_sinks_test.go+++ b/backend/tests/audit_sinks_test.go@@ -9,6 +9,7 @@ import ("testing""connectrpc.com/connect"+ "github.com/google/uuid""github.com/modelcontextprotocol/go-sdk/mcp""github.com/stretchr/testify/require""google.golang.org/genproto/googleapis/type/expr"@@ -120,6 +121,37 @@ func TestAuditSinksOnBothChains(t *testing.T) {memberSession := openMCPSession(ctx, t, ctl, memberToken)defer memberSession.Close()+ // The issueless-rollout guard needs a plan with no issue. A refused+ // CreateRollout creates nothing, so one plan serves both stdout runs.+ guardSheet, err := ctl.sheetServiceClient.CreateSheet(ctx, connect.NewRequest(&v1pb.CreateSheetRequest{+ Parent: project,+ Sheet: &v1pb.Sheet{Content: []byte("CREATE TABLE audit_sinks_guard(id INT);")},+ }))+ a.NoError(err)+ guardPlan, err := ctl.planServiceClient.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{+ Parent: project,+ Plan: &v1pb.Plan{Specs: []*v1pb.Plan_Spec{{+ Id: uuid.NewString(),+ Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{+ Targets: []string{clamp.database},+ Sheet: guardSheet.Msg.Name,+ },+ },+ }}},+ }))+ a.NoError(err)+ setRequireIssueApproval := func(t *testing.T, on bool) {+ t.Helper()+ ctl.project.RequireIssueApproval = on+ updated, err := ctl.projectServiceClient.UpdateProject(ctx, connect.NewRequest(&v1pb.UpdateProjectRequest{+ Project: ctl.project,+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"require_issue_approval"}},+ }))+ require.NoError(t, err)+ require.Equal(t, on, updated.Msg.RequireIssueApproval)+ }+requireCode := func(t *testing.T, want connect.Code, err error) {t.Helper()if want == 0 {@@ -249,6 +281,40 @@ func TestAuditSinksOnBothChains(t *testing.T) {require.Contains(t, write.text, "READ_ONLY")},},+ {+ name: "mcp/audited refused by the grant-issue guard in the handler", method: v1connect.IssueServiceUpdateIssueProcedure, mcp: true, parent: project,+ wantRow: true, wantWarning: true,+ call: func(t *testing.T) {+ out := callAPIOnSession(ctx, t, adminSession, "IssueService/UpdateIssue", map[string]any{+ "issue": map[string]any{+ "name": project + "/issues/999999",+ "title": "grant me project owner",+ "type": "ROLE_GRANT",+ "roleGrant": map[string]any{+ "role": "roles/projectOwner",+ "user": "users/[email protected]",+ },+ },+ "updateMask": "title",+ "allowMissing": true,+ })+ require.Equal(t, http.StatusForbidden, out.Status, out.Error)+ require.Contains(t, out.Error, "ROLE_GRANT")+ },+ },+ {+ name: "mcp/audited refused by the issueless-rollout guard in the handler", method: v1connect.RolloutServiceCreateRolloutProcedure, mcp: true, parent: project,+ wantRow: true, wantWarning: true,+ call: func(t *testing.T) {+ setRequireIssueApproval(t, true)+ defer setRequireIssueApproval(t, false)+ out := callAPIOnSession(ctx, t, adminSession, "RolloutService/CreateRollout", map[string]any{+ "parent": guardPlan.Msg.Name,+ })+ require.Equal(t, http.StatusForbidden, out.Status, out.Error)+ require.Contains(t, out.Error, "plan with no issue")+ },+ },{name: "public/audited refused by the SQL Editor access check", method: v1connect.SQLServiceQueryProcedure,member: true, parent: project,diff --git a/backend/tests/mcp_gate_test.go b/backend/tests/mcp_gate_test.goindex ee11b36999..89c2c0d91c 100644--- a/backend/tests/mcp_gate_test.go+++ b/backend/tests/mcp_gate_test.go@@ -169,6 +169,17 @@ func TestMCPGateRefusesGrantIssues(t *testing.T) {a.Equal(http.StatusBadRequest, change.Status,"a database-change issue must reach the handler, which then asks for its plan: %s", change.Error)a.NotContains(change.Error, "not available to MCP sessions")++ // An unset type reaches the handler too, and is the handler's own+ // complaint: an invalid argument, not a policy verdict, so its row stays+ // INFO.+ untyped := callAPIOnSession(ctx, t, session, "IssueService/CreateIssue", map[string]any{+ "parent": projectName,+ "issue": map[string]any{"title": "no type at all"},+ })+ a.Equal(http.StatusBadRequest, untyped.Status,+ "an unset type is the handler's invalid argument: %s", untyped.Error)+ a.NotContains(untyped.Error, "MCP session")}// TestMCPCannotRunAnIssuelessRollout is the rule rollout_service.proto says thediff --git a/docs/design/audit-what-produces-a-row.md b/docs/design/audit-what-produces-a-row.mdindex 79d51beea2..a26eebdc2e 100644--- a/docs/design/audit-what-produces-a-row.md+++ b/docs/design/audit-what-produces-a-row.md@@ -147,7 +147,8 @@ option on its response field.**Implementation.** No API change. The mark is set where the access-controlinterceptor answers permission-denied, at the MCP ceiling gate, at the-read-only SQL clamp, and at the custom-auth handler sites that make the check+read-only SQL clamp, at the two MCP-origin guards in the issue and rollout+handlers, and at the custom-auth handler sites that make the checkthemselves. Handler sites mark by building the refusal with`permissionDeniedError` in `backend/api/v1`, so the mark cannot be forgotten separatelyfrom the error; the two out-of-band doors mark through their shared writer.@@ -162,9 +163,10 @@ from the error; the two out-of-band doors mark through their shared writer.- The runtime flag is deleted. The write site makes two decisions: store ifthe method is audited and the call reached its handler; stream if stdout ison and the call was stored or refused by a permission check.-- No MCP exception. The MCP chain, which is pre-release, stops storing the- ceiling-gate refusals it stores today; they stream instead. Clamp refusals- run inside the audited Query handler and stay stored.+- No MCP exception. The MCP chain, whose enforcement shipped in 3.22.0 with+ the access-policy section of its settings page behind a dev gate until+ 3.23.0, stops storing the ceiling-gate refusals it stores today; they stream+ instead. Clamp refusals run inside the audited Query handler and stay stored.**Implementation.** No API change.