Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: redact credentials from audit log payloads
Danny53f5d97eef01
diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.goindex 4da9435525..647ecf176c 100644--- a/backend/api/v1/audit.go+++ b/backend/api/v1/audit.go@@ -483,6 +483,18 @@ func getRequestString(request any) (string, error) {case *v1pb.RemoveDataSourceRequest:r.DataSource = redactDataSource(r.DataSource)return r+ case *v1pb.UpdateSettingRequest:+ r = proto.CloneOf(r)+ r.Setting = redactSetting(r.Setting)+ return r+ case *v1pb.CreateIdentityProviderRequest:+ r = proto.CloneOf(r)+ r.IdentityProvider = redactIdentityProvider(r.IdentityProvider)+ return r+ case *v1pb.UpdateIdentityProviderRequest:+ r = proto.CloneOf(r)+ r.IdentityProvider = redactIdentityProvider(r.IdentityProvider)+ return rdefault:if p, ok := r.(protoreflect.ProtoMessage); ok {return p@@ -519,6 +531,12 @@ func getResponseString(response any) (string, error) {return redactExchangeTokenResponse(r)case *v1pb.RotateDirectorySyncTokenResponse:return redactRotateDirectorySyncTokenResponse(r)+ case *v1pb.ServiceAccount:+ return redactServiceAccount(r)+ case *v1pb.Setting:+ return redactSetting(r)+ case *v1pb.IdentityProvider:+ return redactIdentityProvider(r)case *v1pb.User:return redactUser(r)case *v1pb.Instance:@@ -641,6 +659,95 @@ func redactRotateDirectorySyncTokenResponse(r *v1pb.RotateDirectorySyncTokenRespreturn &v1pb.RotateDirectorySyncTokenResponse{}}+// redactServiceAccount drops the API key. Create and key rotation are the only+// responses that carry it — the read path never populates it — and it is a live+// credential, so logging it would hand a working key to anyone who can read the+// audit log or its stdout stream.+func redactServiceAccount(r *v1pb.ServiceAccount) *v1pb.ServiceAccount {+ if r == nil {+ return nil+ }+ r = proto.CloneOf(r)+ if r.ServiceKey != "" {+ r.ServiceKey = maskedString+ }+ return r+}++// redactIdentityProvider masks the IdP credentials. The read path already blanks+// these (convertToIdentityProvider), so they only reach the audit log through+// the create/update request payload.+func redactIdentityProvider(r *v1pb.IdentityProvider) *v1pb.IdentityProvider {+ if r == nil {+ return nil+ }+ r = proto.CloneOf(r)+ switch config := r.GetConfig().GetConfig().(type) {+ case *v1pb.IdentityProviderConfig_Oauth2Config:+ if config.Oauth2Config.GetClientSecret() != "" {+ config.Oauth2Config.ClientSecret = maskedString+ }+ case *v1pb.IdentityProviderConfig_OidcConfig:+ if config.OidcConfig.GetClientSecret() != "" {+ config.OidcConfig.ClientSecret = maskedString+ }+ case *v1pb.IdentityProviderConfig_LdapConfig:+ if config.LdapConfig.GetBindPassword() != "" {+ config.LdapConfig.BindPassword = maskedString+ }+ default:+ }+ return r+}++// redactSetting masks every credential a settings payload can carry. The read+// path blanks these, so they reach the audit log only through UpdateSetting's+// request. Each secret is masked rather than dropped so the log still records+// that the field was being written.+func redactSetting(r *v1pb.Setting) *v1pb.Setting {+ if r == nil {+ return nil+ }+ r = proto.CloneOf(r)+ switch value := r.GetValue().GetValue().(type) {+ case *v1pb.SettingValue_Email:+ if smtp := value.Email.GetSmtp(); smtp.GetPassword() != "" {+ smtp.Password = maskedString+ }+ case *v1pb.SettingValue_Ai:+ if value.Ai.GetApiKey() != "" {+ value.Ai.ApiKey = maskedString+ }+ case *v1pb.SettingValue_AppIm:+ maskAppIMSecrets(value.AppIm)+ default:+ }+ return r+}++func maskAppIMSecrets(s *v1pb.AppIMSetting) {+ for _, setting := range s.GetSettings() {+ if v := setting.GetSlack(); v.GetToken() != "" {+ v.Token = maskedString+ }+ if v := setting.GetFeishu(); v.GetAppSecret() != "" {+ v.AppSecret = maskedString+ }+ if v := setting.GetWecom(); v.GetSecret() != "" {+ v.Secret = maskedString+ }+ if v := setting.GetLark(); v.GetAppSecret() != "" {+ v.AppSecret = maskedString+ }+ if v := setting.GetDingtalk(); v.GetClientSecret() != "" {+ v.ClientSecret = maskedString+ }+ if v := setting.GetTeams(); v.GetClientSecret() != "" {+ v.ClientSecret = maskedString+ }+ }+}+func redactCreateUserRequest(r *v1pb.CreateUserRequest) *v1pb.CreateUserRequest {if r == nil {return nildiff --git a/backend/api/v1/audit_redaction_test.go b/backend/api/v1/audit_redaction_test.gonew file mode 100644index 0000000000..6a8c842719--- /dev/null+++ b/backend/api/v1/audit_redaction_test.go@@ -0,0 +1,126 @@+package v1++import (+ "testing"++ "github.com/stretchr/testify/require"++ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+)++// Audited RPCs write their request and response payloads to audit_log, and to+// stdout when RuntimeEnableAuditLogStdout is set. Anything with+// bb.auditLogs.search/export, or read access to the log pipeline, can read them.+// So no live credential may survive into either payload.+//+// These are behavioral: each case populates a secret and asserts the marshaled+// audit string does not contain it. A redactor that stops covering a field fails+// here rather than silently starting to log it.++const secretSentinel = "s3cr3t-sentinel-value"++func TestAuditResponseRedactsCredentials(t *testing.T) {+ for _, tt := range []struct {+ name string+ response any+ }{+ {+ // Only create and key rotation populate this; the read path never+ // does. It is a live API key.+ name: "service account key",+ response: &v1pb.ServiceAccount{Name: "serviceAccounts/[email protected]", ServiceKey: secretSentinel},+ },+ {+ name: "smtp password in a returned setting",+ response: &v1pb.Setting{Value: &v1pb.SettingValue{Value: &v1pb.SettingValue_Email{+ Email: &v1pb.EmailSetting{Config: &v1pb.EmailSetting_Smtp{+ Smtp: &v1pb.EmailSetting_SMTPConfig{Password: secretSentinel},+ }},+ }}},+ },+ {+ name: "idp client secret in a returned provider",+ response: &v1pb.IdentityProvider{Config: &v1pb.IdentityProviderConfig{+ Config: &v1pb.IdentityProviderConfig_Oauth2Config{+ Oauth2Config: &v1pb.OAuth2IdentityProviderConfig{ClientSecret: secretSentinel},+ },+ }},+ },+ } {+ t.Run(tt.name, func(t *testing.T) {+ got, err := getResponseString(tt.response)+ require.NoError(t, err)+ require.NotContains(t, got, secretSentinel, "credential written to the audit log")+ })+ }+}++func TestAuditRequestRedactsCredentials(t *testing.T) {+ settingRequest := func(v *v1pb.SettingValue) *v1pb.UpdateSettingRequest {+ return &v1pb.UpdateSettingRequest{Setting: &v1pb.Setting{Value: v}}+ }+ imSetting := func(s *v1pb.AppIMSetting_IMSetting) *v1pb.UpdateSettingRequest {+ return settingRequest(&v1pb.SettingValue{Value: &v1pb.SettingValue_AppIm{+ AppIm: &v1pb.AppIMSetting{Settings: []*v1pb.AppIMSetting_IMSetting{s}},+ }})+ }++ for _, tt := range []struct {+ name string+ request any+ }{+ {"smtp password", settingRequest(&v1pb.SettingValue{Value: &v1pb.SettingValue_Email{+ Email: &v1pb.EmailSetting{Config: &v1pb.EmailSetting_Smtp{+ Smtp: &v1pb.EmailSetting_SMTPConfig{Password: secretSentinel},+ }},+ }})},+ {"ai api key", settingRequest(&v1pb.SettingValue{Value: &v1pb.SettingValue_Ai{+ Ai: &v1pb.AISetting{ApiKey: secretSentinel},+ }})},+ {"slack token", imSetting(&v1pb.AppIMSetting_IMSetting{Payload: &v1pb.AppIMSetting_IMSetting_Slack{Slack: &v1pb.AppIMSetting_Slack{Token: secretSentinel}}})},+ {"feishu app secret", imSetting(&v1pb.AppIMSetting_IMSetting{Payload: &v1pb.AppIMSetting_IMSetting_Feishu{Feishu: &v1pb.AppIMSetting_Feishu{AppSecret: secretSentinel}}})},+ {"wecom secret", imSetting(&v1pb.AppIMSetting_IMSetting{Payload: &v1pb.AppIMSetting_IMSetting_Wecom{Wecom: &v1pb.AppIMSetting_Wecom{Secret: secretSentinel}}})},+ {"lark app secret", imSetting(&v1pb.AppIMSetting_IMSetting{Payload: &v1pb.AppIMSetting_IMSetting_Lark{Lark: &v1pb.AppIMSetting_Lark{AppSecret: secretSentinel}}})},+ {"dingtalk client secret", imSetting(&v1pb.AppIMSetting_IMSetting{Payload: &v1pb.AppIMSetting_IMSetting_Dingtalk{Dingtalk: &v1pb.AppIMSetting_DingTalk{ClientSecret: secretSentinel}}})},+ {"teams client secret", imSetting(&v1pb.AppIMSetting_IMSetting{Payload: &v1pb.AppIMSetting_IMSetting_Teams{Teams: &v1pb.AppIMSetting_Teams{ClientSecret: secretSentinel}}})},+ {"oauth2 client secret", &v1pb.CreateIdentityProviderRequest{IdentityProvider: &v1pb.IdentityProvider{+ Config: &v1pb.IdentityProviderConfig{Config: &v1pb.IdentityProviderConfig_Oauth2Config{+ Oauth2Config: &v1pb.OAuth2IdentityProviderConfig{ClientSecret: secretSentinel},+ }},+ }}},+ {"oidc client secret", &v1pb.CreateIdentityProviderRequest{IdentityProvider: &v1pb.IdentityProvider{+ Config: &v1pb.IdentityProviderConfig{Config: &v1pb.IdentityProviderConfig_OidcConfig{+ OidcConfig: &v1pb.OIDCIdentityProviderConfig{ClientSecret: secretSentinel},+ }},+ }}},+ {"ldap bind password", &v1pb.UpdateIdentityProviderRequest{IdentityProvider: &v1pb.IdentityProvider{+ Config: &v1pb.IdentityProviderConfig{Config: &v1pb.IdentityProviderConfig_LdapConfig{+ LdapConfig: &v1pb.LDAPIdentityProviderConfig{BindPassword: secretSentinel},+ }},+ }}},+ } {+ t.Run(tt.name, func(t *testing.T) {+ got, err := getRequestString(tt.request)+ require.NoError(t, err)+ require.NotContains(t, got, secretSentinel, "credential written to the audit log")+ })+ }+}++// Redaction must not mutate the caller's message: these run on the live request+// and response objects, so masking in place would corrupt what the handler+// returns to the client or what a later interceptor reads.+func TestAuditRedactionDoesNotMutateInput(t *testing.T) {+ account := &v1pb.ServiceAccount{ServiceKey: secretSentinel}+ _, err := getResponseString(account)+ require.NoError(t, err)+ require.Equal(t, secretSentinel, account.GetServiceKey(), "redaction mutated the response")++ request := &v1pb.UpdateSettingRequest{Setting: &v1pb.Setting{Value: &v1pb.SettingValue{+ Value: &v1pb.SettingValue_Ai{Ai: &v1pb.AISetting{ApiKey: secretSentinel}},+ }}}+ _, err = getRequestString(request)+ require.NoError(t, err)+ require.Equal(t, secretSentinel, request.GetSetting().GetValue().GetAi().GetApiKey(),+ "redaction mutated the request")+}diff --git a/docs/design/v1-api-audit-2026-08.md b/docs/design/v1-api-audit-2026-08.mdindex 8251fd316b..c4af1b5ba6 100644--- a/docs/design/v1-api-audit-2026-08.md+++ b/docs/design/v1-api-audit-2026-08.md@@ -31,6 +31,20 @@ The external ID is the confused-deputy guard shared with the customer's AWS acco### T3 ✅ HIGH — live credentials written to the audit log in plaintext+> **FIXED.** Redactors added for `ServiceAccount` (the API key), `UpdateSettingRequest` and+> `Setting` (SMTP password, AI key, all six IM secrets), and `IdentityProvider` (OAuth2/OIDC+> client secrets, LDAP bind password), wired into both `getRequestString` and+> `getResponseString`. Covered by behavioral tests that populate each secret and assert it never+> reaches the marshaled payload; removing the redactors fails 11 of them.+>+> The IdP secrets were **not** in the original finding — the read path blanks them, so only the+> create/update *request* payload leaked. Found by walking every audited RPC's request and+> response message tree for secret-shaped fields rather than reasoning from the reported cases.+>+> Existing audit rows are not rewritten. Treat any service-account key, SMTP password, AI key, IM+> secret, or IdP client secret written before this as disclosed to anyone with audit-log read+> access, and rotate.+- **Service-account API keys.** `backend/api/v1/service_account_service.go:117` (create) and `:270` (rotate) set `result.ServiceKey = serviceKey`. `audit.go` has redactors only for `ExchangeToken` (`:463`, `:512`) — none for `ServiceAccount` — so it falls through to `default: protojson.Marshal` and the live key lands in `audit_log.response`, and in stdout when `RuntimeEnableAuditLogStdout` is set (`audit.go:312`).- The codebase already names this exact hazard for the sibling type: `redactExchangeTokenResponse` exists because *"Logging it would give anyone with audit-log read access a valid API token"* (`audit.go:612-616`).- **◐ `UpdateSetting` payloads.** No `*v1pb.UpdateSettingRequest` case in `getRequestString` (`audit.go:446-495`), so SMTP password, AI `api_key`, all six IM secrets and the directory-sync token are logged unredacted. Note `getRequestResource` *does* have a case for this type (`audit.go:433`) — the type was handled for resource extraction but not for redaction.@@ -264,7 +278,7 @@ despite `VALIDATION_STANDARDS.md`.# What I'd do, in order1. **Runtime-confirm T9.** Eleven bad logins. If the eleventh is accepted, there is no lockout and that outranks everything else here.-2. **Redact the audit path** (T3) — `ServiceAccount` and `UpdateSettingRequest`. The `ExchangeToken` redactors are the template.+2. ~~**Redact the audit path** (T3).~~ **Done** — plus IdP secrets, which the original finding missed. Rotate anything already written to the log.3. **Blank the keytab** (T2), and stop returning AWS `role_arn`/`external_id`.4. **Close the multi-resource ACL class, not the instances** (T4–T7 + the pending `DiffSchema` patch): teach `getResourceFromRequest` to collect *every* `resource_reference`d field including oneof and repeated members, then annotate `DiffSchemaRequest.changelog`, `CheckReleaseRequest.targets`, `Revision.release/file/task_run`. `UpdateDatabase`'s project-transfer case (`acl.go:604-624`) is the in-repo precedent.5. **Give sheets an ownership model** (T5), or at minimum scope the blob fetch by the owning project/workspace. Until then the sheet namespace is workspace-global by design, and should be documented as such.