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): refuse a result whose columns do not match its query span (#21650)
rebelice42bbcae13221
diff --git a/backend/api/v1/query_result_masker.go b/backend/api/v1/query_result_masker.goindex d0e3281df4..ea2967c7fd 100644--- a/backend/api/v1/query_result_masker.go+++ b/backend/api/v1/query_result_masker.go@@ -83,6 +83,11 @@ func (s *QueryResultMasker) MaskResults(ctx context.Context, spans []*parserbase}continue}+ if columnsMisalignedWithSpan(spans[i], results[i]) && s.spanTouchesMaskedColumns(ctx, m, instance, user, spans[i]) {+ return errors.Errorf(+ "masking cannot be applied: the query returned %d columns but Bytebase resolved %d from the synced schema, so the query was not returned",+ len(results[i].ColumnNames), len(spans[i].Results))+ }maskers, reasons, err := s.getMaskersForQuerySpan(ctx, m, instance, user, spans[i])if err != nil {return errors.Wrapf(err, "failed to get maskers for query span")@@ -102,6 +107,18 @@ func maskingBlockedByUnresolvedColumns(span *parserbase.QuerySpan, instance *storeturn common.EngineSupportMasking(instance.Metadata.GetEngine())}+// columnsMisalignedWithSpan reports whether doMaskResult, which applies the+// span's maskers by position, would put a value beside the wrong masker or past+// the last one. A DML or DDL result reports on the command (Affected Rows)+// rather than projecting span.Results. A result without rows has no values to+// misplace, and BigQuery returns no column names for one.+func columnsMisalignedWithSpan(span *parserbase.QuerySpan, result *v1pb.QueryResult) bool {+ if span.Type == parserbase.DML || span.Type == parserbase.DDL {+ return false+ }+ return len(result.Rows) > 0 && len(result.ColumnNames) != len(span.Results)+}+// spanTouchesMaskedColumns checks whether any column referenced anywhere in// the query (SELECT, WHERE, JOIN, etc.) has a masking policy applied.//diff --git a/backend/api/v1/query_result_masker_test.go b/backend/api/v1/query_result_masker_test.goindex cb54bb3d2a..d8f8d61b5d 100644--- a/backend/api/v1/query_result_masker_test.go+++ b/backend/api/v1/query_result_masker_test.go@@ -6,6 +6,7 @@ import ("github.com/stretchr/testify/require"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base""github.com/bytebase/bytebase/backend/store")@@ -87,3 +88,33 @@ func TestMaskingEnginesAgreeWithSignalProducers(t *testing.T) {"%s masks, so it must act on the signal once its extractor sets one", engine)}}++func TestColumnsMisalignedWithSpan(t *testing.T) {+ spanOf := func(n int) *parserbase.QuerySpan {+ return &parserbase.QuerySpan{Type: parserbase.Select, Results: make([]parserbase.QuerySpanResult, n)}+ }+ resultOf := func(columns, rows int) *v1pb.QueryResult {+ r := &v1pb.QueryResult{ColumnNames: make([]string, columns)}+ for range rows {+ r.Rows = append(r.Rows, &v1pb.QueryRow{Values: make([]*v1pb.RowValue, columns)})+ }+ return r+ }+ testCases := []struct {+ name string+ span *parserbase.QuerySpan+ result *v1pb.QueryResult+ want bool+ }{+ {name: "same column count", span: spanOf(3), result: resultOf(3, 2), want: false},+ {name: "more columns than the span resolved", span: spanOf(4), result: resultOf(5, 2), want: true},+ {name: "fewer columns than the span resolved", span: spanOf(3), result: resultOf(2, 2), want: true},+ {name: "no rows", span: spanOf(3), result: resultOf(0, 0), want: false},+ {name: "DML affected rows", span: &parserbase.QuerySpan{Type: parserbase.DML}, result: resultOf(1, 1), want: false},+ }+ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ require.Equal(t, tc.want, columnsMisalignedWithSpan(tc.span, tc.result))+ })+ }+}diff --git a/backend/tests/sensitive_data_test.go b/backend/tests/sensitive_data_test.goindex 676664c698..aa3c9577d4 100644--- a/backend/tests/sensitive_data_test.go+++ b/backend/tests/sensitive_data_test.go@@ -2,6 +2,7 @@ package testsimport ("context"+ "database/sql""fmt""strings""testing"@@ -303,4 +304,32 @@ func TestSensitiveData(t *testing.T) {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")++ // Created directly on the target, after the last schema sync, so the synced+ // schema does not know the function or how many columns it returns.+ targetDB, err := sql.Open("pgx", fmt.Sprintf("postgresql://postgres:root-password@%s:%s/%s?sslmode=disable", pgContainer.GetHost(), pgContainer.GetPort(), databaseName))+ a.NoError(err)+ defer targetDB.Close()+ _, err = targetDB.ExecContext(ctx, `CREATE FUNCTION unsynced_rows() RETURNS TABLE(a int, b int) LANGUAGE sql AS 'SELECT 1, 2'`)+ a.NoError(err)++ // The span lists one placeholder column for the function, so tech_book's+ // columns would shift onto the wrong maskers and author past the last one.+ misalignedResp, err := ctl.sqlServiceClient.Query(ctx, connect.NewRequest(&v1pb.QueryRequest{+ Name: database.Name,+ Statement: "SELECT * FROM unsynced_rows() f CROSS JOIN tech_book",+ }))+ a.NoError(err)+ a.Len(misalignedResp.Msg.Results, 1)+ a.Contains(misalignedResp.Msg.Results[0].Error, "masking cannot be applied: the query returned 5 columns but Bytebase resolved 4")+ a.Empty(misalignedResp.Msg.Results[0].Rows)++ unmaskedResp, err := ctl.sqlServiceClient.Query(ctx, connect.NewRequest(&v1pb.QueryRequest{+ Name: database.Name,+ Statement: "SELECT * FROM unsynced_rows()",+ }))+ a.NoError(err)+ a.Len(unmaskedResp.Msg.Results, 1)+ a.Equal([]string{"a", "b"}, unmaskedResp.Msg.Results[0].ColumnNames)+ a.Len(unmaskedResp.Msg.Results[0].Rows, 1)}