Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(audit): stream refused calls to stdout; store what reached a handler (#21482)
Vincent Huangbff6c8d1618e
diff --git a/backend/api/mcp/connection_audit.go b/backend/api/mcp/connection_audit.goindex 9a9adcf075..41e5464ac6 100644--- a/backend/api/mcp/connection_audit.go+++ b/backend/api/mcp/connection_audit.go@@ -47,7 +47,6 @@ func (s *Server) refuseConnection(c *echo.Context, delegated auth.DelegatedMCPCrParent: common.FormatWorkspace(delegated.WorkspaceID),Method: common.AuditMethodMCPSessionAuthorize,Resource: common.FormatWorkspace(delegated.WorkspaceID),- Severity: storepb.AuditLog_INFO,User: common.FormatUserEmail(delegated.Principal),Status: &spb.Status{Code: int32(codes.PermissionDenied),diff --git a/backend/api/mcp/connection_audit_test.go b/backend/api/mcp/connection_audit_test.goindex 04be24db9f..33ef8be971 100644--- a/backend/api/mcp/connection_audit_test.go+++ b/backend/api/mcp/connection_audit_test.go@@ -78,6 +78,8 @@ func TestMCPConnectionDenialEmission(t *testing.T) {require.Equal(t, "workspaces/ws-test", row.Resource)require.Equal(t, "users/[email protected]", row.User)require.EqualValues(t, 7, row.Status.GetCode(), "PermissionDenied")+ require.Equal(t, storepb.AuditLog_WARNING, row.Severity,+ "the door and the per-request gate stamp one ceiling verdict the same way")require.Contains(t, row.Status.GetMessage(), "turned MCP access off")require.Equal(t, "10.0.1.50", row.RequestMetadata.GetCallerIp())require.Equal(t, "TestAgent/1.0", row.RequestMetadata.GetCallerSuppliedUserAgent())diff --git a/backend/api/mcp/gen/openapi.yaml b/backend/api/mcp/gen/openapi.yamlindex 9cc5a17346..34576b4537 100644--- a/backend/api/mcp/gen/openapi.yaml+++ b/backend/api/mcp/gen/openapi.yaml@@ -23420,9 +23420,9 @@ components:description: |-The same Stripe Checkout Session ID PurchaseResponse mints, arriving backthe other way, so it carries the same classification. VerifyCheckoutSession- is EXCLUDED, and the MCP gate records a row for every method it refuses, so- leaving this unannotated would write the id into a denial row while the- response that produced it was redacted.+ is EXCLUDED, and a refusal of it streams its request to the stdout audit+ log, so leaving this unannotated would write the id into that line while+ the response that produced it was redacted.title: VerifyCheckoutSessionRequestadditionalProperties: falsebytebase.v1.VerifyCheckoutSessionResponse:diff --git a/backend/api/oauth2/consent_audit.go b/backend/api/oauth2/consent_audit.goindex bdb602dc17..1bdb0299eb 100644--- a/backend/api/oauth2/consent_audit.go+++ b/backend/api/oauth2/consent_audit.go@@ -95,7 +95,6 @@ func consentRefusalRow(attempt consentAttempt, verdict auth.MCPCeilingVerdict, rParent: common.FormatWorkspace(attempt.user.workspaceID),Method: common.AuditMethodMCPConsentApprove,Resource: common.FormatWorkspace(attempt.user.workspaceID),- Severity: storepb.AuditLog_INFO,User: common.FormatUserEmail(attempt.user.email),Status: &spb.Status{Code: int32(codes.PermissionDenied),diff --git a/backend/api/v1/acl.go b/backend/api/v1/acl.goindex 637ba29240..e98882c3cc 100644--- a/backend/api/v1/acl.go+++ b/backend/api/v1/acl.go@@ -46,8 +46,17 @@ func (in *ACLInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {err := in.doACLCheck(ctx, req.Any(), req.Spec().Procedure)if err != nil {+ // Keyed on the code because inside doACLCheck only a permission+ // verdict answers PermissionDenied.+ if connect.CodeOf(err) == connect.CodePermissionDenied {+ common.SetPermissionDenied(ctx)+ }return nil, err}+ // ACL is the last interceptor on both chains, so admission here is the+ // call reaching its handler. It is set here, not in doACLCheck, because+ // the skipped-authentication return admits too.+ common.SetHandlerReached(ctx)return next(ctx, req)}}@@ -151,11 +160,11 @@ func (in *ACLInterceptor) doACLCheck(ctx context.Context, request any, fullMetho// workspace BEFORE publishing them on the AuthContext. Project, instance,// and database ownership is already validated in populateRawResources via// workspace-filtered store lookups; here we validate workspace resources.- // Publishing only validated entries matters on the internal MCP chain,- // where the audit interceptor runs outside ACL and derives a denied row's- // parents from Resources — an entry that failed this check must never- // become an audit parent (the denial would be filed under the foreign- // workspace the request named, not under the caller).+ // Publishing only validated entries matters because the audit interceptor+ // runs outside ACL and derives a refused call's parents from Resources —+ // an entry that failed this check must never become an audit parent (the+ // refusal would be filed under the foreign workspace the request named, not+ // under the caller).// Runs after authentication so unauthenticated requests get 401 first,// preventing resource existence probing.for _, resource := range resources {diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.goindex eb2ad8c67f..128e340c35 100644--- a/backend/api/v1/audit.go+++ b/backend/api/v1/audit.go@@ -35,23 +35,22 @@ const ()// AuditInterceptor is the v1 audit interceptor for gRPC server.+//+// It feeds two sinks from one built row. The database stores a call to an+// audited method that reached its handler. Stdout, when on, streams every+// stored row and every call a permission check refused, before the insert.type AuditInterceptor struct {- store *store.Store- secret string- profile *config.Profile-- // createAuditLogFunc, when set, replaces createAuditLog. Test-only seam- // that lets unit tests observe what an audited call writes, and when,- // without a database.- createAuditLogFunc func(context.Context, *auditEntry) error+ auditLogWriter common.AuditLogWriter+ secret string+ profile *config.Profile}// NewAuditInterceptor returns a new v1 API audit interceptor.func NewAuditInterceptor(store *store.Store, secret string, profile *config.Profile) *AuditInterceptor {return &AuditInterceptor{- store: store,- secret: secret,- profile: profile,+ auditLogWriter: store,+ secret: secret,+ profile: profile,}}@@ -71,31 +70,17 @@ func (in *AuditInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunchandlerAuditWorkspaceID = workspaceID})- // The MCP ceiling gate sits inside this interceptor and refuses a call- // before it reaches its handler. needAudit reads only the method's own- // audit annotation, and 47 of the 121 methods the gate refuses carry- // none: the four FORBIDDEN ones that were silent before the gate grew- // (Refresh, SwitchWorkspace, TestIdentityProvider, TestEmailSetting)- // plus 43 EXCLUDED ones. Their denials would leave no trace at all.- // A policy denial is recorded whatever the annotation says: the- // annotation decides whether ordinary use of a method is interesting,- // and a refused agent is interesting either way. Only the internal MCP- // chain runs the gate, so the public chain is unaffected.- //- // Recording a request that was never recorded is why redaction has to- // cover more than the audited RPCs: a denial must not transcribe the- // secret it refused. Since redaction is driven by the field annotation- // rather than by a per-RPC redactor, a gate-refused method is covered- // the moment its fields are annotated — the population is the one above,- // not the four named methods.- mcpPolicyDenied := false- ctx = common.WithSetMCPPolicyDenied(ctx, func() { mcpPolicyDenied = true })+ var permissionDenied, handlerReached bool+ ctx = common.WithSetPermissionDenied(ctx, func() { permissionDenied = true })+ ctx = common.WithSetHandlerReached(ctx, func() { handlerReached = true })startTime := time.Now()response, rerr := next(ctx, req)latency := time.Since(startTime)- if needAudit(ctx) || mcpPolicyDenied {+ stored := handlerReached && needAudit(ctx)+ streamed := (stored || permissionDenied) && in.profile.RuntimeEnableAuditLogStdout.Load()+ if stored || streamed {var respMsg anyif !common.IsNil(response) {respMsg = response.Any()@@ -110,8 +95,10 @@ func (in *AuditInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFuncheaders: req.Header(),peerAddr: req.Peer().Addr,latency: latency,+ permissionDenied: permissionDenied,+ store: stored,}- if err := in.writeAuditLog(ctx, entry); err != nil {+ if err := in.createAuditLog(ctx, entry); err != nil {slog.Warn("audit interceptor: failed to create audit log", log.BBError(err), slog.String("method", req.Spec().Procedure))}}@@ -176,8 +163,9 @@ func (c *auditConnectStreamingConn) Send(resp any) error {headers: c.RequestHeader(),peerAddr: c.Peer().Addr,latency: time.Since(c.startTime),+ store: true,}- if auditErr := c.interceptor.writeAuditLog(c.ctx, entry); auditErr != nil {+ if auditErr := c.interceptor.createAuditLog(c.ctx, entry); auditErr != nil {return auditErr}}@@ -202,6 +190,8 @@ type auditEntry struct {headers http.HeaderpeerAddr stringlatency time.Duration+ permissionDenied bool+ store bool}// auditRow is one row an audited call leaves: the payload, under the@@ -211,28 +201,27 @@ type auditRow struct {payload *storepb.AuditLog}-// writeAuditLog records an audited call, through the test seam when one is set.-func (in *AuditInterceptor) writeAuditLog(ctx context.Context, e *auditEntry) error {- if in.createAuditLogFunc != nil {- return in.createAuditLogFunc(ctx, e)- }- return in.createAuditLog(ctx, e)-}-func (in *AuditInterceptor) createAuditLog(ctx context.Context, e *auditEntry) error {rows, err := in.buildAuditRows(ctx, e)if err != nil {return err}+ // Every line is written before any insert, so a failed insert loses no+ // line: the stream is the surface that still works when the metadata+ // database does not.+ if in.profile.RuntimeEnableAuditLogStdout.Load() {+ for _, row := range rows {+ common.LogAuditToStdout(ctx, row.payload)+ }+ }+ if !e.store {+ return nil+ }createAuditLogCtx := context.WithoutCancel(ctx)for _, row := range rows {- if err := in.store.CreateAuditLog(createAuditLogCtx, row.workspaceID, row.payload); err != nil {+ if err := in.auditLogWriter.CreateAuditLog(createAuditLogCtx, row.workspaceID, row.payload); err != nil {return err}- // Log audit event to stdout using slog (if enabled)- if in.profile.RuntimeEnableAuditLogStdout.Load() {- common.LogAuditToStdout(ctx, row.payload)- }}return nil}@@ -253,8 +242,8 @@ func (in *AuditInterceptor) buildAuditRows(ctx context.Context, e *auditEntry) (// flag alone made setting it a switch that turns off the record of being// caught.//- // EVERY failure records, not only a policy denial, and that is deliberate- // rather than incidental. The instance form runs a validate-only+ // EVERY failure records, not only a permission refusal, and that is+ // deliberate rather than incidental. The instance form runs a validate-only// connection test before each save and on each Test Connection click, so// the rows this adds are mostly failed connection tests, not refusals —// a real volume change on a common flow. Keying on a denial code instead@@ -296,9 +285,9 @@ func (in *AuditInterceptor) buildAuditRows(ctx context.Context, e *auditEntry) (auditWorkspaceID string}// One row per DISTINCT parent: batch requests repeat the same resource- // once per item, and since ACL-denied internal-chain calls are audited- // too, an unprivileged caller reaches this fan-out — duplicates would let- // one denied batch call naming N items write N identical rows.+ // once per item, and a refused call is streamed too, so an unprivileged+ // caller reaches this fan-out — duplicates would let one refused batch+ // call naming N items write N identical lines.var parents []auditParentseenParent := make(map[string]bool)appendParent := func(ap auditParent) {@@ -316,11 +305,11 @@ func (in *AuditInterceptor) buildAuditRows(ctx context.Context, e *auditEntry) (// A request carrying a delegated MCP grant is the one exception, because// its workspace was not named by the caller: the internal MCP interceptor// verified the credential and bound the workspace before the request- // reached this chain. Without the exception these three methods are the- // last silent denials in the system — the ceiling gate refuses them before- // dispatch, so the handler never runs and never announces a workspace —- // and they are the flow that mails or consumes the secret a login accepts.- // Presence of the grant is the marker, never a field value.+ // reached this chain. Without the exception the ceiling gate's refusal of+ // these three methods streams nothing — the gate refuses before dispatch,+ // so the handler never runs and never announces a workspace — and they are+ // the flow that mails or consumes the secret a login accepts. Presence of+ // the grant is the marker, never a field value.handlerValidatedWorkspaceMethod := (e.method == v1connect.AuthServiceRequestPasswordResetProcedure ||e.method == v1connect.AuthServiceResetPasswordProcedure ||e.method == v1connect.AuthServiceSendEmailLoginCodeProcedure) &&@@ -387,6 +376,12 @@ func (in *AuditInterceptor) buildAuditRows(ctx context.Context, e *auditEntry) (serviceData := redactAuditServiceData(e.serviceData)auditStatus := redactAuditStatus(convertErrToStatus(e.rerr))mcpDelegation := mcpDelegationFromAuthContext(authContext)+ // status.code carries every failure, so severity is what separates a+ // refused caller from other failed calls.+ severity := storepb.AuditLog_INFO+ if e.permissionDenied {+ severity = storepb.AuditLog_WARNING+ }var rows []auditRowfor _, ap := range parents {@@ -419,7 +414,7 @@ func (in *AuditInterceptor) buildAuditRows(ctx context.Context, e *auditEntry) (Parent: ap.parent,Method: e.method,Resource: resource,- Severity: storepb.AuditLog_INFO,+ Severity: severity,User: user,Request: requestString,Response: responseString,diff --git a/backend/api/v1/audit_log_service.go b/backend/api/v1/audit_log_service.goindex 03ab41e0f6..68181f1170 100644--- a/backend/api/v1/audit_log_service.go+++ b/backend/api/v1/audit_log_service.go@@ -62,6 +62,19 @@ func (s *AuditLogService) SearchAuditLogs(ctx context.Context, request *connect.}limitPlusOne := offset.limit + 1+ // This search writes its own audit row, so an unbounded offset traversal+ // never reaches the end of the set: newest-first it returns the same row+ // forever, oldest-first the set grows at the rate the offset advances. The+ // first page pins the database's clock and every page holds to it.+ if offset.createTimeUpperBound == nil {+ traversalStart, err := s.store.AuditLogTraversalStart(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, err)+ }+ offset.createTimeUpperBound = timestamppb.New(traversalStart)+ }+ filterQ = store.ApplyCreateTimeUpperBound(filterQ, offset.createTimeUpperBound.AsTime())+var project *stringif request.Msg.Parent != "" && request.Msg.Parent != "projects/-" {project = &request.Msg.Parentdiff --git a/backend/api/v1/audit_redact_inventory_test.go b/backend/api/v1/audit_redact_inventory_test.goindex 76d6a255d4..958e4ac7da 100644--- a/backend/api/v1/audit_redact_inventory_test.go+++ b/backend/api/v1/audit_redact_inventory_test.go@@ -28,9 +28,9 @@ import (// "what the audit row writes down" and a new credential shows up as one line of// diff.//-// The population is wider than the audited RPCs: WrapUnary writes a row on-// `needAudit(ctx) || mcpPolicyDenied`, so gate-refused methods carrying no-// audit annotation are in scope. Every registered Any type is in scope too,+// The population is wider than the audited RPCs: WrapUnary streams every call+// a permission check refused, so refused methods carrying no audit annotation+// are in scope. Every registered Any type is in scope too,// since those reach the row without passing marshalAuditPayload — which is why// the registry is enforced at its call sites for this list to mean anything.//@@ -257,12 +257,6 @@ var auditRecordedFields = []string{"bytebase.v1.ApprovalTemplate.title","bytebase.v1.ApproveIssueRequest.comment","bytebase.v1.ApproveIssueRequest.name",- "bytebase.v1.AuditLog.method",- "bytebase.v1.AuditLog.name",- "bytebase.v1.AuditLog.request",- "bytebase.v1.AuditLog.resource",- "bytebase.v1.AuditLog.response",- "bytebase.v1.AuditLog.user","bytebase.v1.AuthenticationInfo.workspace","bytebase.v1.AuthorizationRequest.client_id","bytebase.v1.AuthorizationRequest.endpoint",@@ -690,10 +684,6 @@ var auditRecordedFields = []string{"bytebase.v1.LoginRequest.email","bytebase.v1.LoginRequest.idp_name","bytebase.v1.LoginRequest.workspace",- "bytebase.v1.MCPDelegation.client_id",- "bytebase.v1.MCPDelegation.correlation_id",- "bytebase.v1.MCPDelegation.resource",- "bytebase.v1.MCPDelegation.scope","bytebase.v1.MaskingExemptionPolicy.Exemption.members","bytebase.v1.MaskingReason.algorithm","bytebase.v1.MaskingReason.context",@@ -812,8 +802,6 @@ var auditRecordedFields = []string{"bytebase.v1.RemoveDataSourceRequest.name","bytebase.v1.RequestIssueRequest.comment","bytebase.v1.RequestIssueRequest.name",- "bytebase.v1.RequestMetadata.caller_ip",- "bytebase.v1.RequestMetadata.caller_supplied_user_agent","bytebase.v1.RequestPasswordResetRequest.email","bytebase.v1.RequestPasswordResetRequest.workspace","bytebase.v1.RequestReauthCodeRequest.name",diff --git a/backend/api/v1/audit_redact_test.go b/backend/api/v1/audit_redact_test.goindex 3039338e85..aa8caac12e 100644--- a/backend/api/v1/audit_redact_test.go+++ b/backend/api/v1/audit_redact_test.go@@ -31,8 +31,9 @@ import ("github.com/bytebase/bytebase/backend/store")-// Audited RPCs write their request and response payloads to audit_log, and to-// stdout when RuntimeEnableAuditLogStdout is set. Anything with+// Audited RPCs write their request and response payloads to audit_log, and+// refused calls of any RPC write them to stdout when RuntimeEnableAuditLogStdout+// is set. Anything with// bb.auditLogs.search/export, or read access to the log pipeline, can read// them. Redaction therefore owes the row two things, and the first group below// is exactly those two:@@ -171,6 +172,16 @@ func TestAuditRowKeepsItsSubstance(t *testing.T) {response: true,want: []string{"next-page-token"},},+ {+ // A search's own row must not copy the rows it read.+ name: "audit search retains page token and drops the results",+ value: &v1pb.SearchAuditLogsResponse{+ AuditLogs: []*v1pb.AuditLog{{Name: secretSentinel, Request: secretSentinel, Response: secretSentinel}},+ NextPageToken: "next-page-token",+ },+ response: true,+ want: []string{"next-page-token"},+ },{// INPUT_ONLY on the password, OUTPUT_ONLY only on its container, so// clients are meant to send it. Which role was written is the point@@ -587,8 +598,8 @@ func TestAuditRedactsPackedAny(t *testing.T) {// response carries every row of an admin-mode query — and Send builds its own// auditEntry and calls createAuditLog directly, so a redaction walk that lived// in WrapUnary would silently skip it. Everything else about streaming-// persistence is exercised through the createAuditLogFunc stub, which bypasses-// the real path; this one writes a real row and reads it back.+// persistence is exercised through a fake audit log writer; this one writes a+// real row through the store and reads it back.func TestStreamingAuditRedactsRows(t *testing.T) {t.Parallel()st := newAuditLiveStore(t)diff --git a/backend/api/v1/audit_test.go b/backend/api/v1/audit_test.goindex 5a9c27afeb..b730773a1b 100644--- a/backend/api/v1/audit_test.go+++ b/backend/api/v1/audit_test.go@@ -1,10 +1,17 @@package v1import (+ "bytes""context""encoding/base64"+ "encoding/json"+ "fmt"+ "log"+ "log/slog""net/http"+ "slices""strings"+ "sync""testing""connectrpc.com/connect"@@ -73,10 +80,11 @@ func TestStreamingAuditPersistedBeforeSend(t *testing.T) {a := require.New(t)recorder := &auditOrderRecorder{}interceptor := &AuditInterceptor{- createAuditLogFunc: func(_ context.Context, _ *auditEntry) error {+ auditLogWriter: auditLogWriterFunc(func(context.Context, string, *storepb.AuditLog) error {recorder.record("audit")return nil- },+ }),+ profile: &config.Profile{},}handler := interceptor.WrapStreamingHandler(func(_ context.Context, conn connect.StreamingHandlerConn) error {if err := conn.Receive(&v1pb.AdminExecuteRequest{}); err != nil {@@ -85,7 +93,7 @@ func TestStreamingAuditPersistedBeforeSend(t *testing.T) {return conn.Send(&v1pb.AdminExecuteResponse{})})- ctx := context.WithValue(context.Background(), common.AuthContextKey, &common.AuthContext{Audit: true})+ ctx := newAuditTestContext(&common.AuthContext{Audit: true})a.NoError(handler(ctx, &auditRecorderConn{recorder: recorder}))a.Equal([]string{"audit", "send"}, recorder.events,@@ -97,10 +105,11 @@ func TestStreamingAuditPersistedBeforeSend(t *testing.T) {recorder := &auditOrderRecorder{}persistErr := errors.New("persist failed")interceptor := &AuditInterceptor{- createAuditLogFunc: func(_ context.Context, _ *auditEntry) error {+ auditLogWriter: auditLogWriterFunc(func(context.Context, string, *storepb.AuditLog) error {recorder.record("audit")return persistErr- },+ }),+ profile: &config.Profile{},}handler := interceptor.WrapStreamingHandler(func(_ context.Context, conn connect.StreamingHandlerConn) error {if err := conn.Receive(&v1pb.AdminExecuteRequest{}); err != nil {@@ -109,7 +118,7 @@ func TestStreamingAuditPersistedBeforeSend(t *testing.T) {return conn.Send(&v1pb.AdminExecuteResponse{})})- ctx := context.WithValue(context.Background(), common.AuthContextKey, &common.AuthContext{Audit: true})+ ctx := newAuditTestContext(&common.AuthContext{Audit: true})a.ErrorIs(handler(ctx, &auditRecorderConn{recorder: recorder}), persistErr)a.Equal([]string{"audit"}, recorder.events,"response must not be delivered when the audit entry cannot be persisted")@@ -226,22 +235,108 @@ func (r *specRequest) Spec() connect.Spec {return connect.Spec{Procedure: r.procedure}}-// newRecordingAuditInterceptor builds the interceptor with no store and-// captures the rows each audited call would have written, so a test asserts on-// what the interceptor decided rather than on a database. That the rows reach-// the audit page on a live server is backend/tests' TestMCPAuditProvenance.-func newRecordingAuditInterceptor() (*AuditInterceptor, *[]auditRow) {+// auditLogWriterFunc adapts a function to common.AuditLogWriter.+type auditLogWriterFunc func(context.Context, string, *storepb.AuditLog) error++func (f auditLogWriterFunc) CreateAuditLog(ctx context.Context, workspace string, payload *storepb.AuditLog) error {+ return f(ctx, workspace, payload)+}++// recordingAuditLogWriter stands in for the store's insert. It keeps every row+// the interceptor tried to store, and fails each insert with err when set.+type recordingAuditLogWriter struct {+ mu sync.Mutex+ rows []auditRow+ err error+ // onInsert runs before each row is recorded, for a test that asserts what+ // has already reached the stream.+ onInsert func()+}++func (w *recordingAuditLogWriter) CreateAuditLog(_ context.Context, workspace string, payload *storepb.AuditLog) error {+ if w.onInsert != nil {+ w.onInsert()+ }+ w.mu.Lock()+ defer w.mu.Unlock()+ w.rows = append(w.rows, auditRow{workspaceID: workspace, payload: payload})+ return w.err+}++func (w *recordingAuditLogWriter) stored() []auditRow {+ w.mu.Lock()+ defer w.mu.Unlock()+ return slices.Clone(w.rows)+}++// newRecordingAuditInterceptor builds the interceptor with stdout off and a+// writer that keeps the rows each call stored, so a test asserts on what the+// interceptor decided rather than on a database. TestStreamingAuditRedactsRows+// writes through the real store.+func newRecordingAuditInterceptor() (*AuditInterceptor, *recordingAuditLogWriter) {in := NewAuditInterceptor(nil, "test-secret", &config.Profile{})- rows := &[]auditRow{}- in.createAuditLogFunc = func(ctx context.Context, e *auditEntry) error {- built, err := in.buildAuditRows(ctx, e)- if err != nil {- return err+ writer := &recordingAuditLogWriter{}+ in.auditLogWriter = writer+ return in, writer+}++// admitted stands in for the ACL interceptor's admission, for a test that+// runs the audit interceptor around a handler with no ACL between them.+func admitted(handler connect.UnaryFunc) connect.UnaryFunc {+ return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {+ common.SetHandlerReached(ctx)+ return handler(ctx, req)+ }+}++// captureAuditStream sends slog.Default to a buffer until the test ends and+// returns a reader for the audit lines written so far. slog.Default is+// process-wide, so a test that calls this must not be parallel.+func captureAuditStream(t *testing.T) func() []map[string]any {+ t.Helper()+ var mu sync.Mutex+ var buf bytes.Buffer+ prev, prevWriter, prevFlags := slog.Default(), log.Writer(), log.Flags()+ // slog.SetDefault also points the log package at the new handler, and+ // restoring the slog default does not undo that.+ t.Cleanup(func() {+ slog.SetDefault(prev)+ log.SetOutput(prevWriter)+ log.SetFlags(prevFlags)+ })+ slog.SetDefault(slog.New(slog.NewJSONHandler(lockedWriter{mu: &mu, w: &buf}, nil)))+ return func() []map[string]any {+ mu.Lock()+ defer mu.Unlock()+ var lines []map[string]any+ for _, raw := range bytes.Split(buf.Bytes(), []byte("\n")) {+ var line map[string]any+ if json.Unmarshal(raw, &line) == nil && line["log_type"] == "audit" {+ lines = append(lines, line)+ }}- *rows = append(*rows, built...)- return nil+ return lines+ }+}++// lineText reads one string attribute off a captured audit line.+func lineText(line map[string]any, key string) string {+ v, ok := line[key].(string)+ if !ok {+ return ""}- return in, rows+ return v+}++type lockedWriter struct {+ mu *sync.Mutex+ w *bytes.Buffer+}++func (l lockedWriter) Write(p []byte) (int, error) {+ l.mu.Lock()+ defer l.mu.Unlock()+ return l.w.Write(p)}func auditTestUser() *store.UserMessage {@@ -294,7 +389,7 @@ func TestAuditRowCarriesMCPDelegationProvenance(t *testing.T) {AnyRequest: connect.NewRequest(&v1pb.QueryRequest{Name: "instances/i/databases/d"}),procedure: "/bytebase.v1.SQLService/Query",}- _, err := in.WrapUnary(next)(newAuditTestContext(authCtx), req)+ _, err := in.WrapUnary(admitted(next))(newAuditTestContext(authCtx), req)require.NoError(t, err)}@@ -305,7 +400,7 @@ func TestAuditRowCarriesMCPDelegationProvenance(t *testing.T) {ClientID: "client-A",CorrelationID: "corr-full",})- matched := rowsByCorrelation(*rows, "corr-full")+ matched := rowsByCorrelation(rows.stored(), "corr-full")require.Len(t, matched, 1, "an audited internal-chain call must produce exactly one provenance-carrying row")got := matched[0].GetMcpDelegation()require.Equal(t, "mcp:read-only", got.GetScope())@@ -315,7 +410,7 @@ func TestAuditRowCarriesMCPDelegationProvenance(t *testing.T) {t.Run("a legacy empty grant still marks MCP origin, empty stays empty", func(t *testing.T) {invoke(t, &common.DelegatedGrant{CorrelationID: "corr-legacy"})- matched := rowsByCorrelation(*rows, "corr-legacy")+ matched := rowsByCorrelation(rows.stored(), "corr-legacy")require.Len(t, matched, 1)got := matched[0].GetMcpDelegation()require.NotNil(t, got, "presence of the delegation message is the MCP-origin marker, even for empty legacy grants")@@ -327,7 +422,7 @@ func TestAuditRowCarriesMCPDelegationProvenance(t *testing.T) {t.Run("a public-chain row carries no MCP fields", func(t *testing.T) {invoke(t, nil)var publicRows int- for _, row := range *rows {+ for _, row := range rows.stored() {if row.payload.GetMcpDelegation() == nil {publicRows++}@@ -338,10 +433,8 @@ func TestAuditRowCarriesMCPDelegationProvenance(t *testing.T) {// TestAuditParentsDeduplicated pins that an audited call writes ONE row per// distinct parent. Batch requests repeat the same project resource once per-// item, and since PR 5b routes ACL-denied internal-chain calls through the-// audit interceptor, an unprivileged caller reaches this fan-out — without-// dedup, a single denied batch call naming N items would write N identical-// rows.+// item; without dedup, a single batch call naming N items would write N+// identical rows, and a refused one N identical stream lines.func TestAuditParentsDeduplicated(t *testing.T) {t.Parallel()in, rows := newRecordingAuditInterceptor()@@ -364,11 +457,11 @@ func TestAuditParentsDeduplicated(t *testing.T) {AnyRequest: connect.NewRequest(&v1pb.QueryRequest{Name: "instances/i/databases/d"}),procedure: "/bytebase.v1.SQLService/Query",}- _, err := in.WrapUnary(next)(newAuditTestContext(authCtx), req)+ _, err := in.WrapUnary(admitted(next))(newAuditTestContext(authCtx), req)require.NoError(t, err)var parents []string- for _, row := range rowsByCorrelation(*rows, "corr-dedup") {+ for _, row := range rowsByCorrelation(rows.stored(), "corr-dedup") {parents = append(parents, row.Parent)}require.ElementsMatch(t,@@ -377,76 +470,199 @@ func TestAuditParentsDeduplicated(t *testing.T) {"one audit row per DISTINCT parent — repeated batch resources must not multiply rows")}-// TestInternalChainAuditRecordsACLDenial pins PR 5b's denial-audit mechanism:-// with the audit interceptor wrapped OUTSIDE the ACL interceptor (the internal-// MCP chain's order), an ACL denial produces an audit row carrying the-// provenance and the denied status; a method whose annotation opts out of-// auditing stays silent for permitted and denied calls alike. The workspace-// mismatch is decided on the request alone, so no store is needed.-func TestInternalChainAuditRecordsACLDenial(t *testing.T) {- auditIn, rows := newRecordingAuditInterceptor()- aclIn := NewACLInterceptor(nil, "test-secret", nil /* iamManager: unreached on these paths */, &config.Profile{})-- invoke := func(t *testing.T, audited bool, correlationID, resource string) (handlerReached bool, rerr error) {- t.Helper()- authCtx := &common.AuthContext{- Audit: audited,- AuthMethod: common.AuthMethodCustom,- DelegatedGrant: &common.DelegatedGrant{- Scope: "mcp:read-only",- Resource: "https://bb.example.com/mcp",- ClientID: "client-A",- CorrelationID: correlationID,- },+// auditCallOutcome is where a call in TestAuditSinks ends.+type auditCallOutcome int++const (+ // The handler runs and succeeds.+ outcomeOK auditCallOutcome = iota+ // The handler makes its own permission check, marks it and refuses.+ outcomeRefusedInHandler+ // The handler fails for a reason that is not a permission check.+ outcomeFailedInHandler+ // ACL refuses: the request names another workspace.+ outcomeRefusedByACL+ // ACL fails before any verdict: the request names no workspace.+ outcomeFailedInACL+ // The MCP ceiling gate refuses a FORBIDDEN method.+ outcomeRefusedByGate+)++// TestAuditSinks is the store and stream rule, driven through the real audit,+// gate and ACL interceptors in each chain's order:+//+// store = audited method and the call reached its handler+// stream = stdout on and (stored or refused by a permission check)+//+// Every expectation is written out per row rather than derived, so a change to+// the rule has to change this table. Not parallel: it captures slog.Default.+func TestAuditSinks(t *testing.T) {+ lines := captureAuditStream(t)+ const procedure = "/bytebase.v1.SettingService/UpdateSetting"+ aclIn := NewACLInterceptor(nil, "test-secret", nil /* iamManager: CUSTOM methods never reach it */, &config.Profile{})+ gate := NewInternalMCPGateInterceptor(readWriteCeiling())++ type sinkCase struct {+ name string+ audited bool+ validateOnly bool+ outcome auditCallOutcome+ wantRow bool+ wantLine bool // when stdout is on+ wantWarning bool+ }+ cases := []sinkCase{+ {name: "audited ok", audited: true, outcome: outcomeOK, wantRow: true, wantLine: true},+ {name: "audited refused in handler", audited: true, outcome: outcomeRefusedInHandler, wantRow: true, wantLine: true, wantWarning: true},+ {name: "audited failed in handler", audited: true, outcome: outcomeFailedInHandler, wantRow: true, wantLine: true},+ {name: "audited refused by ACL", audited: true, outcome: outcomeRefusedByACL, wantLine: true, wantWarning: true},+ {name: "audited failed in ACL", audited: true, outcome: outcomeFailedInACL},+ {name: "unaudited ok", outcome: outcomeOK},+ {name: "unaudited refused in handler", outcome: outcomeRefusedInHandler, wantLine: true, wantWarning: true},+ {name: "unaudited failed in handler", outcome: outcomeFailedInHandler},+ {name: "unaudited refused by ACL", outcome: outcomeRefusedByACL, wantLine: true, wantWarning: true},+ {name: "audited validate-only ok", audited: true, validateOnly: true, outcome: outcomeOK},+ {name: "audited validate-only refused in handler", audited: true, validateOnly: true, outcome: outcomeRefusedInHandler, wantRow: true, wantLine: true, wantWarning: true},+ {name: "audited validate-only refused by ACL", audited: true, validateOnly: true, outcome: outcomeRefusedByACL, wantLine: true, wantWarning: true},+ }+ internalCases := []sinkCase{+ {name: "audited refused by gate", audited: true, outcome: outcomeRefusedByGate, wantLine: true, wantWarning: true},+ {name: "unaudited refused by gate", outcome: outcomeRefusedByGate, wantLine: true, wantWarning: true},+ {name: "audited validate-only refused by gate", audited: true, validateOnly: true, outcome: outcomeRefusedByGate, wantLine: true, wantWarning: true},+ }++ handler := func(outcome auditCallOutcome) connect.UnaryFunc {+ return func(ctx context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {+ switch outcome {+ case outcomeRefusedInHandler:+ common.SetPermissionDenied(ctx)+ return nil, connect.NewError(connect.CodePermissionDenied, errors.New("permission denied"))+ case outcomeFailedInHandler:+ return nil, connect.NewError(connect.CodeFailedPrecondition, errors.New("setting is locked"))+ case outcomeOK:+ return connect.NewResponse(&v1pb.Setting{Name: "settings/AI"}), nil+ default:+ return nil, errors.Errorf("handler reached on outcome %d", outcome)+ }}- handler := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {- handlerReached = true- return connect.NewResponse(&v1pb.IamPolicy{}), nil+ }++ for _, chain := range []string{"public", "internal"} {+ chainCases := cases+ if chain == "internal" {+ chainCases = append(slices.Clone(cases), internalCases...)}- // The internal chain's order: audit outside, ACL inside.- chain := auditIn.WrapUnary(aclIn.WrapUnary(handler))- req := &specRequest{- AnyRequest: connect.NewRequest(&v1pb.SetIamPolicyRequest{Resource: resource}),- procedure: "/bytebase.v1.WorkspaceService/SetIamPolicy",+ for _, stdout := range []bool{true, false} {+ for _, tc := range chainCases {+ name := fmt.Sprintf("%s/stdout=%t/%s", chain, stdout, tc.name)+ t.Run(name, func(t *testing.T) {+ in, writer := newRecordingAuditInterceptor()+ in.profile.RuntimeEnableAuditLogStdout.Store(stdout)++ settingName := "settings/AI"+ switch tc.outcome {+ case outcomeRefusedByACL:+ settingName = "workspaces/other-ws/settings/AI"+ case outcomeFailedInACL:+ settingName = "workspaces/"+ default:+ }+ authCtx := &common.AuthContext{+ Audit: tc.audited,+ AuthMethod: common.AuthMethodCustom,+ MCPMethodClass: v1pb.MCPMethodClass_WRITE,+ }+ var call connect.UnaryFunc+ if chain == "public" {+ call = in.WrapUnary(aclIn.WrapUnary(handler(tc.outcome)))+ } else {+ authCtx.DelegatedGrant = &common.DelegatedGrant{CorrelationID: name, Scope: "mcp:read-write"}+ if tc.outcome == outcomeRefusedByGate {+ authCtx.MCPMethodClass = v1pb.MCPMethodClass_FORBIDDEN+ }+ call = in.WrapUnary(gate.WrapUnary(aclIn.WrapUnary(handler(tc.outcome))))+ }+ request := &v1pb.UpdateSettingRequest{+ Setting: &v1pb.Setting{Name: settingName},+ ValidateOnly: tc.validateOnly,+ }+ before := len(lines())+ _, err := call(newAuditTestContext(authCtx), &specRequest{AnyRequest: connect.NewRequest(request), procedure: procedure})+ if tc.outcome == outcomeOK {+ require.NoError(t, err)+ } else {+ require.Error(t, err)+ }+ got := lines()[before:]+ rows := writer.stored()++ if tc.wantRow {+ require.Len(t, rows, 1, "the call must be stored")+ } else {+ require.Empty(t, rows, "the call must not be stored")+ }+ if !stdout || !tc.wantLine {+ require.Empty(t, got, "the call must not be streamed")+ return+ }+ require.Len(t, got, 1, "the call must be streamed once")+ line := got[0]+ require.Equal(t, procedure, line["method"])+ require.Equal(t, "workspaces/"+auditTestWorkspace, line["parent"],+ "a refused call is filed under the caller's workspace, never the one it named")+ require.Equal(t, common.FormatUserEmail(auditTestUser().Email), line["user"])+ wantSeverity := storepb.AuditLog_INFO+ if tc.wantWarning {+ wantSeverity = storepb.AuditLog_WARNING+ require.InDelta(t, float64(connect.CodePermissionDenied), line["status_code"], 0)+ }+ require.Equal(t, wantSeverity.String(), line["severity"])+ if chain == "internal" {+ require.Equal(t, name, line["mcp_correlation_id"])+ }+ if len(rows) == 1 {+ // One built row feeds both sinks.+ require.Equal(t, wantSeverity, rows[0].payload.Severity)+ require.Equal(t, rows[0].payload.Request, line["request"])+ }+ })+ }}- _, rerr = chain(newAuditTestContext(authCtx), req)- return handlerReached, rerr}+}- t.Run("an ACL denial produces a provenance-carrying denied row", func(t *testing.T) {- handlerReached, err := invoke(t, true, "corr-denied", "workspaces/other-ws")- require.Error(t, err)- require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err))- require.False(t, handlerReached, "the denial must come from the ACL interceptor, not the handler")-- matched := rowsByCorrelation(*rows, "corr-denied")- require.Len(t, matched, 1, "an ACL-denied internal-chain call must still produce an audit row")- row := matched[0]- require.Equal(t, "workspaces/"+auditTestWorkspace, row.Parent,- "a denied cross-workspace attempt must be audited under the CALLER's workspace, never the foreign one it named")- require.Equal(t, "/bytebase.v1.WorkspaceService/SetIamPolicy", row.Method)- require.Equal(t, common.FormatUserEmail(auditTestUser().Email), row.User)- require.NotNil(t, row.Status, "the row must reflect the denial")- require.Equal(t, int32(connect.CodePermissionDenied), row.Status.Code)- require.Equal(t, "mcp:read-only", row.GetMcpDelegation().GetScope())- })-- t.Run("a method opted out of auditing stays silent for denials too", func(t *testing.T) {- _, err := invoke(t, false, "corr-optout", "workspaces/other-ws")- require.Error(t, err)- require.Empty(t, rowsByCorrelation(*rows, "corr-optout"),- "audit opt-out must behave consistently for permitted and denied calls")- })-- t.Run("a permitted call is audited exactly once", func(t *testing.T) {- handlerReached, err := invoke(t, true, "corr-permitted", "workspaces/"+auditTestWorkspace)- require.NoError(t, err)- require.True(t, handlerReached)+// TestAuditStreamSurvivesAFailedInsert pins that every line is written before+// any insert, and once, so a metadata database that refuses the row does not+// take the stream line with it. The call names two parents, so a per-row+// "log it, insert it" loop that stops at the first failure fails here. Not+// parallel: it captures slog.Default.+func TestAuditStreamSurvivesAFailedInsert(t *testing.T) {+ lines := captureAuditStream(t)+ in, writer := newRecordingAuditInterceptor()+ writer.err = errors.New("audit_log insert failed")+ writer.onInsert = func() {+ require.Len(t, lines(), 2, "every line must be on the stream before any insert runs")+ }+ in.profile.RuntimeEnableAuditLogStdout.Store(true)- matched := rowsByCorrelation(*rows, "corr-permitted")- require.Len(t, matched, 1)- require.Nil(t, matched[0].Status, "a permitted call keeps its success status")- })+ next := func(context.Context, connect.AnyRequest) (connect.AnyResponse, error) {+ return connect.NewResponse(&v1pb.QueryResponse{}), nil+ }+ req := &specRequest{+ AnyRequest: connect.NewRequest(&v1pb.QueryRequest{Name: "instances/i/databases/d"}),+ procedure: "/bytebase.v1.SQLService/Query",+ }+ authCtx := &common.AuthContext{+ Audit: true,+ Resources: []*common.Resource{+ {Type: common.ResourceTypeProject, ID: "proj-insert-failed"},+ {Type: common.ResourceTypeWorkspace, ID: auditTestWorkspace},+ },+ }+ _, err := in.WrapUnary(admitted(next))(newAuditTestContext(authCtx), req)+ require.NoError(t, err, "a failed audit insert must not fail the call")+ require.Len(t, writer.stored(), 1, "the insert stops at the first failure")+ require.Len(t, lines(), 2, "both lines are written, whatever the insert did")}// TestValidateOnlyAuditSkipAppliesOnlyToSuccess pins the boundary of the@@ -504,7 +720,7 @@ func TestValidateOnlyAuditSkipAppliesOnlyToSuccess(t *testing.T) {AnyRequest: connect.NewRequest(request),procedure: "/bytebase.v1.InstanceService/UpdateDataSource",}- _, err := in.WrapUnary(next)(newAuditTestContext(authCtx), req)+ _, err := in.WrapUnary(admitted(next))(newAuditTestContext(authCtx), req)if rerr == nil {require.NoError(t, err)} else {@@ -514,10 +730,10 @@ func TestValidateOnlyAuditSkipAppliesOnlyToSuccess(t *testing.T) {t.Run("a denied validate-only call is recorded", func(t *testing.T) {denial := connect.NewError(connect.CodePermissionDenied,- errors.New("InstanceService/UpdateDataSource is not available to MCP sessions"))+ errors.New("permission denied for the data source"))invoke(t, "corr-validate-only-denied", retargetRequest(true), denial)- rows := rowsByCorrelation(*captured, "corr-validate-only-denied")+ rows := rowsByCorrelation(captured.stored(), "corr-validate-only-denied")require.Len(t, rows, 1,"a refused attempt must be auditable whether or not validate_only was set — "+"otherwise the flag is a switch that turns off the record")@@ -546,7 +762,7 @@ func TestValidateOnlyAuditSkipAppliesOnlyToSuccess(t *testing.T) {t.Run("a succeeding validate-only call stays silent", func(t *testing.T) {invoke(t, "corr-validate-only-ok", retargetRequest(true), nil)- require.Empty(t, rowsByCorrelation(*captured, "corr-validate-only-ok"),+ require.Empty(t, rowsByCorrelation(captured.stored(), "corr-validate-only-ok"),"a dry run that succeeded changed nothing — the original reason for the skip")})@@ -559,7 +775,7 @@ func TestValidateOnlyAuditSkipAppliesOnlyToSuccess(t *testing.T) {errors.New("failed to connect to attacker.example.com: connection refused"))invoke(t, "corr-validate-only-dial-failed", retargetRequest(true), failedDial)- rows := rowsByCorrelation(*captured, "corr-validate-only-dial-failed")+ rows := rowsByCorrelation(captured.stored(), "corr-validate-only-dial-failed")require.Len(t, rows, 1,"keying on a denial code would drop every other rejected attempt — the hole this change closes")require.Equal(t, int32(connect.CodeInvalidArgument), rows[0].GetStatus().GetCode())@@ -569,7 +785,7 @@ func TestValidateOnlyAuditSkipAppliesOnlyToSuccess(t *testing.T) {denial := connect.NewError(connect.CodePermissionDenied, errors.New("denied"))invoke(t, "corr-plain-denied", retargetRequest(false), denial)- rows := rowsByCorrelation(*captured, "corr-plain-denied")+ rows := rowsByCorrelation(captured.stored(), "corr-plain-denied")require.Len(t, rows, 1, "control: the flag is the only difference between this and the first case")require.True(t, strings.Contains(rows[0].GetMethod(), "UpdateDataSource"))})diff --git a/backend/api/v1/common.go b/backend/api/v1/common.goindex 5a5500ac2d..9f09daa662 100644--- a/backend/api/v1/common.go+++ b/backend/api/v1/common.go@@ -9,6 +9,7 @@ import ("connectrpc.com/connect""github.com/pkg/errors""google.golang.org/protobuf/proto"+ "google.golang.org/protobuf/types/known/timestamppb""github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store"@@ -351,12 +352,16 @@ type pageSize struct {type pageOffset struct {limit intoffset int+ // createTimeUpperBound is set by a list whose own reads write rows it+ // would page over. Inclusive.+ createTimeUpperBound *timestamppb.Timestamp}func (p *pageOffset) getNextPageToken() (string, error) {return marshalPageToken(&storepb.PageToken{- Limit: int32(p.limit),- Offset: int32(p.offset + p.limit),+ Limit: int32(p.limit),+ Offset: int32(p.offset + p.limit),+ CreateTimeUpperBound: p.createTimeUpperBound,})}@@ -372,6 +377,7 @@ func parseLimitAndOffset(size *pageSize) (*pageOffset, error) {}offset.limit = int(size.limit)offset.offset = int(token.Offset)+ offset.createTimeUpperBound = token.CreateTimeUpperBound} else {offset.limit = int(size.limit)}diff --git a/backend/api/v1/mcp_gate.go b/backend/api/v1/mcp_gate.goindex 794a38d595..64a7633265 100644--- a/backend/api/v1/mcp_gate.go+++ b/backend/api/v1/mcp_gate.go@@ -305,46 +305,29 @@ func mcpSettingsForCurrentWorkspace(ctx context.Context, reader mcpSettingsReade//// It belongs to the internal MCP chain only — every request there originates at// /mcp — and sits inside the audit interceptor, outside ACL: the ceiling-// refuses regardless of what RBAC would have said, and the refusal is recorded.+// refuses regardless of what RBAC would have said, and the refusal is streamed.//// The ceiling is read live, per request, with no caching anywhere in the path// (store.GetMCPSettingsUncached, which resolves the masking toggle off the same// row). An admin tightening the ceiling binds the next request of a session// already open; work already admitted finishes.//-// A policy denial is recorded whatever the method's audit annotation says: the-// gate marks the outcome and the audit interceptor records it (see-// common.SetMCPPolicyDenied). 39 of the 113 refused methods carry no audit-// annotation — the 4 FORBIDDEN ones that were silent before this gate-// (Refresh, SwitchWorkspace, TestIdentityProvider, TestEmailSetting) plus 35-// EXCLUDED ones — and TestEmailSetting and TestIdentityProvider are the rows an-// operator would most want, since each would have carried a stored secret to an-// address the agent chose. Recording requests that were never recorded is why-// redaction has to cover more than the audited RPCs (audit.go): a denial must-// not transcribe the secret it refused.+// A policy denial is marked (common.SetPermissionDenied), so the audit+// interceptor streams it whatever the method's audit annotation says. That is+// why redaction covers every refused request, not only the audited RPCs.//// A ceiling the gate cannot act on splits in two, and the split is the same one// the /mcp connection gate makes. A stored value this build cannot interpret —// a mistyped enum name, a wrong-typed row — is a policy refusal: it will never-// succeed on retry, so it answers CodePermissionDenied and is audited, and the+// succeed on retry, so it answers CodePermissionDenied and is marked, and the// connection gate answers 403 for it. A read that FAILED is an outage: it// answers CodeUnavailable, is not marked as a policy denial, and the connection// gate answers 503, the same way it already answers 503 rather than 401 when it// cannot resolve the token audience. Both refuse — an unknown policy never// permits — and neither is allowed to describe itself as the other.//-// Two gaps survive this PR, and both are worth knowing because nothing in the-// annotations shows either.-//-// - RequestPasswordReset, ResetPassword and SendEmailLoginCode are-// allow_without_credential, so createAuditLog takes their audit parent ONLY-// from what the handler announced (handlerValidatedWorkspaceMethod,-// audit.go) — an unvalidated workspace on an unauthenticated method would-// let anyone write rows into someone else's. This gate refuses before-// dispatch, so the handler never runs and no parent is ever set. Their-// denials are still silent, TestMCPResetFlowDenialsAreSilent pins that they-// are, and closing it means letting the audit path trust the workspace of-// the delegated credential the internal chain already verified.+// One gap survives, and it is worth knowing because nothing in the annotations+// shows it.//// - Two refusals this gate is the right slot for the CLASS of, but not the// right place for the decision, because the fact they turn on is not in the@@ -376,11 +359,10 @@ func (in *internalMCPGateInterceptor) WrapUnary(next connect.UnaryFunc) connect.return next(ctx, req)}if policyDenial {- // Record the refusal before returning it: the audit interceptor- // wraps this one and reads the mark when the request comes back- // out. Only a verdict about the caller is marked — an unreadable- // ceiling and a broken chain are not policy denials.- common.SetMCPPolicyDenied(ctx)+ // The audit interceptor wraps this one and reads the mark when the+ // request comes back out. Only a verdict about the caller is marked —+ // an unreadable ceiling and a broken chain are not policy denials.+ common.SetPermissionDenied(ctx)}return nil, err}@@ -411,7 +393,7 @@ func (*internalMCPGateInterceptor) WrapStreamingHandler(connect.StreamingHandler// refuse returns the error the gate refuses this request with, or nil to let it// through to ACL. The bool reports whether the refusal is a verdict about the-// caller, which is an audited outcome; an infrastructure failure is not.+// caller, which is marked; an infrastructure failure is not.//// The context it returns carries the MCP settings this request was held// against, for the enforcement points that read the request's argument rather@@ -479,7 +461,7 @@ func (in *internalMCPGateInterceptor) refuseByCeiling(ctx context.Context, proce// the state and the remedy, never the error. What it does get is the right// KIND of answer, because the two failures are opposites for a client. A// stored value this build cannot interpret never succeeds on retry — an- // admin has to rewrite it — so it is a policy refusal and an audited one.+ // admin has to rewrite it — so it is a policy refusal and a marked one.// A read that failed is an outage: retryable, and not a verdict about the// caller.//@@ -539,7 +521,7 @@ func describeServedClasses(served []v1pb.MCPMethodClass) string {//// This lives inside the gate rather than beside it. The decision is the same// decision — may this MCP session make this call? — and it has to be taken at-// the same point in the chain, so that the denial is recorded the same way, is+// the same point in the chain, so that the denial is marked the same way, is// worded the same way, and reaches the caller before any handler side effect// can land. A second interceptor would duplicate the slot, the message, and the// audit mark to serve one method, and it would put the exception somewhere thediff --git a/backend/api/v1/mcp_gate_test.go b/backend/api/v1/mcp_gate_test.goindex 0bde0e1463..c11155aee2 100644--- a/backend/api/v1/mcp_gate_test.go+++ b/backend/api/v1/mcp_gate_test.go@@ -836,7 +836,7 @@ type mcpGateResult struct {// invokeMCPGate runs one request through the gate alone. auditMarked is what// the audit interceptor reads when the request comes back out; standing in for// it here keeps the class rules provable without a database, and-// TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation proves the real+// TestMCPGateRefusalIsStreamedWithoutAnAuditAnnotation proves the real// interceptor honors the mark.func invokeMCPGate(t *testing.T, stores mcpSettingsReader, authCtx *common.AuthContext, procedure string, req connect.AnyRequest) mcpGateResult {t.Helper()@@ -851,7 +851,7 @@ func invokeMCPGate(t *testing.T, stores mcpSettingsReader, authCtx *common.AuthCctx = context.WithValue(ctx, common.AuthContextKey, authCtx)}ctx = context.WithValue(ctx, common.WorkspaceIDContextKey, auditTestWorkspace)- ctx = common.WithSetMCPPolicyDenied(ctx, func() { out.auditMarked = true })+ ctx = common.WithSetPermissionDenied(ctx, func() { out.auditMarked = true })_, out.err = NewInternalMCPGateInterceptor(stores).WrapUnary(next)(ctx,&specRequest{AnyRequest: req, procedure: procedure})return out@@ -1164,26 +1164,24 @@ func TestMCPGateRefusesGrantIssues(t *testing.T) {})}-// TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation is the typed-// policy-denial record, end to end through the real audit interceptor and a-// real store. Every method below is refused and carries no audit annotation, so-// its denial produced nothing at all before the gate started marking its own-// refusals: needAudit reads the annotation and nothing else.+// TestMCPGateRefusalIsStreamedWithoutAnAuditAnnotation is the policy-denial+// line, end to end through the real audit interceptor. Every method below is+// refused and carries no audit annotation, so its refusal is never stored and+// reaches the stdout stream only because the gate marks it.//-// The first four are the FORBIDDEN half of that population, which was the whole-// of it while the gate refused FORBIDDEN alone. Enforcing EXCLUDED raises it to-// 47, and TestWebhook is the fifth case here because it is the member the wider-// population added whose request carries a credential.+// TestWebhook is here because it is an EXCLUDED method whose request carries a+// credential.//// Each request below carries a secret or an unbounded body, which is why the-// rows are checked for what they wrote as well as that they wrote. The gate-// refuses before dispatch, so nothing in them was ever used — recording one-// verbatim would turn a silent denial into a worse one.+// lines are checked for what they wrote as well as that they wrote. The gate+// refuses before dispatch, so nothing in them was ever used — streaming one+// verbatim would turn a refusal into a leak.//-//nolint:tparallel // Subtests share the parent's fixture.-func TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation(t *testing.T) {- t.Parallel()+// Not parallel: it captures slog.Default.+func TestMCPGateRefusalIsStreamedWithoutAnAuditAnnotation(t *testing.T) {+ lines := captureAuditStream(t)auditIn, captured := newRecordingAuditInterceptor()+ auditIn.profile.RuntimeEnableAuditLogStdout.Store(true)gate := NewInternalMCPGateInterceptor(readWriteCeiling())invoke := func(t *testing.T, correlationID, procedure string, req connect.AnyRequest) {@@ -1209,22 +1207,30 @@ func TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation(t *testing.T) {require.False(t, handlerReached)}- assertOneDeniedRow := func(t *testing.T, correlationID, procedure string) *storepb.AuditLog {+ // assertOneRefusalLine returns the request the refusal's line carries.+ assertOneRefusalLine := func(t *testing.T, correlationID, procedure string) string {t.Helper()- rows := rowsByCorrelation(*captured, correlationID)- require.Len(t, rows, 1, "a policy denial must be recorded even where the method asks for no audit row")- row := rows[0]- require.Equal(t, procedure, row.Method)- require.Equal(t, int32(connect.CodePermissionDenied), row.GetStatus().GetCode())- require.Equal(t, "workspaces/"+auditTestWorkspace, row.Parent)- require.Equal(t, "mcp:read-write", row.GetMcpDelegation().GetScope(),- "the row must carry the MCP provenance an operator filters on")- return row+ require.Empty(t, rowsByCorrelation(captured.stored(), correlationID), "a refusal is never stored")+ var matched []map[string]any+ for _, line := range lines() {+ if line["mcp_correlation_id"] == correlationID {+ matched = append(matched, line)+ }+ }+ require.Len(t, matched, 1, "a policy denial must be streamed even where the method asks for no audit row")+ line := matched[0]+ require.Equal(t, procedure, line["method"])+ require.InDelta(t, float64(connect.CodePermissionDenied), line["status_code"], 0)+ require.Equal(t, "workspaces/"+auditTestWorkspace, line["parent"])+ require.Equal(t, storepb.AuditLog_WARNING.String(), line["severity"])+ require.Equal(t, "mcp:read-write", line["mcp_scope"],+ "the line must carry the MCP provenance an operator filters on")+ return lineText(line, "request")}t.Run("Refresh", func(t *testing.T) {invoke(t, "corr-refresh", v1connect.AuthServiceRefreshProcedure, connect.NewRequest(&v1pb.RefreshRequest{}))- assertOneDeniedRow(t, "corr-refresh", v1connect.AuthServiceRefreshProcedure)+ assertOneRefusalLine(t, "corr-refresh", v1connect.AuthServiceRefreshProcedure)})t.Run("SwitchWorkspace", func(t *testing.T) {@@ -1239,20 +1245,24 @@ func TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation(t *testing.T) {RecoveryCode: &recovery,MfaTempToken: &temp,}))- row := assertOneDeniedRow(t, "corr-switch", v1connect.AuthServiceSwitchWorkspaceProcedure)+ request := assertOneRefusalLine(t, "corr-switch", v1connect.AuthServiceSwitchWorkspaceProcedure)for _, proof := range []string{otp, recovery, temp} {- require.NotContains(t, row.Request, proof, "an MFA proof must not be transcribed into the row")+ require.NotContains(t, request, proof, "an MFA proof must not be transcribed into the line")}})// One subtest per oneof arm: each arm masks a different credential, and// only the arm the request carries runs.+ const idpIssuer = "https://collector.attacker.example.com"idpRequest := func() *v1pb.TestIdentityProviderRequest {return &v1pb.TestIdentityProviderRequest{IdentityProvider: &v1pb.IdentityProvider{Config: &v1pb.IdentityProviderConfig{Config: &v1pb.IdentityProviderConfig_OidcConfig{- OidcConfig: &v1pb.OIDCIdentityProviderConfig{ClientSecret: "idp-client-secret"},+ OidcConfig: &v1pb.OIDCIdentityProviderConfig{+ Issuer: idpIssuer,+ ClientSecret: "idp-client-secret",+ },},},},@@ -1280,21 +1290,23 @@ func TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation(t *testing.T) {correlationID := "corr-idp-" + nameinvoke(t, correlationID, v1connect.IdentityProviderServiceTestIdentityProviderProcedure,connect.NewRequest(arm.request))- row := assertOneDeniedRow(t, correlationID, v1connect.IdentityProviderServiceTestIdentityProviderProcedure)- require.NotContains(t, row.Request, "idp-client-secret")- require.NotContains(t, row.Request, arm.secret)+ request := assertOneRefusalLine(t, correlationID, v1connect.IdentityProviderServiceTestIdentityProviderProcedure)+ require.NotContains(t, request, "idp-client-secret")+ require.NotContains(t, request, arm.secret)+ require.Contains(t, request, "collector.attacker.example.com",+ "the issuer the agent named is the point of the line and stays readable")})}t.Run("TestIdentityProvider/empty context arm", func(t *testing.T) {// A oneof wrapper set with a nil payload. It cannot arrive over JSON,// but the audit path must not panic on a message shape the type system- // permits — a panic here costs the row the gate exists to write.+ // permits — a panic here costs the line the gate exists to write.invoke(t, "corr-idp-nil", v1connect.IdentityProviderServiceTestIdentityProviderProcedure,connect.NewRequest(&v1pb.TestIdentityProviderRequest{Context: &v1pb.TestIdentityProviderRequest_LdapContext{},}))- assertOneDeniedRow(t, "corr-idp-nil", v1connect.IdentityProviderServiceTestIdentityProviderProcedure)+ assertOneRefusalLine(t, "corr-idp-nil", v1connect.IdentityProviderServiceTestIdentityProviderProcedure)})t.Run("TestWebhook", func(t *testing.T) {@@ -1307,22 +1319,21 @@ func TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation(t *testing.T) {Project: "projects/p",Webhook: &v1pb.Webhook{Url: "https://hooks.example.com/T000/B000/secret-token"},}))- row := assertOneDeniedRow(t, "corr-webhook", v1connect.ProjectServiceTestWebhookProcedure)- require.NotContains(t, row.Request, "secret-token",- "the webhook URL is a bearer credential and must not be transcribed into the row")+ request := assertOneRefusalLine(t, "corr-webhook", v1connect.ProjectServiceTestWebhookProcedure)+ require.NotContains(t, request, "secret-token",+ "the webhook URL is a bearer credential and must not be transcribed into the line")})aiChatBody := "the whole conversation the caller sent"t.Run("AIService/Chat", func(t *testing.T) {- // Not a secret but an unbounded body, and the audit row stores it- // whole — the size cap applies to the stdout logger only. A denial- // needs the fact of the call, not the transcript.+ // Not a secret but an unbounded body. A denial needs the fact of the+ // call, not the transcript.invoke(t, "corr-ai", v1connect.AIServiceChatProcedure,connect.NewRequest(&v1pb.AIChatRequest{Messages: []*v1pb.AIChatMessage{{Content: &aiChatBody}},}))- row := assertOneDeniedRow(t, "corr-ai", v1connect.AIServiceChatProcedure)- require.NotContains(t, row.Request, aiChatBody)+ request := assertOneRefusalLine(t, "corr-ai", v1connect.AIServiceChatProcedure)+ require.NotContains(t, request, aiChatBody)})t.Run("TestEmailSetting", func(t *testing.T) {@@ -1336,10 +1347,10 @@ func TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation(t *testing.T) {},},}))- row := assertOneDeniedRow(t, "corr-email", v1connect.SettingServiceTestEmailSettingProcedure)- require.NotContains(t, row.Request, "relay-password")- require.Contains(t, row.Request, "smtp.attacker.example",- "the host the agent named is the point of the row and stays readable")+ request := assertOneRefusalLine(t, "corr-email", v1connect.SettingServiceTestEmailSettingProcedure)+ require.NotContains(t, request, "relay-password")+ require.Contains(t, request, "smtp.attacker.example",+ "the host the agent named is the point of the line and stays readable")})}@@ -1352,13 +1363,13 @@ type mcpDenialRequestReview struct {// mcpDenialRequestsUnderReview is the population the redaction sweep has to// consider: every method carrying NO audit annotation whose request holds-// anything beyond the AIP vocabulary below. Before the gate, none of these-// produced an audit row at all; now a denial writes one, so whatever the-// request holds is what lands in the audit_log table.+// anything beyond the AIP vocabulary below. A refusal of any of them is+// streamed to stdout, so whatever the request holds is what lands in the+// stdout audit log.//-// It covers READ and WRITE as well as the refused classes, because the gate can-// refuse those too — an uninterpretable stored ceiling refuses whatever the-// method's class is, and that refusal is a policy denial like any other.+// It covers every class, because ACL on either chain and the ceiling gate can+// refuse any of them — an uninterpretable stored ceiling refuses whatever the+// method's class is, and that refusal is a permission refusal like any other.//// The decision is keyed on (method, field), not on the field name alone. A name// exempted globally is an exemption for every future method that happens to@@ -1555,8 +1566,9 @@ func TestLintDenialRequestsAreReviewedForRedaction(t *testing.T) {}slices.Sort(undecided)require.Empty(t, undecided,- "these methods are unaudited and the gate can refuse them, so a denial writes their request into an "+- "audit row: decide what may be recorded and record it in mcpDenialRequestsUnderReview")+ "these methods are unaudited and a permission check can refuse them, so a refusal streams their "++ "request to the stdout audit log: decide what may be recorded and record it in "++ "mcpDenialRequestsUnderReview")var stale []stringfor procedure := range mcpDenialRequestsUnderReview {diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex 12a4885666..0dea556d7e 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -346,13 +346,11 @@ func (s *SQLService) Query(ctx context.Context, req *connect.Request[v1pb.QueryR}if clamped {if err := refuseNonReadOnlyStatement(instance.Metadata.GetEngine(), statement); err != nil {- // The same kind of refusal the ceiling gate records, taken at the- // one point that can see the request's argument. It adds no row- // today — Query is annotated audit = true, so the denial is- // recorded either way — and it is here so that stays true if that- // annotation ever changes, which is the only thing the mark- // controls.- common.SetMCPPolicyDenied(ctx)+ // The same kind of refusal the ceiling gate marks, taken at the+ // one point that can see the request's argument. Query is audited+ // and this runs in its handler, so the row is stored either way;+ // the mark stamps it WARNING.+ common.SetPermissionDenied(ctx)return nil, err}}diff --git a/backend/common/audit.go b/backend/common/audit.goindex 8a33025280..ef300cd456 100644--- a/backend/common/audit.go+++ b/backend/common/audit.go@@ -19,7 +19,7 @@ import (// surface the protos define and where the next reader would go looking for a// descriptor. component/recovery already names its rows the same way, under// bytebase.cli. The method part names what was ATTEMPTED — the row's status-// carries the verdict, exactly as for an RPC that ACL refused.+// carries the verdict.const (// AuditMethodMCPSessionAuthorize is a bearer token presented at /mcp and// held against the workspace ceiling. Per REQUEST, not per session: /mcp is@@ -32,8 +32,8 @@ const (AuditMethodMCPConsentApprove = "/bytebase.mcp.Consent/Approve")-// AuditLogWriter is the one store method a door outside the connect chains-// needs to record its own denial.+// AuditLogWriter inserts one audit row. The v1 audit interceptor and the doors+// outside the connect chains write through it.type AuditLogWriter interface {CreateAuditLog(ctx context.Context, workspace string, payload *storepb.AuditLog) error}@@ -41,11 +41,20 @@ type AuditLogWriter interface {// RecordOutOfBandAudit writes a row for a refusal no interceptor sees, and// mirrors it to stdout when that is enabled.//+// It stamps the severity itself. Every caller records a policy refusal, which+// the audit interceptor stamps WARNING when it makes the same call inside a+// connect chain, and a compliance reader filters on that field alone. Setting+// it here rather than at each door keeps a new door from answering INFO for a+// refusal the gate calls WARNING. A caller that needs to record something other+// than a refusal needs its own writer, not an argument here.+//// Best effort by design: an audit row that cannot be written must never turn a// refusal into an admission, so the caller gets no error to act on. The stdout// mirror does NOT depend on the insert — a metadata-database failure is when// losing the row from both surfaces would matter most.func RecordOutOfBandAudit(ctx context.Context, writer AuditLogWriter, mirrorToStdout bool, workspace string, row *storepb.AuditLog) {+ row.Severity = storepb.AuditLog_WARNING+// WithoutCancel so the row survives a client hanging up on its own refusal,// bounded because WithoutCancel drops the request deadline too. Both// callers write on the synchronous path of a refusal already decided, so an@@ -78,11 +87,10 @@ const maxAuditPayloadChars = 102400// Logs include a "log_type": "audit" field to distinguish from application logs.// This is a best-effort operation - errors are not returned to avoid failing the audit flow.//-// Every writer in the server process calls this when stdout audit is enabled:-// the stream is a mirror of the table, and a row only one of them carries is a-// row an operator reading the other cannot see. The recovery CLI-// (component/recovery) is the exception — it writes without a profile to read-// the flag from.+// Every writer in the server process calls this when stdout audit is enabled,+// so the stream carries every stored row. It also carries the v1 calls a+// permission check refused. The recovery CLI (component/recovery) is the+// exception — it writes without a profile to read the flag from.func LogAuditToStdout(ctx context.Context, p *storepb.AuditLog) {attrs := []slog.Attr{slog.String("log_type", "audit"),diff --git a/backend/common/audit_test.go b/backend/common/audit_test.goindex 52b4f06536..a2a95356c3 100644--- a/backend/common/audit_test.go+++ b/backend/common/audit_test.go@@ -127,3 +127,44 @@ func TestLogAuditToStdoutFormat(t *testing.T) {a.Equal("https://bb.example.com/mcp", delegated["mcp_resource"])a.Equal("client-A", delegated["mcp_client_id"])}++// recordingWriter captures the row RecordOutOfBandAudit inserts.+type recordingWriter struct {+ rows []*storepb.AuditLog+}++func (w *recordingWriter) CreateAuditLog(_ context.Context, _ string, row *storepb.AuditLog) error {+ w.rows = append(w.rows, row)+ return nil+}++// TestRecordOutOfBandAuditStampsWarning pins that a door outside the connect+// chains cannot file a refusal the compliance reader's severity filter misses.+// The /mcp connection and OAuth consent doors both refuse on the same ceiling+// verdict the per-request gate does, and the gate's refusal is WARNING.+func TestRecordOutOfBandAuditStampsWarning(t *testing.T) {+ a := require.New(t)++ prev := slog.Default()+ t.Cleanup(func() { slog.SetDefault(prev) })+ var buf bytes.Buffer+ slog.SetDefault(slog.New(slog.NewJSONHandler(&buf, nil)))++ writer := &recordingWriter{}+ row := &storepb.AuditLog{+ Parent: "workspaces/ws-abc",+ Method: AuditMethodMCPSessionAuthorize,+ User: "users/[email protected]",+ Status: &spb.Status{Code: 7, Message: "MCP access is turned off"},+ }++ RecordOutOfBandAudit(context.Background(), writer, true, "ws-abc", row)++ a.Len(writer.rows, 1)+ a.Equal(storepb.AuditLog_WARNING, writer.rows[0].Severity, "a refusal is stored as a warning")++ var line map[string]any+ a.NoError(json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &line))+ a.Equal(storepb.AuditLog_WARNING.String(), line["severity"],+ "the stream carries the same severity as the row")+}diff --git a/backend/common/context.go b/backend/common/context.goindex 00253c83da..6963d1985a 100644--- a/backend/common/context.go+++ b/backend/common/context.go@@ -19,7 +19,8 @@ const (ServiceDataKeyWorkspaceIDContextKeyAuditWorkspaceIDKey- MCPPolicyDenialKey+ PermissionDeniedKey+ HandlerReachedKey)func WithSetServiceData(ctx context.Context, setServiceData func(a *anypb.Any)) context.Context {@@ -54,29 +55,36 @@ func SetAuditWorkspaceID(ctx context.Context, workspaceID string) {setter(workspaceID)}-// WithSetMCPPolicyDenied registers a callback the MCP ceiling gate uses to tell-// the audit interceptor that it refused this request. The gate runs inside the-// audit interceptor, so a value it puts on the context cannot travel back out;-// this is the same setter shape WithSetAuditWorkspaceID already uses for the-// same reason.-//-// The signal is needed because the audit interceptor otherwise writes a row-// only when the method's own audit annotation asks for one, and 47 of the-// methods the gate refuses carry no such annotation. A denial nobody can see is-// the outcome an operator most needs to see.-func WithSetMCPPolicyDenied(ctx context.Context, setMCPPolicyDenied func()) context.Context {- return context.WithValue(ctx, MCPPolicyDenialKey, setMCPPolicyDenied)+// WithSetPermissionDenied registers a callback that a permission check inside+// the audit interceptor uses to report that it refused the request. The check+// runs inside the interceptor, so a value it puts on the context cannot travel+// back out; WithSetAuditWorkspaceID has the same shape for the same reason.+func WithSetPermissionDenied(ctx context.Context, setPermissionDenied func()) context.Context {+ return context.WithValue(ctx, PermissionDeniedKey, setPermissionDenied)}-// SetMCPPolicyDenied records that the MCP ceiling gate refused the current-// request, if the audit interceptor registered a setter on the context. Safe to-// call when it did not: the public chain never runs the gate at all.-func SetMCPPolicyDenied(ctx context.Context) {- setter, ok := ctx.Value(MCPPolicyDenialKey).(func())- if !ok {- return+// SetPermissionDenied reports that a permission check refused the current+// request. Mark only a verdict about the caller's permission, not an outage, a+// license gate, a workflow state or an ownership rule.+func SetPermissionDenied(ctx context.Context) {+ if setter, ok := ctx.Value(PermissionDeniedKey).(func()); ok {+ setter()+ }+}++// WithSetHandlerReached registers a callback the ACL interceptor uses to report+// that it admitted the request to its handler.+func WithSetHandlerReached(ctx context.Context, setHandlerReached func()) context.Context {+ return context.WithValue(ctx, HandlerReachedKey, setHandlerReached)+}++// SetHandlerReached reports that the current request passed every interceptor+// check and is about to run its handler. The audit interceptor stores a row+// only for such a call.+func SetHandlerReached(ctx context.Context) {+ if setter, ok := ctx.Value(HandlerReachedKey).(func()); ok {+ setter()}- setter()}// GetWorkspaceIDFromContext returns the workspace ID from the request context.diff --git a/backend/generated-go/store/common.pb.go b/backend/generated-go/store/common.pb.goindex daac955d11..9a4f43bd53 100644--- a/backend/generated-go/store/common.pb.go+++ b/backend/generated-go/store/common.pb.go@@ -9,6 +9,7 @@ package storeimport (protoreflect "google.golang.org/protobuf/reflect/protoreflect"protoimpl "google.golang.org/protobuf/runtime/protoimpl"+ timestamppb "google.golang.org/protobuf/types/known/timestamppb"reflect "reflect"sync "sync"unsafe "unsafe"@@ -608,9 +609,12 @@ type PageToken struct {// Maximum number of items to return.Limit int32 `protobuf:"varint,1,opt,name=limit,proto3" json:"limit,omitempty"`// Number of items to skip before starting to return results.- Offset int32 `protobuf:"varint,2,opt,name=offset,proto3" json:"offset,omitempty"`- unknownFields protoimpl.UnknownFields- sizeCache protoimpl.SizeCache+ Offset int32 `protobuf:"varint,2,opt,name=offset,proto3" json:"offset,omitempty"`+ // Bounds later pages to the rows that existed when the traversal started,+ // inclusive. Set by a list whose own reads write rows it would page over.+ CreateTimeUpperBound *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=create_time_upper_bound,json=createTimeUpperBound,proto3" json:"create_time_upper_bound,omitempty"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache}func (x *PageToken) Reset() {@@ -657,6 +661,13 @@ func (x *PageToken) GetOffset() int32 {return 0}+func (x *PageToken) GetCreateTimeUpperBound() *timestamppb.Timestamp {+ if x != nil {+ return x.CreateTimeUpperBound+ }+ return nil+}+// Position in a text expressed as one-based line and one-based column.// We use 1-based numbering to match the majority of industry standards:// - Monaco Editor uses 1-based (https://microsoft.github.io/monaco-editor/typedoc/interfaces/IPosition.html)@@ -795,10 +806,11 @@ var File_store_common_proto protoreflect.FileDescriptorconst file_store_common_proto_rawDesc = "" +"\n" +- "\x12store/common.proto\x12\x0ebytebase.store\"9\n" ++ "\x12store/common.proto\x12\x0ebytebase.store\x1a\x1fgoogle/protobuf/timestamp.proto\"\x8c\x01\n" +"\tPageToken\x12\x14\n" +"\x05limit\x18\x01 \x01(\x05R\x05limit\x12\x16\n" +- "\x06offset\x18\x02 \x01(\x05R\x06offset\"6\n" ++ "\x06offset\x18\x02 \x01(\x05R\x06offset\x12Q\n" ++ "\x17create_time_upper_bound\x18\x03 \x01(\v2\x1a.google.protobuf.TimestampR\x14createTimeUpperBound\"6\n" +"\bPosition\x12\x12\n" +"\x04line\x18\x01 \x01(\x05R\x04line\x12\x16\n" +"\x06column\x18\x02 \x01(\x05R\x06column\"/\n" +@@ -939,23 +951,25 @@ func file_store_common_proto_rawDescGZIP() []byte {var file_store_common_proto_enumTypes = make([]protoimpl.EnumInfo, 7)var file_store_common_proto_msgTypes = make([]protoimpl.MessageInfo, 3)var file_store_common_proto_goTypes = []any{- (Engine)(0), // 0: bytebase.store.Engine- (VCSType)(0), // 1: bytebase.store.VCSType- (ExportFormat)(0), // 2: bytebase.store.ExportFormat- (RiskLevel)(0), // 3: bytebase.store.RiskLevel- (SchemaChangeType)(0), // 4: bytebase.store.SchemaChangeType- (WebhookType)(0), // 5: bytebase.store.WebhookType- (StatementType)(0), // 6: bytebase.store.StatementType- (*PageToken)(nil), // 7: bytebase.store.PageToken- (*Position)(nil), // 8: bytebase.store.Position- (*Range)(nil), // 9: bytebase.store.Range+ (Engine)(0), // 0: bytebase.store.Engine+ (VCSType)(0), // 1: bytebase.store.VCSType+ (ExportFormat)(0), // 2: bytebase.store.ExportFormat+ (RiskLevel)(0), // 3: bytebase.store.RiskLevel+ (SchemaChangeType)(0), // 4: bytebase.store.SchemaChangeType+ (WebhookType)(0), // 5: bytebase.store.WebhookType+ (StatementType)(0), // 6: bytebase.store.StatementType+ (*PageToken)(nil), // 7: bytebase.store.PageToken+ (*Position)(nil), // 8: bytebase.store.Position+ (*Range)(nil), // 9: bytebase.store.Range+ (*timestamppb.Timestamp)(nil), // 10: google.protobuf.Timestamp}var file_store_common_proto_depIdxs = []int32{- 0, // [0:0] is the sub-list for method output_type- 0, // [0:0] is the sub-list for method input_type- 0, // [0:0] is the sub-list for extension type_name- 0, // [0:0] is the sub-list for extension extendee- 0, // [0:0] is the sub-list for field type_name+ 10, // 0: bytebase.store.PageToken.create_time_upper_bound:type_name -> google.protobuf.Timestamp+ 1, // [1:1] is the sub-list for method output_type+ 1, // [1:1] is the sub-list for method input_type+ 1, // [1:1] is the sub-list for extension type_name+ 1, // [1:1] is the sub-list for extension extendee+ 0, // [0:1] is the sub-list for field type_name}func init() { file_store_common_proto_init() }diff --git a/backend/generated-go/store/common_equal.pb.go b/backend/generated-go/store/common_equal.pb.goindex ddb4201cdc..e9a588488c 100644--- a/backend/generated-go/store/common_equal.pb.go+++ b/backend/generated-go/store/common_equal.pb.go@@ -16,6 +16,9 @@ func (x *PageToken) Equal(y *PageToken) bool {if x.Offset != y.Offset {return false}+ if p, q := x.CreateTimeUpperBound, y.CreateTimeUpperBound; (p == nil && q != nil) || (p != nil && (q == nil || p.Seconds != q.Seconds || p.Nanos != q.Nanos)) {+ return false+ }return true}diff --git a/backend/generated-go/v1/audit_log_service.pb.go b/backend/generated-go/v1/audit_log_service.pb.goindex a897d55bdd..68347ca0e8 100644--- a/backend/generated-go/v1/audit_log_service.pb.go+++ b/backend/generated-go/v1/audit_log_service.pb.go@@ -779,10 +779,10 @@ const file_v1_audit_log_service_proto_rawDesc = "" +"\border_by\x18\x02 \x01(\tR\aorderBy\x12\x1b\n" +"\tpage_size\x18\x03 \x01(\x05R\bpageSize\x12\x1d\n" +"\n" +- "page_token\x18\x04 \x01(\tR\tpageToken\"w\n" +- "\x17SearchAuditLogsResponse\x124\n" ++ "page_token\x18\x04 \x01(\tR\tpageToken\"}\n" ++ "\x17SearchAuditLogsResponse\x12:\n" +"\n" +- "audit_logs\x18\x01 \x03(\v2\x15.bytebase.v1.AuditLogR\tauditLogs\x12&\n" ++ "audit_logs\x18\x01 \x03(\v2\x15.bytebase.v1.AuditLogB\x04\xd0\xea0\x02R\tauditLogs\x12&\n" +"\x0fnext_page_token\x18\x02 \x01(\tR\rnextPageToken\"\xf1\x01\n" +"\x16ExportAuditLogsRequest\x125\n" +"\x06parent\x18\x04 \x01(\tB\x1d\xe0A\x02\xfaA\x17\x12\x15bytebase.com/AuditLogR\x06parent\x12\x16\n" +@@ -831,9 +831,9 @@ const file_v1_audit_log_service_proto_rawDesc = "" +"\x05scope\x18\x01 \x01(\tR\x05scope\x12\x1a\n" +"\bresource\x18\x02 \x01(\tR\bresource\x12\x1b\n" +"\tclient_id\x18\x03 \x01(\tR\bclientId\x12%\n" +- "\x0ecorrelation_id\x18\x04 \x01(\tR\rcorrelationId2\xb9\x03\n" +- "\x0fAuditLogService\x12\xcf\x01\n" +- "\x0fSearchAuditLogs\x12#.bytebase.v1.SearchAuditLogsRequest\x1a$.bytebase.v1.SearchAuditLogsResponse\"q\x8a\xea0\x13bb.auditLogs.search\x90\xea0\x01\xa0\xea0\x04\xa8\xea0\t\x82\xd3\xe4\x93\x02H:\x01*Z\x19:\x01*\"\x14/v1/auditLogs:search\"(/v1/{parent=projects/*}/auditLogs:search\x12\xd3\x01\n" ++ "\x0ecorrelation_id\x18\x04 \x01(\tR\rcorrelationId2\xbd\x03\n" ++ "\x0fAuditLogService\x12\xd3\x01\n" ++ "\x0fSearchAuditLogs\x12#.bytebase.v1.SearchAuditLogsRequest\x1a$.bytebase.v1.SearchAuditLogsResponse\"u\x8a\xea0\x13bb.auditLogs.search\x90\xea0\x01\x98\xea0\x01\xa0\xea0\x04\xa8\xea0\t\x82\xd3\xe4\x93\x02H:\x01*Z\x19:\x01*\"\x14/v1/auditLogs:search\"(/v1/{parent=projects/*}/auditLogs:search\x12\xd3\x01\n" +"\x0fExportAuditLogs\x12#.bytebase.v1.ExportAuditLogsRequest\x1a$.bytebase.v1.ExportAuditLogsResponse\"u\x8a\xea0\x13bb.auditLogs.export\x90\xea0\x01\x98\xea0\x01\xa0\xea0\x04\xa8\xea0\t\x82\xd3\xe4\x93\x02H:\x01*Z\x19:\x01*\"\x14/v1/auditLogs:export\"(/v1/{parent=projects/*}/auditLogs:exportB\xaa\x01\n" +"\x0fcom.bytebase.v1B\x14AuditLogServiceProtoP\x01Z4github.com/bytebase/bytebase/backend/generated-go/v1\xa2\x02\x03BXX\xaa\x02\vBytebase.V1\xca\x02\vBytebase\\V1\xe2\x02\x17Bytebase\\V1\\GPBMetadata\xea\x02\fBytebase::V1b\x06proto3"diff --git a/backend/generated-go/v1/subscription_service.pb.go b/backend/generated-go/v1/subscription_service.pb.goindex f436c85ebc..7693d5d965 100644--- a/backend/generated-go/v1/subscription_service.pb.go+++ b/backend/generated-go/v1/subscription_service.pb.go@@ -767,9 +767,9 @@ type VerifyCheckoutSessionRequest struct {state protoimpl.MessageState `protogen:"open.v1"`// The same Stripe Checkout Session ID PurchaseResponse mints, arriving back// the other way, so it carries the same classification. VerifyCheckoutSession- // is EXCLUDED, and the MCP gate records a row for every method it refuses, so- // leaving this unannotated would write the id into a denial row while the- // response that produced it was redacted.+ // is EXCLUDED, and a refusal of it streams its request to the stdout audit+ // log, so leaving this unannotated would write the id into that line while+ // the response that produced it was redacted.SessionId string `protobuf:"bytes,1,opt,name=session_id,json=sessionId,proto3" json:"session_id,omitempty"`unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCachediff --git a/backend/server/grpc_routes.go b/backend/server/grpc_routes.goindex b452b41915..288c462b4a 100644--- a/backend/server/grpc_routes.go+++ b/backend/server/grpc_routes.go@@ -134,13 +134,18 @@ func configureGrpcRouters(// Create validation interceptor.validateInterceptor := validate.NewInterceptor()+ // The first-listed interceptor is outermost. Audit wraps ACL, so a call ACL+ // refuses is still streamed to the stdout audit log. Auth stays outside+ // audit: it populates the identity and workspace every row needs. ACL stays+ // last on both chains, because its admission is what the audit interceptor+ // reads as the call reaching its handler.handlerOpts := connect.WithHandlerOptions(connect.WithRecover(onPanic),connect.WithInterceptors(validateInterceptor,auth.New(stores, secret, licenseService, bus, profile),- apiv1.NewACLInterceptor(stores, secret, iamManager, profile),apiv1.NewAuditInterceptor(stores, secret, profile),+ apiv1.NewACLInterceptor(stores, secret, iamManager, profile),),)@@ -198,18 +203,9 @@ func configureGrpcRouters(// chain: the credential carries identity + grant state, while authorization// is re-resolved live per request.//- // Unlike the public chain, audit sits OUTSIDE ACL (first-listed interceptor- // is outermost, so listing audit before ACL wraps it): an ACL denial must- // still produce an audit row, because a denied MCP call is exactly the- // event an operator investigating an agent needs to see. Methods whose- // annotation opts out of auditing stay unaudited for permitted and denied- // calls alike (needAudit gates both).- //- // The MCP ceiling gate sits between them — inside audit, so a denial is- // recorded, and outside ACL, because the ceiling refuses whatever the- // caller's RBAC would have allowed. It marks its denials so the audit- // interceptor records them even on methods whose annotation asks for no- // audit row at all.+ // The order is the public chain's plus the MCP ceiling gate, which sits+ // inside audit, so its refusals are streamed like ACL's, and outside ACL,+ // because the ceiling refuses whatever the caller's RBAC would have allowed.internalHandlerOpts := connect.WithHandlerOptions(connect.WithRecover(onPanic),connect.WithInterceptors(diff --git a/backend/store/audit_log.go b/backend/store/audit_log.goindex 0afb2f0c32..215a0bd4fd 100644--- a/backend/store/audit_log.go+++ b/backend/store/audit_log.go@@ -271,6 +271,31 @@ func ApplyRetentionFilter(userFilterQ *qb.Query, cutoff *time.Time) *qb.Query {return qb.Q().Space("(?)", q)}+// AuditLogTraversalStart is the bound a paged audit-log traversal pins on its+// first page. It is read from the database because created_at is written+// there: a time taken in this process is not comparable with it.+func (s *Store) AuditLogTraversalStart(ctx context.Context) (time.Time, error) {+ var now time.Time+ if err := s.GetDB().QueryRowContext(ctx, "SELECT now()").Scan(&now); err != nil {+ return time.Time{}, errors.Wrapf(err, "failed to read the database time")+ }+ return now, nil+}++// ApplyCreateTimeUpperBound bounds a search to the rows that existed when the+// traversal started. A search writes its own audit row, so without it an+// offset traversal never reaches the end of the set.+func ApplyCreateTimeUpperBound(userFilterQ *qb.Query, upperBound time.Time) *qb.Query {+ boundQ := qb.Q().Space("created_at <= ?", upperBound)+ if userFilterQ == nil {+ return qb.Q().Space("(?)", boundQ)+ }+ q := qb.Q()+ q.Space("?", userFilterQ)+ q.And("?", boundQ)+ return qb.Q().Space("(?)", q)+}+func GetAuditLogOrders(orderBy string) ([]*OrderByKey, error) {keys, err := parseOrderBy(orderBy)if err != nil {diff --git a/backend/tests/audit_log_paging_test.go b/backend/tests/audit_log_paging_test.gonew file mode 100644index 0000000000..5736d7d75c--- /dev/null+++ b/backend/tests/audit_log_paging_test.go@@ -0,0 +1,69 @@+package tests++import (+ "context"+ "testing"++ "connectrpc.com/connect"+ "github.com/stretchr/testify/require"++ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/generated-go/v1/v1connect"+)++// TestAuditLogSearchPagesTerminate pins that a search's own audit row does not+// shift the traversal it belongs to. Reading the audit log is audited, and the+// search pages by offset, so the row each request writes lands at the top of+// the set the next page counts from: at page size 1 the traversal would return+// the same row forever.+//+//nolint:tparallel // The traversals share one server and run in sequence.+func TestAuditLogSearchPagesTerminate(t *testing.T) {+ t.Parallel()+ ctx := context.Background()+ ctl, ctx := startWorkspace(ctx, t)++ workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{+ Name: "workspaces/-",+ }))+ require.NoError(t, err)++ search := func(t *testing.T, orderBy, token string) *v1pb.SearchAuditLogsResponse {+ t.Helper()+ resp, err := ctl.auditLogServiceClient.SearchAuditLogs(ctx, connect.NewRequest(&v1pb.SearchAuditLogsRequest{+ Parent: workspace.Msg.Name,+ Filter: `method == "` + v1connect.AuditLogServiceSearchAuditLogsProcedure + `"`,+ OrderBy: orderBy,+ PageSize: 1,+ PageToken: token,+ }))+ require.NoError(t, err)+ return resp.Msg+ }++ // Two searches to seed the set these traversals page over.+ search(t, "", "")+ search(t, "", "")++ // Newest-first returns the same row forever without the bound; oldest-first+ // grows the set at the rate the offset advances.+ for _, orderBy := range []string{"", "create_time desc", "create_time asc"} {+ t.Run("order_by="+orderBy, func(t *testing.T) {+ seen := map[string]bool{}+ token := ""+ for page := 0; page < 20; page++ {+ resp := search(t, orderBy, token)+ for _, row := range resp.AuditLogs {+ require.False(t, seen[row.Name], "page %d repeated %s", page, row.Name)+ seen[row.Name] = true+ }+ token = resp.NextPageToken+ if token == "" {+ break+ }+ }+ require.Empty(t, token, "the traversal must end, whatever the searches themselves wrote")+ require.NotEmpty(t, seen, "the traversal must read the rows the earlier searches wrote")+ })+ }+}diff --git a/backend/tests/mcp_audit_provenance_test.go b/backend/tests/mcp_audit_provenance_test.goindex 0ac69e64de..3d9e7ad4ba 100644--- a/backend/tests/mcp_audit_provenance_test.go+++ b/backend/tests/mcp_audit_provenance_test.go@@ -5,7 +5,6 @@ import ("encoding/base64""encoding/json""net/http"- "strings""testing""connectrpc.com/connect"@@ -15,19 +14,20 @@ import (v1pb "github.com/bytebase/bytebase/backend/generated-go/v1")-// TestMCPAuditProvenance is the P1a PR 5b operator-story e2e: a workspace-// member's MCP session — real OAuth grant, real /mcp session — makes one-// permitted call and one call the ACL interceptor denies, and an operator-// investigating the agent sees BOTH through the v1 audit-log API, each row-// carrying the delegation provenance (grant scope and resource verbatim,-// client ID, session correlation ID), the denial with its denied status.-// Before this PR the denied call vanished entirely (ACL returned before the-// audit interceptor on the internal chain) and no row carried MCP provenance.+// TestMCPAuditProvenance is the operator-story e2e: a workspace member's MCP+// session — real OAuth grant, real /mcp session — makes permitted and refused+// calls. The permitted call is stored and read back through the v1 audit-log+// API; the refused ones are never stored and are read from the stdout stream.+// Both carry the delegation provenance (grant scope and resource verbatim,+// client ID, session correlation ID).+//+// Not parallel: it captures slog.Default.func TestMCPAuditProvenance(t *testing.T) {- t.Parallel()a := require.New(t)ctx := context.Background()+ lines := captureAuditStream(t)ctl, ctx := startWorkspace(ctx, t)+ ctl.profile.RuntimeEnableAuditLogStdout.Store(true)// A plain workspace member drives the MCP session, so an IAM-gated admin// action is genuinely denied by the ACL interceptor, not the handler.@@ -130,16 +130,24 @@ func TestMCPAuditProvenance(t *testing.T) {"sheet": map[string]any{"content": base64.StdEncoding.EncodeToString([]byte("SELECT 1;"))},}))- // Denied audited call: creating a user needs bb.users.create, which a- // workspace member does not hold. CreateUser is also FORBIDDEN to MCP- // sessions, so the refusal now comes from the ceiling gate rather than the- // ACL — either way it is a workspace-parented denial the operator must see.+ // Refused audited call: CreateUser is FORBIDDEN to MCP sessions, so the+ // ceiling gate refuses it before ACL, and its line is parented to the+ // caller's workspace.a.Equal(http.StatusForbidden, callAPI("UserService/CreateUser", map[string]any{"user": map[string]any{"email": "[email protected]", "title": "sneaky", "password": memberPassword},}))- // The operator's view: the calls surface through the v1 audit-log read- // API, attributed to the member.+ // The operator's view, attributed to the member.+ memberLine := func(method string) map[string]any {+ var matched []map[string]any+ for _, line := range lines() {+ if line["method"] == method && line["user"] == "users/"+memberEmail {+ matched = append(matched, line)+ }+ }+ a.Len(matched, 1, "the refused %s must be streamed once", method)+ return matched[0]+ }searchMemberRows := func(parent, method string) []*v1pb.AuditLog {resp, err := ctl.auditLogServiceClient.SearchAuditLogs(ctx, connect.NewRequest(&v1pb.SearchAuditLogsRequest{Parent: parent,@@ -166,28 +174,23 @@ func TestMCPAuditProvenance(t *testing.T) {a.Equal(clientID, permitted.McpDelegation.ClientId)a.NotEmpty(permitted.McpDelegation.CorrelationId)- deniedRows := searchMemberRows(workspace, "/bytebase.v1.UserService/CreateUser")- a.Len(deniedRows, 1, "a denied MCP call must still produce an audit row — it is exactly the event an operator investigating an agent needs")- denied := deniedRows[0]- a.NotNil(denied.Status, "the denied row must reflect the denial")- a.Equal(int32(connect.CodePermissionDenied), denied.Status.Code)- a.NotNil(denied.McpDelegation)- a.Equal("mcp:read-only", denied.McpDelegation.Scope)- a.Equal(ctl.rootURL+"/mcp", denied.McpDelegation.Resource)- a.Equal(clientID, denied.McpDelegation.ClientId)- a.Equal(permitted.McpDelegation.CorrelationId, denied.McpDelegation.CorrelationId,+ a.Empty(searchMemberRows(workspace, "/bytebase.v1.UserService/CreateUser"), "a gate refusal is never stored")+ denied := memberLine("/bytebase.v1.UserService/CreateUser")+ a.Equal(workspace, denied["parent"])+ a.Equal(v1pb.AuditLog_WARNING.String(), denied["severity"])+ a.InDelta(float64(connect.CodePermissionDenied), denied["status_code"], 0)+ a.Equal(true, denied["mcp"])+ a.Equal("mcp:read-only", denied["mcp_scope"])+ a.Equal(ctl.rootURL+"/mcp", denied["mcp_resource"])+ a.Equal(clientID, denied["mcp_client_id"])+ a.Equal(permitted.McpDelegation.CorrelationId, denied["mcp_correlation_id"],"one MCP session carries one correlation ID across all of its tool calls")- // A denial on a project-scoped resource keeps its true project parent —- // the denied probe must show up for that project's auditors. (Only- // UNVALIDATED resources — the workspace-mismatch arm — fall back to the- // caller's workspace; an IAM denial's resources passed workspace-scoped- // validation.)- //- // The probe is a WRITE method deliberately: the parent comes from the- // resources the ACL interceptor validated, and the ceiling gate runs before- // ACL. A method the ceiling refuses never reaches ACL, so its row falls- // back to the caller's workspace and would prove nothing about parents.+ // A refusal on a project-scoped resource keeps its true project parent,+ // because ACL publishes the resources it validated before the IAM verdict.+ // The probe is a WRITE method deliberately: the ceiling gate runs before+ // ACL, so a method the ceiling refuses falls back to the caller's workspace+ // and would prove nothing about parents.a.Equal(http.StatusForbidden, callAPI("PlanService/CreatePlan", map[string]any{"parent": otherProjectName,"plan": map[string]any{"title": "not mine to make"},@@ -197,12 +200,12 @@ func TestMCPAuditProvenance(t *testing.T) {Filter: `method == "/bytebase.v1.PlanService/CreatePlan"`,}))a.NoError(err)- a.Len(projectDenied.Msg.AuditLogs, 1, "the project-scoped denial must be audited under the project it targeted")- projectRow := projectDenied.Msg.AuditLogs[0]- a.True(strings.HasPrefix(projectRow.Name, otherProjectName+"/auditLogs/"))- a.Equal("users/"+memberEmail, projectRow.User)- a.Equal(int32(connect.CodePermissionDenied), projectRow.Status.GetCode())- a.Equal(permitted.McpDelegation.CorrelationId, projectRow.McpDelegation.GetCorrelationId())+ a.Empty(projectDenied.Msg.AuditLogs, "an ACL refusal is never stored")+ projectLine := memberLine("/bytebase.v1.PlanService/CreatePlan")+ a.Equal(otherProjectName, projectLine["parent"])+ a.Equal(v1pb.AuditLog_WARNING.String(), projectLine["severity"])+ a.InDelta(float64(connect.CodePermissionDenied), projectLine["status_code"], 0)+ a.Equal(permitted.McpDelegation.CorrelationId, projectLine["mcp_correlation_id"])// Public-chain rows are untouched: the admin's direct v1 CreateUser (the// member's own creation, same audited method as the denial) carries nodiff --git a/backend/tests/mcp_forbidden_class_test.go b/backend/tests/mcp_forbidden_class_test.goindex f273d2fe70..b70344015d 100644--- a/backend/tests/mcp_forbidden_class_test.go+++ b/backend/tests/mcp_forbidden_class_test.go@@ -120,33 +120,11 @@ func TestMCPCannotChangeOwnPasswordAndLogIn(t *testing.T) {a.NoError(err, "the user's real password must still be their real password")a.NotEmpty(stillOld.Msg.Token)- // The operator's view: a denied MCP call is exactly the event worth- // investigating, so both denials must be on the audit page — including- // Login, which the handler never reached.- deniedRows := func(method string) []*v1pb.AuditLog {- var rows []*v1pb.AuditLog- for _, row := range deniedMCPRows(ctx, t, ctl, workspace, method) {- if row.User == "users/"+agentEmail {- rows = append(rows, row)- }- }- return rows- }-- updateRows := deniedRows("/bytebase.v1.UserService/UpdateUser")- a.Len(updateRows, 1, "the denied password change must produce exactly one audit row")- a.Equal(int32(connect.CodePermissionDenied), updateRows[0].Status.GetCode(),- "the row must record the denial, not a success")- a.NotEmpty(updateRows[0].McpDelegation.GetCorrelationId(),- "the denial must be correlatable back to the agent session that made it")-- loginRows := deniedRows("/bytebase.v1.AuthService/Login")- a.Len(loginRows, 2, "both refused Login attempts must be audited")- for _, row := range loginRows {- a.Equal(int32(connect.CodePermissionDenied), row.Status.GetCode())- }- a.Equal(updateRows[0].McpDelegation.GetCorrelationId(), loginRows[0].McpDelegation.GetCorrelationId(),- "one session, one correlation ID across the whole chain")+ // Both refusals are streamed, never stored.+ a.Empty(mcpAuditRows(ctx, t, ctl, workspace, "/bytebase.v1.UserService/UpdateUser"),+ "a gate refusal is never stored")+ a.Empty(mcpAuditRows(ctx, t, ctl, workspace, "/bytebase.v1.AuthService/Login"),+ "a gate refusal is never stored")}// TestWebUserStillChangesPasswordAndLogsIn is the regression half: the gatediff --git a/backend/tests/mcp_forbidden_credential_mints_test.go b/backend/tests/mcp_forbidden_credential_mints_test.goindex 8589856ed6..abf564b287 100644--- a/backend/tests/mcp_forbidden_credential_mints_test.go+++ b/backend/tests/mcp_forbidden_credential_mints_test.go@@ -1,9 +1,14 @@package testsimport (+ "bytes""context"+ "encoding/json"+ "log"+ "log/slog""net/http""strings"+ "sync""testing""connectrpc.com/connect"@@ -15,10 +20,12 @@ import ("github.com/bytebase/bytebase/backend/generated-go/v1/v1connect")-// deniedMCPRows returns the audit rows an MCP session produced for one method.+// mcpAuditRows returns the audit rows an MCP session stored for one method.// Filtering on McpDelegation is what makes a row attributable to the agent// rather than to the same human working in the console.-func deniedMCPRows(ctx context.Context, t *testing.T, ctl *controller, workspace, method string) []*v1pb.AuditLog {+// TestMCPReadOnlyCeilingRefusesAWrite reads a stored row through this, which+// keeps an empty result meaningful.+func mcpAuditRows(ctx context.Context, t *testing.T, ctl *controller, workspace, method string) []*v1pb.AuditLog {t.Helper()resp, err := ctl.auditLogServiceClient.SearchAuditLogs(ctx, connect.NewRequest(&v1pb.SearchAuditLogsRequest{Parent: workspace,@@ -215,9 +222,7 @@ func TestMCPCannotMintCredentialsForOtherPrincipals(t *testing.T) {}))a.Error(err, "the address the MCP session tried to rebind to must own no account")- // All five carry the audit annotation, so all five denials are on the- // operator's audit page — a denied credential mint is precisely the event- // worth investigating.+ // All five are refused before their handlers, so none is stored.for method, label := range map[string]string{"/bytebase.v1.ServiceAccountService/CreateServiceAccount": "CreateServiceAccount","/bytebase.v1.ServiceAccountService/UpdateServiceAccount": "UpdateServiceAccount",@@ -225,12 +230,7 @@ func TestMCPCannotMintCredentialsForOtherPrincipals(t *testing.T) {"/bytebase.v1.UserService/CreateUser": "CreateUser","/bytebase.v1.UserService/UpdateEmail": "UpdateEmail",} {- rows := deniedMCPRows(ctx, t, ctl, workspaceName, method)- a.Len(rows, 1, "the denied %s must produce exactly one audit row", label)- a.Equal(int32(connect.CodePermissionDenied), rows[0].Status.GetCode(),- "the %s row must record the denial, not a success", label)- a.NotEmpty(rows[0].McpDelegation.GetCorrelationId(),- "the %s denial must be correlatable back to the agent session", label)+ a.Empty(mcpAuditRows(ctx, t, ctl, workspaceName, method), "the refused %s is never stored", label)}// The console keeps working. The gate lives on the internal MCP chain, so@@ -420,13 +420,9 @@ func TestMCPCannotWriteTrustAnchorsOrShipStoredSecrets(t *testing.T) {a.NotContains(wi.Email, wiID, "the workload identity must never have been written")}- // All six produce a denial row, and two of them only since 1b-2. Four carry- // the audit annotation; TestIdentityProvider and TestEmailSetting carry- // none, so until the gate started marking its own refusals they were- // refused silently — the two methods in this batch that would have carried- // a stored secret to an address the agent chose were the two that left no- // trace. This assertion is what tells anyone who touches the marking that- // the coverage is load-bearing.+ // All six are refused before their handlers, so none is stored. The stream+ // line's redaction of TestIdentityProvider's request is pinned by+ // TestMCPGateRefusalIsStreamedWithoutAnAuditAnnotation.for method, label := range map[string]string{"/bytebase.v1.IdentityProviderService/CreateIdentityProvider": "CreateIdentityProvider","/bytebase.v1.IdentityProviderService/UpdateIdentityProvider": "UpdateIdentityProvider",@@ -435,20 +431,8 @@ func TestMCPCannotWriteTrustAnchorsOrShipStoredSecrets(t *testing.T) {"/bytebase.v1.IdentityProviderService/TestIdentityProvider": "TestIdentityProvider (no audit annotation)","/bytebase.v1.SettingService/TestEmailSetting": "TestEmailSetting (no audit annotation)",} {- rows := deniedMCPRows(ctx, t, ctl, workspaceName, method)- a.Len(rows, 1, "the denied %s must produce exactly one audit row", label)- a.Equal(int32(connect.CodePermissionDenied), rows[0].Status.GetCode(),- "the %s row must record the denial", label)+ a.Empty(mcpAuditRows(ctx, t, ctl, workspaceName, method), "the refused %s is never stored", label)}-- // What the row must NOT carry: TestIdentityProvider's request names the- // stored client secret's destination and supplies a code, and recording a- // denial verbatim would put both in a log the denial exists to protect.- idpRow := deniedMCPRows(ctx, t, ctl, workspaceName,- "/bytebase.v1.IdentityProviderService/TestIdentityProvider")[0]- a.NotContains(idpRow.Request, "anything", "the supplied authorization code must be masked in the row")- a.Contains(idpRow.Request, "collector.attacker.example.com",- "the host the agent named is the point of the row and stays readable")}// TestMCPCannotRetargetADataSource covers the same "carry a stored secret to a@@ -527,9 +511,7 @@ func TestMCPCannotRetargetADataSource(t *testing.T) {a.Equal(realHost, after.Msg.DataSources[0].Host,"the data source must still point where the operator put it")- // The same retarget WITHOUT validate_only, denied identically. The pair is- // an A/B on one flag: same method, same session, same refusal — and only- // one of them is auditable, which is the point of the assertions below.+ // The same retarget WITHOUT validate_only, denied identically.persisting := callAPIOnSession(ctx, t, session, "InstanceService/UpdateDataSource", map[string]any{"name": instance.Msg.Name,"dataSource": map[string]any{"id": dataSourceID, "host": "attacker.example.com"},@@ -541,27 +523,11 @@ func TestMCPCannotRetargetADataSource(t *testing.T) {Name: "workspaces/-",}))a.NoError(err)- rows := deniedMCPRows(ctx, t, ctl, workspace.Msg.Name, "/bytebase.v1.InstanceService/UpdateDataSource")- t.Logf("audit rows for the two denied retargets (one validate-only): %d", len(rows))-- // Both denials are on the audit page. UpdateDataSource carries audit = true- // and is not carved out of workspace-parent resolution, and createAuditLog- // now skips a validate-only request only when the call SUCCEEDED — a dry- // run Bytebase accepted changed nothing worth recording, a refused one is- // worth exactly as much as any other refusal.- //- // This is the assertion that discriminates the two. Before, the- // validate-only denial returned from createAuditLog's first statement- // before the denial was ever considered, so this pair — same method, same- // session, same refusal, one flag apart — produced ONE row, and the- // validate-only variant is precisely the one that leaves no other trace:- // it dials the caller's host and persists nothing.- a.Len(rows, 2,- "both denials are auditable: setting validate_only must not turn off the record of being refused")- for _, row := range rows {- a.Equal(int32(connect.CodePermissionDenied), row.GetStatus().GetCode(),- "each row must carry the denial, not a blank status")- }+ // Both are refused before the handler, so neither is stored. That the+ // validate_only flag does not drop the refusal's stream line is pinned by+ // TestAuditSinks.+ a.Empty(mcpAuditRows(ctx, t, ctl, workspace.Msg.Name, "/bytebase.v1.InstanceService/UpdateDataSource"),+ "a gate refusal is never stored")// The console keeps working — the gate is on the internal MCP chain only._, err = ctl.instanceServiceClient.UpdateDataSource(ctx, connect.NewRequest(&v1pb.UpdateDataSourceRequest{@@ -705,16 +671,8 @@ func TestMCPCannotRewriteItsOwnCeiling(t *testing.T) {a.Equal("https://api.openai.com", ai.Msg.GetValue().GetAi().GetEndpoint(),"the endpoint the stored API key is sent to must not have moved")- // UpdateSetting is audited and is NOT one of the reset-flow methods carved- // out of workspace-parent resolution, so unlike those its denials do reach- // the operator's audit page. Three denied attempts, three rows.- rows := deniedMCPRows(ctx, t, ctl, workspace.Msg.Name, "/bytebase.v1.SettingService/UpdateSetting")- a.Len(rows, 3, "each denied settings write must be on the audit page")- for _, row := range rows {- a.Equal(int32(connect.CodePermissionDenied), row.Status.GetCode())- a.NotEmpty(row.McpDelegation.GetCorrelationId(),- "the denial must be correlatable back to the agent session")- }+ a.Empty(mcpAuditRows(ctx, t, ctl, workspace.Msg.Name, "/bytebase.v1.SettingService/UpdateSetting"),+ "a gate refusal is never stored")// The console keeps working: the gate is on the internal MCP chain only.// An explicit value rather than an echo of ceilingBefore, which the fixture@@ -729,36 +687,23 @@ func TestMCPCannotRewriteItsOwnCeiling(t *testing.T) {"the console write must actually land — otherwise the denial above proves nothing about the gate")}-// TestMCPResetFlowDenialsAreSilent pins a gap the audit annotation does NOT-// close.+// TestMCPResetFlowRefusalsAreStreamed pins the stream line of a gate refusal+// on the three allow_without_credential reset-flow methods.//-// #21162 gave RequestPasswordReset and ResetPassword `audit = true`, which-// looks like it should give their MCP denials a row — needAudit reads that-// annotation and nothing else. It does not, and the reason is one layer down.+// An unauthenticated caller of these methods could name any workspace, so the+// audit interceptor takes their parent only from what the handler validated+// (handlerValidatedWorkspaceMethod, audit.go). The gate refuses before the+// handler runs, so no handler ever validates one. A request carrying a+// delegated grant is the exception: the internal MCP chain already bound a+// verified workspace, and the line is filed under it.//-// These three RPCs are allow_without_credential, so an unauthenticated caller-// could name any workspace, and auditing against an unvalidated workspace would-// let anyone write rows into someone else's. createAuditLog therefore carves-// them out (handlerValidatedWorkspaceMethod, audit.go): their audit parent-// comes only from what the HANDLER validated via common.SetAuditWorkspaceID,-// and the ordinary workspace fallback is explicitly skipped for them.-//-// The gate refuses before dispatch, so the handler never runs, so nothing ever-// calls SetAuditWorkspaceID. That used to mean no parent and no row, which made-// the silent set seven rather than the four an annotation audit suggests.-//-// 1b-2 closed it, and this test now pins the close. The workspace was never-// actually unknown here: the internal MCP chain put a credential-verified one-// in the context, and the carve-out simply could not use it. It can now, for-// requests carrying a delegated grant and only those — an unauthenticated-// public reset still gets its parent from the handler alone, because there the-// workspace IS caller-named and auditing against it would let anyone write-// rows into someone else'"'"'s.-func TestMCPResetFlowDenialsAreAudited(t *testing.T) {- t.Parallel()+// Not parallel: it captures slog.Default.+func TestMCPResetFlowRefusalsAreStreamed(t *testing.T) {a := require.New(t)ctx := context.Background()+ lines := captureAuditStream(t)ctl, ctx := startWorkspace(ctx, t)+ ctl.profile.RuntimeEnableAuditLogStdout.Store(true)workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{Name: "workspaces/-",@@ -769,14 +714,13 @@ func TestMCPResetFlowDenialsAreAudited(t *testing.T) {session := openMCPSession(ctx, t, ctl, ctl.authInterceptor.token)defer session.Close()- // The positive control goes first and through the same helper: UpdateUser is- // FORBIDDEN, audited, and NOT carved out, so it must produce a row. Without- // it the zeroes below would also be satisfied by a query that finds nothing- // for an unrelated reason.+ // The positive control: UpdateUser is FORBIDDEN and not carved out, so its+ // refusal streams a line whatever the carve-out does.control := callAPIOnSession(ctx, t, session, "UserService/UpdateUser", map[string]any{"user": map[string]any{"name": "users/[email protected]", "password": "2048bytebase"},"updateMask": "password",})+ a.Equal(http.StatusForbidden, control.Status)probes := map[string]mcpCallResult{"RequestPasswordReset": callAPIOnSession(ctx, t, session, "AuthService/RequestPasswordReset", map[string]any{@@ -793,34 +737,83 @@ func TestMCPResetFlowDenialsAreAudited(t *testing.T) {"workspace": workspaceName,}),}- for name, out := range probes {- t.Logf("MCP %s → status=%d error=%q", name, out.Status, out.Error)- }for name, out := range probes {a.Equal(http.StatusForbidden, out.Status, "%s must be refused", name)a.Contains(out.Error, "not available to MCP sessions", "%s must be refused by the gate", name)}- a.Equal(http.StatusForbidden, control.Status)- a.Len(deniedMCPRows(ctx, t, ctl, workspaceName, "/bytebase.v1.UserService/UpdateUser"), 1,- "positive control: a FORBIDDEN, audited, non-carved-out method must produce a denial row")-- for method, label := range map[string]string{- "/bytebase.v1.AuthService/RequestPasswordReset": "RequestPasswordReset",- "/bytebase.v1.AuthService/ResetPassword": "ResetPassword",- "/bytebase.v1.AuthService/SendEmailLoginCode": "SendEmailLoginCode",+ for _, method := range []string{+ "/bytebase.v1.UserService/UpdateUser",+ "/bytebase.v1.AuthService/RequestPasswordReset",+ "/bytebase.v1.AuthService/ResetPassword",+ "/bytebase.v1.AuthService/SendEmailLoginCode",} {- rows := deniedMCPRows(ctx, t, ctl, workspaceName, method)- a.Len(rows, 1,- "%s is refused before its handler runs, so it announces no workspace — the delegated "+- "credential's verified one stands in, and the denial is recorded like every other", label)- a.Equal(int32(connect.CodePermissionDenied), rows[0].Status.GetCode(),- "the %s row must record the denial", label)- a.True(strings.HasPrefix(rows[0].Name, workspaceName+"/auditLogs/"),- "the row is parented to the workspace the MCP credential was bound to, not one the caller named")+ a.Empty(mcpAuditRows(ctx, t, ctl, workspaceName, method), "the refused %s is never stored", method)+ var matched []map[string]any+ for _, line := range lines() {+ if line["method"] == method && lineFlag(line, "mcp") {+ matched = append(matched, line)+ }+ }+ a.Len(matched, 1, "the refused %s is streamed once", method)+ a.Equal(workspaceName, matched[0]["parent"],+ "the line is filed under the workspace the MCP credential was bound to, not one the caller named")+ a.Equal(v1pb.AuditLog_WARNING.String(), matched[0]["severity"])+ a.InDelta(float64(connect.CodePermissionDenied), matched[0]["status_code"], 0)+ }+}++// captureAuditStream sends slog.Default to a buffer until the test ends and+// returns a reader for the audit lines written so far. The servers these tests+// boot run in this process, so their stream lands there too. slog.Default is+// process-wide, so a test that calls this must not be parallel, and must call+// it before starting its server so the server stops before the logger is+// restored.+func captureAuditStream(t *testing.T) func() []map[string]any {+ t.Helper()+ var mu sync.Mutex+ var buf bytes.Buffer+ prev, prevWriter, prevFlags := slog.Default(), log.Writer(), log.Flags()+ // slog.SetDefault also points the log package at the new handler, and+ // restoring the slog default does not undo that.+ t.Cleanup(func() {+ slog.SetDefault(prev)+ log.SetOutput(prevWriter)+ log.SetFlags(prevFlags)+ })+ slog.SetDefault(slog.New(slog.NewJSONHandler(lockedBuffer{mu: &mu, buf: &buf}, nil)))+ return func() []map[string]any {+ mu.Lock()+ defer mu.Unlock()+ var lines []map[string]any+ for _, raw := range bytes.Split(buf.Bytes(), []byte("\n")) {+ var line map[string]any+ if json.Unmarshal(raw, &line) == nil && line["log_type"] == "audit" {+ lines = append(lines, line)+ }+ }+ return lines}}+// lineFlag reads one boolean attribute off a captured audit line, absent+// meaning false.+func lineFlag(line map[string]any, key string) bool {+ v, ok := line[key].(bool)+ return ok && v+}++type lockedBuffer struct {+ mu *sync.Mutex+ buf *bytes.Buffer+}++func (l lockedBuffer) Write(p []byte) (int, error) {+ l.mu.Lock()+ defer l.mu.Unlock()+ return l.buf.Write(p)+}+// TestMCPCredentialMintsLeaveDiscovery pins the other half of the// classification: an agent must not be OFFERED work it can never do, but a call// that arrives anyway — from memory, or from a skill written before thediff --git a/backend/tests/mcp_gate_test.go b/backend/tests/mcp_gate_test.goindex 246d130601..ee11b36999 100644--- a/backend/tests/mcp_gate_test.go+++ b/backend/tests/mcp_gate_test.go@@ -73,10 +73,11 @@ func TestMCPGateServesAndRefusesByClass(t *testing.T) {a.Equal(http.StatusForbidden, forbidden.Status)a.Contains(forbidden.Error, "ends the human's own login session")- // The EXCLUDED denial is audited, and ListUsers carries no audit- // annotation: the record comes from the gate, not from the method.- a.Len(deniedMCPRows(ctx, t, ctl, workspaceName, "/bytebase.v1.UserService/ListUsers"), 1,- "a policy denial is recorded even where the method asks for no audit row")+ // A gate refusal is streamed, never stored; the served CreateSheet is stored.+ a.Empty(mcpAuditRows(ctx, t, ctl, workspaceName, "/bytebase.v1.UserService/ListUsers"),+ "a gate refusal is never stored")+ a.Len(mcpAuditRows(ctx, t, ctl, projectName, "/bytebase.v1.SheetService/CreateSheet"), 1,+ "a served, audited MCP call is stored")}// TestMCPGateRefusesGrantIssues is the one refusal a per-method class cannotdiff --git a/backend/tests/mcp_masking_test.go b/backend/tests/mcp_masking_test.goindex a8a84bc378..69f3f8e574 100644--- a/backend/tests/mcp_masking_test.go+++ b/backend/tests/mcp_masking_test.go@@ -935,12 +935,6 @@ func TestMCPSessionCannotFlipTheMaskingToggle(t *testing.T) {a.NoError(err)a.True(stored, "the refusal must land before the write: the toggle is still on")- // The operator's view. A gate denial runs before ACL, so it carries no- // resource and its row is parented to the caller's workspace.- rows := deniedMCPRows(ctx, t, ctl, workspace.Msg.Name, "/bytebase.v1.SettingService/UpdateSetting")- a.NotEmpty(rows, "a denied settings write must be visible to an operator with MCP provenance")- a.Equal(int32(connect.CodePermissionDenied), rows[0].Status.GetCode(),- "the row must record the denial, not a success")- a.NotEmpty(rows[0].McpDelegation.GetCorrelationId(),- "the denial must be correlatable back to the agent session that made it")+ a.Empty(mcpAuditRows(ctx, t, ctl, workspace.Msg.Name, "/bytebase.v1.SettingService/UpdateSetting"),+ "a gate refusal is never stored")}diff --git a/backend/tests/mcp_sql_clamp_test.go b/backend/tests/mcp_sql_clamp_test.goindex e65932a763..9850f1fda1 100644--- a/backend/tests/mcp_sql_clamp_test.go+++ b/backend/tests/mcp_sql_clamp_test.go@@ -206,7 +206,7 @@ func TestMCPReadOnlyCeilingRefusesAWrite(t *testing.T) {// same human working in the console.// Query is a database-scoped method, so its rows are parented to the// project rather than to the workspace.- rows := deniedMCPRows(f.ctx, t, f.ctl, f.ctl.project.Name, "/bytebase.v1.SQLService/Query")+ rows := mcpAuditRows(f.ctx, t, f.ctl, f.ctl.project.Name, "/bytebase.v1.SQLService/Query")a.NotEmpty(rows, "a clamp denial must be visible to an operator with MCP provenance")var denied *v1pb.AuditLogfor _, row := range rows {@@ -217,6 +217,12 @@ func TestMCPReadOnlyCeilingRefusesAWrite(t *testing.T) {}a.NotNil(denied, "the denied query must have produced a row of its own")a.Contains(denied.Status.Message, "READ_ONLY")+ a.Equal(v1pb.AuditLog_WARNING, denied.Severity, "the clamp marks its refusal")+ for _, row := range rows {+ if row.Status == nil {+ a.Equal(v1pb.AuditLog_INFO, row.Severity, "a served query is not marked")+ }+ }// The control. Same principal, same credential, same statement; only the// ceiling changed, and the INSERT now lands. It runs on a session openeddiff --git a/docs/design/audit-payload-redaction-from-proto.md b/docs/design/audit-payload-redaction-from-proto.mdindex f0c4cb5de1..1e29c9fcf0 100644--- a/docs/design/audit-payload-redaction-from-proto.md+++ b/docs/design/audit-payload-redaction-from-proto.md@@ -162,10 +162,10 @@ The hook point is `marshalAuditPayload` (originally two functions, `getRequestStstreaming rows reach redaction only through those two: `auditConnectStreamingConn.Send`(`audit.go:171-193`) builds its own `auditEntry` and calls `createAuditLog` directly. `AdminExecute`is the only streaming RPC, is audited, and its response carries every row of an admin-mode query, so-moving the walk up into `WrapUnary` — where the `service_data` and `mcpPolicyDenied` plumbing lives-— silently drops streaming redaction. One end-to-end assertion on the `Send` path is required:-streaming persistence is otherwise exercised through the `createAuditLogFunc` stub seam-(`audit.go:49-52`), which bypasses the real path.+moving the walk up into `WrapUnary` — where the `service_data` and permission-refusal plumbing+lives — silently drops streaming redaction. One end-to-end assertion on the `Send` path is+required: streaming persistence is otherwise exercised through a fake `common.AuditLogWriter`,+which bypasses the real insert.A **plan** per message type, built from the descriptor on first use and cached by`protoreflect.FullName`: the fields to drop, and the submessage fields that lead to one. An@@ -368,11 +368,12 @@ walking each of the ten rebuilds for what it newly admits, is a precondition for`mcpDenialRequestsUnderReview` (`mcp_gate_test.go:1322`), which it replaces once the populationmatches.- **That population is wider than the audited RPCs.** `WrapUnary` writes a row on- `needAudit(ctx) || mcpPolicyDenied` (`audit.go:102`), so the gate-refused methods carrying no- audit annotation — `ListInstanceDatabaseRequest` and `SwitchWorkspaceRequest` among them — are in- scope; deriving the inventory from audited methods alone would omit exactly the population- `TestLintDenialRequestsAreReviewedForRedaction` exists to cover. Every registered `Any` type is+ **That population is wider than the audited RPCs.** `WrapUnary` streams every call a permission+ check refused, whatever its annotation ([audit-what-produces-a-row.md](audit-what-produces-a-row.md)),+ so the refused methods carrying no audit annotation — `ListInstanceDatabaseRequest` and+ `SwitchWorkspaceRequest` among them — are in scope; deriving the inventory from audited methods+ alone would omit exactly the population `TestLintDenialRequestsAreReviewedForRedaction` exists to+ cover. Every registered `Any` type isin scope too, from all three paths in the table above, since those reach the row without passingeither entry point. That half comes from the registry rather than from a descriptor walk, whichis why the registry has to be enforced at its call sites for the inventory to mean anything. Thediff --git a/docs/design/audit-what-produces-a-row.md b/docs/design/audit-what-produces-a-row.mdnew file mode 100644index 0000000000..0e5b81266b--- /dev/null+++ b/docs/design/audit-what-produces-a-row.md@@ -0,0 +1,215 @@+# Audit: What Produces a Row++Author: Vincent Huang + AI+Last Update: Sept. 17, 2026+Reviewers: [x] Danny Xu [x] Edward Lu [x] Xzavier Zane+Related Docs: [Draft 3](https://docs.google.com/document/d/1zBam0DT3jtLrDUnSIdiUp2oz3uLdKx53Jb13FGvy_LI/edit), [Audit logs in comparable products](https://claude.ai/code/artifact/579af5ff-ff73-41ad-b446-9c94e9da7b46), [Audit compliance](https://docs.google.com/presentation/d/1CyG-HsnhclncnAO0JlxE4Kit8tr5ZInKHsA8CLvPDC8/edit?usp=sharing)++## Background++- Two interceptor chains serve the same handlers, and the first-listed+ interceptor is the outermost one:++```+public chain: validate -> auth -> ACL -> audit -> handler+internal MCP: validate -> auth -> audit -> gate -> ACL -> handler+```++- On the public chain a permission denial returns before the audit+ interceptor runs and leaves no trace. On the MCP chain the same denial+ writes a row. Nothing decided it that way.+- The audit trigger also fires when the MCP gate refused the call, through a+ runtime flag, so a method with no audit annotation can produce rows and the+ protos no longer say what is stored.+- A permission check inside a handler on an audited method writes a row+ today, with the error as its status, and every failed login writes a row.+- The audit log lives in the metadata database, where audit writes compete+ with the operational workload. Stdout is a workspace setting, TEAM and+ above, self-hosted only. Today it mirrors the database, and Bytebase Cloud+ refuses it.++## Goals++- **Give the compliance reader a record of refused calls.** The standards+ in the related deck require a log of refused access attempts. A call+ refused before it reaches a handler leaves no record today.+- **Keep the console the administrator's record.** It stores exactly what it+ stores today, and nothing that reader does not need.+- **Audit a denied call the same way on both chains,** human or agent.+- **Follow what comparable products do:** a stored record for admins and a+ stream for compliance [7].++## 1. Two readers, two sinks++Two kinds of reader open the audit log, and they want different things.++An administrator opens the console after something went wrong: a row was+dropped, a schema changed, a setting moved. They want to know who did it,+from where, and with what request, and they want it in the product, beside+the object it touched. A refused call is not on that list. It changed+nothing, and an engineer who needs a permission asks for it rather than+being discovered in a log.++A compliance reader wants the opposite: every access decision, refused ones+most of all, kept for a fixed period in a system of record they control and+reviewed with tools built for that. They do not read it in the console. They+read it in a SIEM, and they want it complete more than they want it+convenient.++The database serves the first reader, so it stores what an admitted caller+did. The stream serves the second, so it carries everything, refusals+included, and leaves storage and retention to the reader's own system. One+sink for both would fail one of them: everything in the database puts+compliance volume on the metadata store, and everything in the stream takes+the administrator's record out of the product. The rest of this doc follows+from that split.++We should keep two sinks with two jobs: the database for the administrator's+record, stdout for the compliance record.++```+store = audited method and the call reached its handler+stream = stdout on and (stored or refused by a permission check)+```++| Call | Database | Stdout, when on |+|---|---|---|+| Audited method, reached the handler, any outcome | stored | streamed |+| Any method, refused by the permission check at the interceptor | not stored | streamed |+| Unaudited method, refused by the permission check inside the handler | not stored | streamed |+| Unaudited method, reached the handler, not refused | not stored | not streamed |++- The database rule is today's rule, because today's rule already describes+ what the administrator reads: what an admitted caller did on a method worth+ recording. A permission refusal is streamed wherever it is made, and stored+ only when that rule already stores the call, so the database stays+ identical to today. GCP and Azure keep the same+ always-on tier of administrative changes with reads off by default [1][5];+ AWS logs management reads and writes by default and makes data events+ opt-in [3][4].+- Stdout excludes nothing beyond what the row itself excludes: redaction of+ sensitive fields, and successful validate-only calls.+- What the stream guarantees. Bytebase writes each line to its own standard+ output once, synchronously, before the database insert, so a failed insert+ does not lose the line. It does not buffer, retry or confirm delivery, and+ it cannot see a failed write to stdout; the writer is best-effort today and+ stays so. Collection, durability and retention are the operator's log+ pipeline, and a line the pipeline drops is gone.+- Refused means refused by a permission check. A rejected credential is+ turned away before audit runs, and the one streaming call writes on send,+ so a refusal on receive reaches neither sink. Ownership and workflow rules+ are not permission checks (section 3).+- Stdout is self-hosted, TEAM and above, and Bytebase Cloud refuses it, so+ this change adds no refusal coverage there. Comparable clouds give a+ customer without a stream a record on a paid tier, or a push destination+ [7]. The trigger to build one is a Cloud customer with a compliance need.+- The database grows as it does today, plus one row per audit-log search+ (section 2).++**Implementation.** No setting and no API change. Two shipped strings that say+MCP refusals are recorded in the audit log become "streamed to stdout when it+is on", in all five locale files.++## 2. Annotation++We should keep the annotation as it is: `audit = true` marks the methods whose+calls the database stores. The store's rule does not vary by class or by+outcome, so a richer label would describe nothing the store uses.+SearchAuditLogs gains it, with its response omitted+from the row, so that access to the audit log is itself recorded;+ExportAuditLogs already carries it. The cost is one row per audit-log search,+and a log pipeline reads the stream rather than polling the search API.++**Implementation.** One audit option line on SearchAuditLogs and one omit+option on its response field.++## 3. Denials++- A refused call is one the method's permission check turned away, whether+ the access-control interceptor or the custom-auth handler made the check.+ Any other failure, a validation error or a failed connection test, is an+ ordinary call and follows section 1.+- Rules of other kinds are not permission checks and are not marked: a+ license gate, a workflow state, and ownership rules such as "only the+ assignee may do this".+- A marked refusal is stamped `WARNING`. Everything else stays `INFO`. The+ compliance reader filters on that, without joining on the status code.+- The two doors that refuse outside the connect chains, the `/mcp` connection+ and the OAuth consent page, refuse on the same ceiling verdict the gate+ uses and record only policy refusals, so their rows are `WARNING` too. The+ writer they share stamps it, so a third door cannot answer `INFO` for a+ refusal the gate calls `WARNING`.+- The validate-only rule stays: a validate-only call is skipped only when it+ succeeded.+- A failed login is a stored row today; a refused call is a new line in the+ stream. Both are the compliance reader's events, and neither is the+ administrator's.++**Implementation.** No API change. The mark is set where the access-control+interceptor 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+themselves. Handler sites mark by building the refusal with+`common.PermissionDeniedError`, so the mark cannot be forgotten separately+from the error; the two out-of-band doors mark through their shared writer.++## 4. Interceptor order++- The public chain becomes validate, auth, audit, then access control,+ matching the internal chain minus the gate. Without this the stream reader+ never sees a public-chain refusal, because it returns before the audit+ interceptor runs. Auth stays outside audit, since it populates the identity+ and workspace every row needs.+- The runtime flag is deleted. The write site makes two decisions: store if+ the method is audited and the call reached its handler; stream if stdout is+ on 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.++**Implementation.** No API change.++## Considered++- **Draft 3: a workspace policy with two switches, off by default.** We found+ no dedicated permission-denial audit toggle in the products reviewed, and a+ stored denial was never the admins' record [7].+- **Store every permission refusal.** One row per denied attempt in the+ metadata database with no bound but revocation, for an attempt that changed+ nothing an admin needs to trace.+- **A richer annotation: a class enum, or an explicit line on every method.**+ Two effective states without a policy, and about a hundred lines with no+ change in behavior. A class returns with read auditing, if it is built.+- **No annotation: a Go table keyed by method name, or customer-picked+ methods.** The labeling stays as it is, beside the other per-method+ options. Customer-written policy is Kubernetes' shape, and its own+ documentation warns that operators get it wrong [6].++## Not in this change++- Read auditing. The trigger is a customer asking; the shape is an option on+ the stream.+- A retention setting. Bytebase deletes no audit row; comparable products+ bound retention by default [7]. The lever under every rule.+- A stream for Cloud. Trigger and shape in section 1.+- A rate limit on unauthenticated calls. A login flood writes a row per+ attempt today, and the lockout bounds guesses, not rows.+- Exemptions by identity, GCP's exempted members [2], the damper for a+ retry-looping integration.+- Rejected credentials on API calls, refusals on the one streaming call, and+ a request-size cap.++## Reference++[1] [GCP Cloud Audit Logs: types, defaults, what cannot be disabled](https://docs.cloud.google.com/logging/docs/audit)++[2] [GCP Data Access audit log configuration: auditLogConfigs, logType, exemptedMembers](https://docs.cloud.google.com/logging/docs/audit/configure-data-access)++[3] [AWS CloudTrail: logging management events, read and write selectors](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-management-events-with-cloudtrail.html)++[4] [AWS CloudTrail: event history, the immutable 90-day baseline](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/view-cloudtrail-events.html)++[5] [Azure Monitor: activity log, always collected, 90 days](https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/activity-log)++[6] [Kubernetes: auditing and audit levels](https://kubernetes.io/docs/tasks/debug/debug-cluster/audit/)++[7] [Audit logs in comparable products, the research behind this draft](https://claude.ai/code/artifact/579af5ff-ff73-41ad-b446-9c94e9da7b46)diff --git a/docs/design/mcp-capability-ladder.md b/docs/design/mcp-capability-ladder.mdindex 31c26cc797..4660a4c4db 100644--- a/docs/design/mcp-capability-ladder.md+++ b/docs/design/mcp-capability-ladder.md@@ -177,10 +177,9 @@ names the change when the form is dirty: "Read-only → Read-write applies to evsession's next request." "MCP policy denials are recorded in the audit log" is a fact about thefeature, not about the current state, so it joins the section description, which becomes: "Themost any MCP session may do here. Sessions are also capped by each user's permissions, and policy-refusals are audited." The word "policy" is load-bearing: the audit interceptor writes a row for a-refusal only when the RPC opts into auditing or the gate marked a policy denial, so a permission-denial on an unannotated method is silent, and "every refusal" would promise more than the backend-records.+refusals are audited." Since [audit-what-produces-a-row.md](audit-what-produces-a-row.md), a+refusal is streamed to stdout when audit logging to stdout is enabled, so the shipped sentence+says "streamed to stdout" rather than "recorded in the audit log".**D5 — Edit state: icon cards, the selected mode's "Best for", and the pick's ladder.** Thethree mode cards lose their descriptions; they duplicated the ladder. What remains is the shape ofdiff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 5610811c4d..65824df4e5 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -1629,7 +1629,7 @@"title": "MCP access is turned off"},"line": {- "audit": "Audited actions carry an MCP tag, and anything the policy refuses is recorded",+ "audit": "Audited actions carry an MCP tag, and anything the policy refuses is streamed to stdout when audit logging to stdout is enabled","capped": "Capped by your own Bytebase permissions","masking": "Your masking exemptions do not apply to this session","no-write": "No data or schema changes",@@ -2688,7 +2688,7 @@"title": "Your First Prompt"},"policy": {- "audit": "MCP policy denials are recorded in the audit log.",+ "audit": "MCP policy denials are streamed to stdout when audit logging to stdout is enabled.","description": "The most any MCP session may do in this workspace. Every session is also capped by the connecting user's own permissions.","edit": "Edit policy","in-force": "In force",diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex d666c89b8e..63cbf215a2 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -1629,7 +1629,7 @@"title": "El acceso MCP está desactivado"},"line": {- "audit": "Las acciones auditadas llevan una etiqueta MCP, y lo que la política rechaza también se registra",+ "audit": "Las acciones auditadas llevan una etiqueta MCP, y lo que la política rechaza se envía a la salida estándar cuando el registro de auditoría en la salida estándar está habilitado","capped": "Limitada por sus propios permisos de Bytebase","masking": "Sus exenciones de enmascaramiento no se aplican a esta sesión","no-write": "Ningún cambio de datos ni de esquema",@@ -2688,7 +2688,7 @@"title": "Tu primer prompt"},"policy": {- "audit": "Las denegaciones de la política MCP se registran en el registro de auditoría.",+ "audit": "Las denegaciones de la política MCP se envían a la salida estándar cuando el registro de auditoría en la salida estándar está habilitado.","description": "Lo máximo que puede hacer cualquier sesión MCP en este espacio de trabajo. Cada sesión está además limitada por los permisos del propio usuario que se conecta.","edit": "Editar política","in-force": "En vigor",diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex 058de5323e..dd4c8efbc9 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -1629,7 +1629,7 @@"title": "MCP アクセスはオフです"},"line": {- "audit": "監査対象の操作には MCP タグが付き、ポリシーが拒否したものも記録されます",+ "audit": "監査対象の操作には MCP タグが付き、監査ログの標準出力への出力が有効な場合、ポリシーが拒否したものは標準出力に出力されます","capped": "上限はあなた自身の Bytebase 権限","masking": "あなたのマスキング適用除外はこのセッションには適用されません","no-write": "データやスキーマの変更はできない",@@ -2688,7 +2688,7 @@"title": "最初のプロンプト"},"policy": {- "audit": "MCP ポリシーによる拒否は監査ログに記録されます。",+ "audit": "監査ログの標準出力への出力が有効な場合、MCP ポリシーによる拒否は標準出力に出力されます。","description": "このワークスペースで MCP セッションが実行できる操作の上限です。各セッションは接続ユーザー自身の権限によってもさらに制限されます。","edit": "ポリシーを編集","in-force": "適用中",diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.jsonindex d091257dc2..c146488fb5 100644--- a/frontend/src/locales/vi-VN.json+++ b/frontend/src/locales/vi-VN.json@@ -1629,7 +1629,7 @@"title": "Truy cập MCP đang tắt"},"line": {- "audit": "Hành động được kiểm toán mang nhãn MCP, và những gì chính sách từ chối cũng được ghi lại",+ "audit": "Hành động được kiểm toán mang nhãn MCP, và những gì chính sách từ chối được ghi vào đầu ra chuẩn khi bật ghi nhật ký kiểm toán vào đầu ra chuẩn","capped": "Giới hạn bởi chính quyền Bytebase của bạn","masking": "Các miễn trừ che dữ liệu của bạn không áp dụng cho phiên này","no-write": "Không thay đổi dữ liệu hay lược đồ",@@ -2688,7 +2688,7 @@"title": "Prompt đầu tiên của bạn"},"policy": {- "audit": "Các lần từ chối theo chính sách MCP được ghi vào nhật ký kiểm toán.",+ "audit": "Các lần từ chối theo chính sách MCP được ghi vào đầu ra chuẩn khi bật ghi nhật ký kiểm toán vào đầu ra chuẩn.","description": "Mức tối đa mà một phiên MCP được phép làm trong không gian làm việc này. Mỗi phiên còn bị giới hạn thêm bởi quyền của chính người dùng đang kết nối.","edit": "Sửa chính sách","in-force": "Đang áp dụng",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex ae68841673..673c99805d 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -1629,7 +1629,7 @@"title": "MCP 访问已关闭"},"line": {- "audit": "被审计的操作会带上 MCP 标记,策略拒绝的操作也会被记录",+ "audit": "被审计的操作会带上 MCP 标记,启用审计日志输出到标准输出时,策略拒绝的操作会输出到标准输出","capped": "以你本人的 Bytebase 权限为上限","masking": "你的脱敏豁免对此会话不生效","no-write": "不能修改任何数据或模式",@@ -2688,7 +2688,7 @@"title": "您的第一个提示"},"policy": {- "audit": "MCP 策略拒绝会记录在审计日志中。",+ "audit": "启用审计日志输出到标准输出时,MCP 策略拒绝会输出到标准输出。","description": "本工作空间中任何 MCP 会话所能执行操作的上限。每个会话还会受到连接用户自身权限的限制。","edit": "编辑策略","in-force": "当前生效",diff --git a/frontend/src/types/proto-es/v1/audit_log_service_pb.js b/frontend/src/types/proto-es/v1/audit_log_service_pb.jsindex f2c14a80df..d96b840a45 100644--- a/frontend/src/types/proto-es/v1/audit_log_service_pb.js+++ b/frontend/src/types/proto-es/v1/audit_log_service_pb.js@@ -16,7 +16,7 @@ import { file_v1_iam_policy } from "./iam_policy_pb";* Describes the file v1/audit_log_service.proto.*/export const file_v1_audit_log_service = /*@__PURE__*/- fileDesc("Chp2MS9hdWRpdF9sb2dfc2VydmljZS5wcm90bxILYnl0ZWJhc2UudjEikAEKFlNlYXJjaEF1ZGl0TG9nc1JlcXVlc3QSLQoGcGFyZW50GAUgASgJQh3gQQL6QRcSFWJ5dGViYXNlLmNvbS9BdWRpdExvZxIOCgZmaWx0ZXIYASABKAkSEAoIb3JkZXJfYnkYAiABKAkSEQoJcGFnZV9zaXplGAMgASgFEhIKCnBhZ2VfdG9rZW4YBCABKAkiXQoXU2VhcmNoQXVkaXRMb2dzUmVzcG9uc2USKQoKYXVkaXRfbG9ncxgBIAMoCzIVLmJ5dGViYXNlLnYxLkF1ZGl0TG9nEhcKD25leHRfcGFnZV90b2tlbhgCIAEoCSK7AQoWRXhwb3J0QXVkaXRMb2dzUmVxdWVzdBItCgZwYXJlbnQYBCABKAlCHeBBAvpBFxIVYnl0ZWJhc2UuY29tL0F1ZGl0TG9nEg4KBmZpbHRlchgBIAEoCRIQCghvcmRlcl9ieRgCIAEoCRIpCgZmb3JtYXQYAyABKA4yGS5ieXRlYmFzZS52MS5FeHBvcnRGb3JtYXQSEQoJcGFnZV9zaXplGAUgASgFEhIKCnBhZ2VfdG9rZW4YBiABKAkiSQoXRXhwb3J0QXVkaXRMb2dzUmVzcG9uc2USFQoHY29udGVudBgBIAEoDEIE0OowAhIXCg9uZXh0X3BhZ2VfdG9rZW4YAiABKAki0wQKCEF1ZGl0TG9nEhEKBG5hbWUYASABKAlCA+BBAxI0CgtjcmVhdGVfdGltZRgCIAEoCzIaLmdvb2dsZS5wcm90b2J1Zi5UaW1lc3RhbXBCA+BBAxIMCgR1c2VyGAMgASgJEg4KBm1ldGhvZBgEIAEoCRIwCghzZXZlcml0eRgFIAEoDjIeLmJ5dGViYXNlLnYxLkF1ZGl0TG9nLlNldmVyaXR5EhAKCHJlc291cmNlGAYgASgJEg8KB3JlcXVlc3QYByABKAkSEAoIcmVzcG9uc2UYCCABKAkSIgoGc3RhdHVzGAkgASgLMhIuZ29vZ2xlLnJwYy5TdGF0dXMSKgoHbGF0ZW5jeRgKIAEoCzIZLmdvb2dsZS5wcm90b2J1Zi5EdXJhdGlvbhIwCgxzZXJ2aWNlX2RhdGEYCyABKAsyFC5nb29nbGUucHJvdG9idWYuQW55QgTQ6jACEjYKEHJlcXVlc3RfbWV0YWRhdGEYDCABKAsyHC5ieXRlYmFzZS52MS5SZXF1ZXN0TWV0YWRhdGESNwoObWNwX2RlbGVnYXRpb24YDSABKAsyGi5ieXRlYmFzZS52MS5NQ1BEZWxlZ2F0aW9uQgPgQQMihQEKCFNldmVyaXR5EhgKFFNFVkVSSVRZX1VOU1BFQ0lGSUVEEAASCQoFREVCVUcQARIICgRJTkZPEAISCgoGTk9USUNFEAMSCwoHV0FSTklORxAEEgkKBUVSUk9SEAUSDAoIQ1JJVElDQUwQBhIJCgVBTEVSVBAHEg0KCUVNRVJHRU5DWRAIIjsKCUF1ZGl0RGF0YRIuCgxwb2xpY3lfZGVsdGEYASABKAsyGC5ieXRlYmFzZS52MS5Qb2xpY3lEZWx0YSJICg9SZXF1ZXN0TWV0YWRhdGESEQoJY2FsbGVyX2lwGAEgASgJEiIKGmNhbGxlcl9zdXBwbGllZF91c2VyX2FnZW50GAIgASgJIlsKDU1DUERlbGVnYXRpb24SDQoFc2NvcGUYASABKAkSEAoIcmVzb3VyY2UYAiABKAkSEQoJY2xpZW50X2lkGAMgASgJEhYKDmNvcnJlbGF0aW9uX2lkGAQgASgJMrkDCg9BdWRpdExvZ1NlcnZpY2USzwEKD1NlYXJjaEF1ZGl0TG9ncxIjLmJ5dGViYXNlLnYxLlNlYXJjaEF1ZGl0TG9nc1JlcXVlc3QaJC5ieXRlYmFzZS52MS5TZWFyY2hBdWRpdExvZ3NSZXNwb25zZSJxiuowE2JiLmF1ZGl0TG9ncy5zZWFyY2iQ6jABoOowBKjqMAmC0+STAkg6ASpaGToBKiIUL3YxL2F1ZGl0TG9nczpzZWFyY2giKC92MS97cGFyZW50PXByb2plY3RzLyp9L2F1ZGl0TG9nczpzZWFyY2gS0wEKD0V4cG9ydEF1ZGl0TG9ncxIjLmJ5dGViYXNlLnYxLkV4cG9ydEF1ZGl0TG9nc1JlcXVlc3QaJC5ieXRlYmFzZS52MS5FeHBvcnRBdWRpdExvZ3NSZXNwb25zZSJ1iuowE2JiLmF1ZGl0TG9ncy5leHBvcnSQ6jABmOowAaDqMASo6jAJgtPkkwJIOgEqWhk6ASoiFC92MS9hdWRpdExvZ3M6ZXhwb3J0IigvdjEve3BhcmVudD1wcm9qZWN0cy8qfS9hdWRpdExvZ3M6ZXhwb3J0QqoBCg9jb20uYnl0ZWJhc2UudjFCFEF1ZGl0TG9nU2VydmljZVByb3RvUAFaNGdpdGh1Yi5jb20vYnl0ZWJhc2UvYnl0ZWJhc2UvYmFja2VuZC9nZW5lcmF0ZWQtZ28vdjGiAgNCWFiqAgtCeXRlYmFzZS5WMcoCC0J5dGViYXNlXFYx4gIXQnl0ZWJhc2VcVjFcR1BCTWV0YWRhdGHqAgxCeXRlYmFzZTo6VjFiBnByb3RvMw", [file_google_api_annotations, file_google_api_field_behavior, file_google_api_resource, file_google_protobuf_any, file_google_protobuf_duration, file_google_protobuf_timestamp, file_google_rpc_status, file_v1_annotation, file_v1_common, file_v1_iam_policy]);+ fileDesc("Chp2MS9hdWRpdF9sb2dfc2VydmljZS5wcm90bxILYnl0ZWJhc2UudjEikAEKFlNlYXJjaEF1ZGl0TG9nc1JlcXVlc3QSLQoGcGFyZW50GAUgASgJQh3gQQL6QRcSFWJ5dGViYXNlLmNvbS9BdWRpdExvZxIOCgZmaWx0ZXIYASABKAkSEAoIb3JkZXJfYnkYAiABKAkSEQoJcGFnZV9zaXplGAMgASgFEhIKCnBhZ2VfdG9rZW4YBCABKAkiYwoXU2VhcmNoQXVkaXRMb2dzUmVzcG9uc2USLwoKYXVkaXRfbG9ncxgBIAMoCzIVLmJ5dGViYXNlLnYxLkF1ZGl0TG9nQgTQ6jACEhcKD25leHRfcGFnZV90b2tlbhgCIAEoCSK7AQoWRXhwb3J0QXVkaXRMb2dzUmVxdWVzdBItCgZwYXJlbnQYBCABKAlCHeBBAvpBFxIVYnl0ZWJhc2UuY29tL0F1ZGl0TG9nEg4KBmZpbHRlchgBIAEoCRIQCghvcmRlcl9ieRgCIAEoCRIpCgZmb3JtYXQYAyABKA4yGS5ieXRlYmFzZS52MS5FeHBvcnRGb3JtYXQSEQoJcGFnZV9zaXplGAUgASgFEhIKCnBhZ2VfdG9rZW4YBiABKAkiSQoXRXhwb3J0QXVkaXRMb2dzUmVzcG9uc2USFQoHY29udGVudBgBIAEoDEIE0OowAhIXCg9uZXh0X3BhZ2VfdG9rZW4YAiABKAki0wQKCEF1ZGl0TG9nEhEKBG5hbWUYASABKAlCA+BBAxI0CgtjcmVhdGVfdGltZRgCIAEoCzIaLmdvb2dsZS5wcm90b2J1Zi5UaW1lc3RhbXBCA+BBAxIMCgR1c2VyGAMgASgJEg4KBm1ldGhvZBgEIAEoCRIwCghzZXZlcml0eRgFIAEoDjIeLmJ5dGViYXNlLnYxLkF1ZGl0TG9nLlNldmVyaXR5EhAKCHJlc291cmNlGAYgASgJEg8KB3JlcXVlc3QYByABKAkSEAoIcmVzcG9uc2UYCCABKAkSIgoGc3RhdHVzGAkgASgLMhIuZ29vZ2xlLnJwYy5TdGF0dXMSKgoHbGF0ZW5jeRgKIAEoCzIZLmdvb2dsZS5wcm90b2J1Zi5EdXJhdGlvbhIwCgxzZXJ2aWNlX2RhdGEYCyABKAsyFC5nb29nbGUucHJvdG9idWYuQW55QgTQ6jACEjYKEHJlcXVlc3RfbWV0YWRhdGEYDCABKAsyHC5ieXRlYmFzZS52MS5SZXF1ZXN0TWV0YWRhdGESNwoObWNwX2RlbGVnYXRpb24YDSABKAsyGi5ieXRlYmFzZS52MS5NQ1BEZWxlZ2F0aW9uQgPgQQMihQEKCFNldmVyaXR5EhgKFFNFVkVSSVRZX1VOU1BFQ0lGSUVEEAASCQoFREVCVUcQARIICgRJTkZPEAISCgoGTk9USUNFEAMSCwoHV0FSTklORxAEEgkKBUVSUk9SEAUSDAoIQ1JJVElDQUwQBhIJCgVBTEVSVBAHEg0KCUVNRVJHRU5DWRAIIjsKCUF1ZGl0RGF0YRIuCgxwb2xpY3lfZGVsdGEYASABKAsyGC5ieXRlYmFzZS52MS5Qb2xpY3lEZWx0YSJICg9SZXF1ZXN0TWV0YWRhdGESEQoJY2FsbGVyX2lwGAEgASgJEiIKGmNhbGxlcl9zdXBwbGllZF91c2VyX2FnZW50GAIgASgJIlsKDU1DUERlbGVnYXRpb24SDQoFc2NvcGUYASABKAkSEAoIcmVzb3VyY2UYAiABKAkSEQoJY2xpZW50X2lkGAMgASgJEhYKDmNvcnJlbGF0aW9uX2lkGAQgASgJMr0DCg9BdWRpdExvZ1NlcnZpY2US0wEKD1NlYXJjaEF1ZGl0TG9ncxIjLmJ5dGViYXNlLnYxLlNlYXJjaEF1ZGl0TG9nc1JlcXVlc3QaJC5ieXRlYmFzZS52MS5TZWFyY2hBdWRpdExvZ3NSZXNwb25zZSJ1iuowE2JiLmF1ZGl0TG9ncy5zZWFyY2iQ6jABmOowAaDqMASo6jAJgtPkkwJIOgEqWhk6ASoiFC92MS9hdWRpdExvZ3M6c2VhcmNoIigvdjEve3BhcmVudD1wcm9qZWN0cy8qfS9hdWRpdExvZ3M6c2VhcmNoEtMBCg9FeHBvcnRBdWRpdExvZ3MSIy5ieXRlYmFzZS52MS5FeHBvcnRBdWRpdExvZ3NSZXF1ZXN0GiQuYnl0ZWJhc2UudjEuRXhwb3J0QXVkaXRMb2dzUmVzcG9uc2UidYrqMBNiYi5hdWRpdExvZ3MuZXhwb3J0kOowAZjqMAGg6jAEqOowCYLT5JMCSDoBKloZOgEqIhQvdjEvYXVkaXRMb2dzOmV4cG9ydCIoL3YxL3twYXJlbnQ9cHJvamVjdHMvKn0vYXVkaXRMb2dzOmV4cG9ydEKqAQoPY29tLmJ5dGViYXNlLnYxQhRBdWRpdExvZ1NlcnZpY2VQcm90b1ABWjRnaXRodWIuY29tL2J5dGViYXNlL2J5dGViYXNlL2JhY2tlbmQvZ2VuZXJhdGVkLWdvL3YxogIDQlhYqgILQnl0ZWJhc2UuVjHKAgtCeXRlYmFzZVxWMeICF0J5dGViYXNlXFYxXEdQQk1ldGFkYXRh6gIMQnl0ZWJhc2U6OlYxYgZwcm90bzM", [file_google_api_annotations, file_google_api_field_behavior, file_google_api_resource, file_google_protobuf_any, file_google_protobuf_duration, file_google_protobuf_timestamp, file_google_rpc_status, file_v1_annotation, file_v1_common, file_v1_iam_policy]);/*** Describes the message bytebase.v1.SearchAuditLogsRequest.diff --git a/frontend/src/types/proto-es/v1/subscription_service_pb.d.ts b/frontend/src/types/proto-es/v1/subscription_service_pb.d.tsindex a146bbba8f..baecc454c8 100644--- a/frontend/src/types/proto-es/v1/subscription_service_pb.d.ts+++ b/frontend/src/types/proto-es/v1/subscription_service_pb.d.ts@@ -126,9 +126,9 @@ export declare type VerifyCheckoutSessionRequest = Message<"bytebase.v1.VerifyCh/*** The same Stripe Checkout Session ID PurchaseResponse mints, arriving back* the other way, so it carries the same classification. VerifyCheckoutSession- * is EXCLUDED, and the MCP gate records a row for every method it refuses, so- * leaving this unannotated would write the id into a denial row while the- * response that produced it was redacted.+ * is EXCLUDED, and a refusal of it streams its request to the stdout audit+ * log, so leaving this unannotated would write the id into that line while+ * the response that produced it was redacted.** @generated from field: string session_id = 1;*/diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yamlindex a87e0d23e0..4e2af63807 100644--- a/proto/gen/grpc-doc/openapi.yaml+++ b/proto/gen/grpc-doc/openapi.yaml@@ -14749,9 +14749,9 @@ paths:description: |-The same Stripe Checkout Session ID PurchaseResponse mints, arriving backthe other way, so it carries the same classification. VerifyCheckoutSession- is EXCLUDED, and the MCP gate records a row for every method it refuses, so- leaving this unannotated would write the id into a denial row while the- response that produced it was redacted.+ is EXCLUDED, and a refusal of it streams its request to the stdout audit+ log, so leaving this unannotated would write the id into that line while+ the response that produced it was redacted.required: trueschema:type: string@@ -31293,9 +31293,9 @@ components:description: |-The same Stripe Checkout Session ID PurchaseResponse mints, arriving backthe other way, so it carries the same classification. VerifyCheckoutSession- is EXCLUDED, and the MCP gate records a row for every method it refuses, so- leaving this unannotated would write the id into a denial row while the- response that produced it was redacted.+ is EXCLUDED, and a refusal of it streams its request to the stdout audit+ log, so leaving this unannotated would write the id into that line while+ the response that produced it was redacted.title: VerifyCheckoutSessionRequestadditionalProperties: falsebytebase.v1.VerifyCheckoutSessionResponse:diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex b71b06f927..33098d531f 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -419,6 +419,7 @@ PageToken is used internally for obfuscating pagination tokens.| ----- | ---- | ----- | ----------- || limit | [int32](#int32) | | Maximum number of items to return. || offset | [int32](#int32) | | Number of items to skip before starting to return results. |+| create_time_upper_bound | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | Bounds later pages to the rows that existed when the traversal started, inclusive. Set by a list whose own reads write rows it would page over. |diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.htmlindex 27b147ff9d..94f4bab16f 100644--- a/proto/gen/grpc-doc/store/index.html+++ b/proto/gen/grpc-doc/store/index.html@@ -1695,6 +1695,14 @@ The server computes expire_time from this value at activation time. </p></td><td><p>Number of items to skip before starting to return results. </p></td></tr>+ <tr>+ <td>create_time_upper_bound</td>+ <td><a href="#google.protobuf.Timestamp">google.protobuf.Timestamp</a></td>+ <td></td>+ <td><p>Bounds later pages to the rows that existed when the traversal started,+inclusive. Set by a list whose own reads write rows it would page over. </p></td>+ </tr>+</tbody></table>diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.mdindex a4f0525e55..f07a4d4430 100644--- a/proto/gen/grpc-doc/v1/README.md+++ b/proto/gen/grpc-doc/v1/README.md@@ -12500,7 +12500,7 @@ PlanLimitConfig represents a single plan's configuration| Field | Type | Label | Description || ----- | ---- | ----- | ----------- |-| session_id | [string](#string) | | The same Stripe Checkout Session ID PurchaseResponse mints, arriving back the other way, so it carries the same classification. VerifyCheckoutSession is EXCLUDED, and the MCP gate records a row for every method it refuses, so leaving this unannotated would write the id into a denial row while the response that produced it was redacted. |+| session_id | [string](#string) | | The same Stripe Checkout Session ID PurchaseResponse mints, arriving back the other way, so it carries the same classification. VerifyCheckoutSession is EXCLUDED, and a refusal of it streams its request to the stdout audit log, so leaving this unannotated would write the id into that line while the response that produced it was redacted. |diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.htmlindex a2733bff16..a7cb0aa114 100644--- a/proto/gen/grpc-doc/v1/index.html+++ b/proto/gen/grpc-doc/v1/index.html@@ -35366,9 +35366,9 @@ If empty, the update was applied directly using the existing payment method. </p<td></td><td><p>The same Stripe Checkout Session ID PurchaseResponse mints, arriving backthe other way, so it carries the same classification. VerifyCheckoutSession-is EXCLUDED, and the MCP gate records a row for every method it refuses, so-leaving this unannotated would write the id into a denial row while the-response that produced it was redacted. </p></td>+is EXCLUDED, and a refusal of it streams its request to the stdout audit+log, so leaving this unannotated would write the id into that line while+the response that produced it was redacted. </p></td></tr></tbody>diff --git a/proto/store/store/common.proto b/proto/store/store/common.protoindex ed7ac58fc0..7bbd07e9d2 100644--- a/proto/store/store/common.proto+++ b/proto/store/store/common.proto@@ -2,6 +2,8 @@ syntax = "proto3";package bytebase.store;+import "google/protobuf/timestamp.proto";+option go_package = "generated-go/store";// PageToken is used internally for obfuscating pagination tokens.@@ -10,6 +12,9 @@ message PageToken {int32 limit = 1;// Number of items to skip before starting to return results.int32 offset = 2;+ // Bounds later pages to the rows that existed when the traversal started,+ // inclusive. Set by a list whose own reads write rows it would page over.+ google.protobuf.Timestamp create_time_upper_bound = 3;}// Engine represents the type of database system.diff --git a/proto/v1/v1/audit_log_service.proto b/proto/v1/v1/audit_log_service.protoindex 7bf1926171..c8da2006c8 100644--- a/proto/v1/v1/audit_log_service.proto+++ b/proto/v1/v1/audit_log_service.proto@@ -30,6 +30,7 @@ service AuditLogService {};option (bytebase.v1.permission) = "bb.auditLogs.search";option (bytebase.v1.auth_method) = IAM;+ option (bytebase.v1.audit) = true;option (bytebase.v1.mcp_method_class) = EXCLUDED;option (bytebase.v1.mcp_denial_reason) = ADMINISTERS_THE_WORKSPACE;}@@ -101,7 +102,7 @@ message SearchAuditLogsRequest {// Response message for searching audit logs.message SearchAuditLogsResponse {- repeated AuditLog audit_logs = 1;+ repeated AuditLog audit_logs = 1 [(bytebase.v1.audit_behavior) = OMIT];// A token to retrieve next page of log entities.// Pass this value in the page_token field in the subsequent calldiff --git a/proto/v1/v1/subscription_service.proto b/proto/v1/v1/subscription_service.protoindex a0d4adbb6e..eb34a0f24c 100644--- a/proto/v1/v1/subscription_service.proto+++ b/proto/v1/v1/subscription_service.proto@@ -156,9 +156,9 @@ message PurchaseResponse {message VerifyCheckoutSessionRequest {// The same Stripe Checkout Session ID PurchaseResponse mints, arriving back// the other way, so it carries the same classification. VerifyCheckoutSession- // is EXCLUDED, and the MCP gate records a row for every method it refuses, so- // leaving this unannotated would write the id into a denial row while the- // response that produced it was redacted.+ // is EXCLUDED, and a refusal of it streams its request to the stdout audit+ // log, so leaving this unannotated would write the id into that line while+ // the response that produced it was redacted.string session_id = 1 [(bytebase.v1.audit_behavior) = SENSITIVE];}