Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(masking): report the masked-condition refusal without a code prefix (#21664)

rebelice88e5a792c5b2
diff --git a/backend/api/v1/predicate_columns_check.go b/backend/api/v1/predicate_columns_check.go
index 4bb66187ff..e009bd276c 100644
--- a/backend/api/v1/predicate_columns_check.go
+++ b/backend/api/v1/predicate_columns_check.go
@@ -14,17 +14,17 @@ import (
"github.com/bytebase/bytebase/backend/store"
)
-// refuseSensitivePredicateColumns refuses, before execution, statements whose
-// conditions read a column masked for user. The database evaluates conditions
-// on unmasked values, so the rows it returns, their count, and the time it
-// takes would reveal them.
-func refuseSensitivePredicateColumns(ctx context.Context, stores *store.Store, spans []*base.QuerySpan, instance *store.InstanceMessage, user *store.UserMessage) error {
+// sensitivePredicateRefusal returns the message to report for statements whose
+// conditions read a column masked for user, or "" if none do. The database
+// evaluates conditions on unmasked values, so the rows it returns, their
+// count, and the time it takes would reveal them.
+func sensitivePredicateRefusal(ctx context.Context, stores *store.Store, spans []*base.QuerySpan, instance *store.InstanceMessage, user *store.UserMessage) (string, error) {
if !isPredicateColumnsCheckEnabled(instance.Metadata.GetEngine()) {
- return nil
+ return "", nil
}
sensitiveColumns, err := NewQueryResultMasker(stores).ExtractSensitivePredicateColumns(ctx, spans, instance, user)
if err != nil {
- return connect.NewError(connect.CodeInternal, errors.New(err.Error()))
+ return "", connect.NewError(connect.CodeInternal, errors.New(err.Error()))
}
for _, columns := range sensitiveColumns {
if len(columns) == 0 {
@@ -35,9 +35,9 @@ func refuseSensitivePredicateColumns(ctx context.Context, stores *store.Store, s
names = append(names, column.String())
}
slices.Sort(names)
- return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("Using sensitive columns in query conditions is not allowed: %s", strings.Join(names, ", ")))
+ return "Using sensitive columns in query conditions is not allowed: " + strings.Join(names, ", "), nil
}
- return nil
+ return "", nil
}
func (s *QueryResultMasker) ExtractSensitivePredicateColumns(ctx context.Context, spans []*base.QuerySpan, instance *store.InstanceMessage, user *store.UserMessage) ([][]base.ColumnResource, error) {
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index f9d0175537..dd78e642a4 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -871,9 +871,13 @@ func queryRetry(
return nil, nil, time.Duration(0), err
}
}
- if err := refuseSensitivePredicateColumns(ctx, stores, spans, instance, user); err != nil {
+ refusal, err := sensitivePredicateRefusal(ctx, stores, spans, instance, user)
+ if err != nil {
return nil, nil, time.Duration(0), err
}
+ if refusal != "" {
+ return refusedQueryResults(refusal, originalStatement), nil, time.Duration(0), nil
+ }
if err := preExecuteMaskingCheck(ctx, stores, instance.Metadata.GetEngine(), database, spans); err != nil {
return nil, nil, time.Duration(0), err
}
@@ -932,9 +936,13 @@ func queryRetry(
// A table the first span did not find may surface a condition the
// pre-execution check could not see.
if maskingEnabled {
- if err := refuseSensitivePredicateColumns(ctx, stores, spans, instance, user); err != nil {
+ refusal, err := sensitivePredicateRefusal(ctx, stores, spans, instance, user)
+ if err != nil {
return nil, nil, duration, err
}
+ if refusal != "" {
+ return refusedQueryResults(refusal, originalStatement), nil, duration, nil
+ }
}
}
// The second query span should not tolerate any error, but we should retail the original error from database if possible.
@@ -969,6 +977,12 @@ func queryRetry(
return results, spans, duration, nil
}
+// refusedQueryResults reports a refusal as the statement's error rather than
+// the request's, so every engine shows the message without an RPC code prefix.
+func refusedQueryResults(refusal, statement string) []*v1pb.QueryResult {
+ return []*v1pb.QueryResult{{Error: refusal, Statement: statement}}
+}
+
// buildQuerySpans analyzes statements for access checks and masking.
func buildQuerySpans(
ctx context.Context,
diff --git a/backend/tests/sensitive_data_test.go b/backend/tests/sensitive_data_test.go
index e2714d7067..994771d6f3 100644
--- a/backend/tests/sensitive_data_test.go
+++ b/backend/tests/sensitive_data_test.go
@@ -326,7 +326,7 @@ func TestSensitiveData(t *testing.T) {
}))
a.NoError(err, statement)
a.Len(resp.Msg.Results, 1, statement)
- a.Contains(resp.Msg.Results[0].Error, refusedPredicate, statement)
+ a.True(strings.HasPrefix(resp.Msg.Results[0].Error, refusedPredicate), "%s: %s", statement, resp.Msg.Results[0].Error)
a.Empty(resp.Msg.Results[0].Rows, statement)
}
@@ -365,7 +365,7 @@ func TestSensitiveData(t *testing.T) {
a.Len(batchResp.Msg.Results, 2)
a.Empty(batchResp.Msg.Results[0].Error)
a.Len(batchResp.Msg.Results[0].Rows, 3)
- a.Contains(batchResp.Msg.Results[1].Error, refusedPredicate)
+ a.Equal(fmt.Sprintf("%s: %s.public.%s.author", refusedPredicate, databaseName, tableName), batchResp.Msg.Results[1].Error)
// 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.
@@ -429,5 +429,5 @@ func TestSensitiveData(t *testing.T) {
a.NoError(err)
a.Less(time.Since(start), 20*time.Second)
a.Len(ghostResp.Msg.Results, 1)
- a.Contains(ghostResp.Msg.Results[0].Error, refusedPredicate)
+ a.True(strings.HasPrefix(ghostResp.Msg.Results[0].Error, refusedPredicate), ghostResp.Msg.Results[0].Error)
}