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(masking): redact database error messages that leak masked column values (#19371)
Vincent Huange506e7ad3f3d
diff --git a/backend/api/v1/query_result_masker.go b/backend/api/v1/query_result_masker.goindex 19f5462ac8..7335ed3a02 100644--- a/backend/api/v1/query_result_masker.go+++ b/backend/api/v1/query_result_masker.go@@ -60,8 +60,16 @@ func (s *QueryResultMasker) MaskResults(ctx context.Context, spans []*parserbaseif results[i].Error == "" && spans[i].NotFoundError != nil {return errors.Errorf("masking error: %v", spans[i].NotFoundError)}- // Skip masking for error result.+ // Skip masking for error result, but redact the error message if the+ // statement touches masked columns — database errors can contain+ // actual column values (e.g. "invalid input syntax for type integer: '<value>'").+ // We check SourceColumns (covers SELECT, WHERE, JOIN, etc.) rather than+ // just Results (SELECT output) to catch cases like:+ // SELECT name FROM t WHERE CAST(masked_col AS int) > 5if results[i].Error != "" && len(results[i].Rows) == 0 {+ if i < len(spans) && spans[i] != nil && s.spanTouchesMaskedColumns(ctx, m, instance, user, spans[i]) {+ results[i].Error = "Query execution failed. Error details are hidden because the query references columns with data masking policies."+ }continue}maskers, reasons, err := s.getMaskersForQuerySpan(ctx, m, instance, user, spans[i])@@ -74,6 +82,53 @@ func (s *QueryResultMasker) MaskResults(ctx context.Context, spans []*parserbasereturn nil}+// spanTouchesMaskedColumns checks whether any column referenced anywhere in+// the query (SELECT, WHERE, JOIN, etc.) has a masking policy applied.+//+// We collect column-level entries from span.Results[*].SourceColumns and+// span.PredicateColumns rather than span.SourceColumns, because some engines+// (e.g. MySQL) populate span.SourceColumns with table-level entries only+// (Column field is empty), which cannot be matched against column configs.+func (s *QueryResultMasker) spanTouchesMaskedColumns(ctx context.Context, m *maskingLevelEvaluator, instance *store.InstanceMessage, user *store.UserMessage, span *parserbase.QuerySpan) bool {+ // Collect all column-level source columns from SELECT results and predicates.+ allColumns := make(parserbase.SourceColumnSet)+ for _, r := range span.Results {+ for col := range r.SourceColumns {+ allColumns[col] = true+ }+ }+ for col := range span.PredicateColumns {+ allColumns[col] = true+ }+ if len(allColumns) == 0 {+ return false+ }++ semanticTypesToMasker, err := buildSemanticTypeToMaskerMap(ctx, s.store)+ if err != nil {+ return false+ }++ data, err := newMaskingDataProvider(ctx, s.store, instance, span)+ if err != nil {+ return false+ }++ for column := range allColumns {+ mk, _, err := s.getMaskerForColumnResource(ctx, m, instance, column, data, user, semanticTypesToMasker)+ if err != nil {+ continue+ }+ if mk == nil {+ continue+ }+ if _, isNone := mk.(*masker.NoneMasker); !isNone {+ return true+ }+ }+ return false+}+func getAlgorithmName(m masker.Masker) string {switch m.(type) {case *masker.NoneMasker:diff --git a/backend/tests/sensitive_data_test.go b/backend/tests/sensitive_data_test.goindex cc23584f89..eda54ae080 100644--- a/backend/tests/sensitive_data_test.go+++ b/backend/tests/sensitive_data_test.go@@ -294,4 +294,31 @@ func TestSensitiveData(t *testing.T) {a.Equal("", result.Error)diff = cmp.Diff(originData, result, protocmp.Transform(), protocmp.IgnoreMessages(&durationpb.Duration{}))a.Empty(diff)++ // Query with a type-incompatible function on a masked column.+ // The database error message would normally contain the actual column value,+ // but the masking pipeline should redact it.+ errorLeakResp, err := ctl.sqlServiceClient.Query(ctx, connect.NewRequest(&v1pb.QueryRequest{+ Name: database.Name,+ Statement: "SELECT BIN_TO_UUID(author) FROM tech_book WHERE id = 1",+ DataSourceId: "admin",+ }))+ a.NoError(err)+ a.Equal(1, len(errorLeakResp.Msg.Results))+ a.NotEmpty(errorLeakResp.Msg.Results[0].Error)+ // The error should be redacted — must not contain the actual column value.+ a.NotContains(errorLeakResp.Msg.Results[0].Error, "bber")+ a.Contains(errorLeakResp.Msg.Results[0].Error, "data masking policies")++ // Same query on a non-masked column should preserve the original error.+ nonMaskedErrorResp, err := ctl.sqlServiceClient.Query(ctx, connect.NewRequest(&v1pb.QueryRequest{+ Name: database.Name,+ Statement: "SELECT BIN_TO_UUID(name) FROM tech_book WHERE id = 1",+ DataSourceId: "admin",+ }))+ a.NoError(err)+ a.Equal(1, len(nonMaskedErrorResp.Msg.Results))+ a.NotEmpty(nonMaskedErrorResp.Msg.Results[0].Error)+ // The error should NOT be redacted — name is not masked.+ a.NotContains(nonMaskedErrorResp.Msg.Results[0].Error, "data masking policies")}