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(pg): treat MERGE in a CTE as a write to close a data-masking bypass (#20631)
Vincent Huange5a4ca70442c
diff --git a/backend/common/testcontainer/testcontainer.go b/backend/common/testcontainer/testcontainer.goindex ad5270e10c..08a445f4df 100644--- a/backend/common/testcontainer/testcontainer.go+++ b/backend/common/testcontainer/testcontainer.go@@ -101,10 +101,20 @@ func GetTestMySQLContainer(ctx context.Context) (retc *Container, retErr error)}, nil}-// GetPgContainer creates a PostgreSQL container for testing-func GetPgContainer(ctx context.Context) (retC *Container, retErr error) {+// GetPgContainer creates a PostgreSQL 16 container for testing.+func GetPgContainer(ctx context.Context) (*Container, error) {+ return getPgContainerWithImage(ctx, "postgres:16-alpine")+}++// GetPg17Container creates a PostgreSQL 17 container for testing. PG17 is required+// for features absent in 16 — notably MERGE ... RETURNING.+func GetPg17Container(ctx context.Context) (*Container, error) {+ return getPgContainerWithImage(ctx, "postgres:17-alpine")+}++func getPgContainerWithImage(ctx context.Context, image string) (retC *Container, retErr error) {req := testcontainers.ContainerRequest{- Image: "postgres:16-alpine",+ Image: image,Env: map[string]string{"LANG": "en_US.UTF-8","POSTGRES_PASSWORD": "root-password",@@ -185,6 +195,17 @@ func GetTestPgContainer(ctx context.Context, t testing.TB) *Container {return container}+// GetTestPg17Container creates a PostgreSQL 17 container for testing, failing the+// test on error.+func GetTestPg17Container(ctx context.Context, t testing.TB) *Container {+ t.Helper()+ container, err := GetPg17Container(ctx)+ if err != nil {+ t.Fatalf("failed to create PostgreSQL 17 container: %v", err)+ }+ return container+}+// GetOracleContainer creates an Oracle container for testingfunc GetOracleContainer(ctx context.Context) (retC *Container, retErr error) {req := testcontainers.ContainerRequest{diff --git a/backend/plugin/db/pg/query_merge_cte_test.go b/backend/plugin/db/pg/query_merge_cte_test.gonew file mode 100644index 0000000000..60cec3e685--- /dev/null+++ b/backend/plugin/db/pg/query_merge_cte_test.go@@ -0,0 +1,93 @@+package pg++import (+ "context"+ "strings"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/common/testcontainer"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/plugin/db"+)++// TestQueryConnMergeCTEDoesNotLeakRows is a security regression test for the+// PostgreSQL data-masking bypass where a MERGE smuggled into a data-modifying CTE+//+// WITH x AS (MERGE ... RETURNING masked_col) SELECT masked_col FROM x+//+// was misclassified as a read-only query (hasDMLInTree did not recognise MergeStmt+// as a write). That routed it through QueryConn's row-returning QueryContext path,+// so the RETURNING columns came back to the client and — because the query-span+// extractor cannot trace lineage through a MERGE — were returned UNMASKED.+//+// The fix classifies MERGE as a write, so the editor routes the statement to+// ExecContext, which discards the RETURNING rows. This test asserts the+// security-relevant outcome at the driver boundary: the MERGE-CTE must NOT return+// the secret value as data rows. MERGE ... RETURNING requires PostgreSQL 17.+func TestQueryConnMergeCTEDoesNotLeakRows(t *testing.T) {+ ctx := context.Background()++ pgContainer := testcontainer.GetTestPg17Container(ctx, t)+ defer pgContainer.Close(ctx)++ const secret = "TOP-SECRET-SSN-123-45-6789"+ rawDB := pgContainer.GetDB()+ require.NoError(t, rawDB.Ping())+ _, err := rawDB.ExecContext(ctx, `CREATE TABLE secret_doc (id int PRIMARY KEY, secret text);`)+ require.NoError(t, err)+ _, err = rawDB.ExecContext(ctx, `INSERT INTO secret_doc VALUES (1, '`+secret+`');`)+ require.NoError(t, err)++ driver, err := (&Driver{}).Open(ctx, storepb.Engine_POSTGRES, db.ConnectionConfig{+ DataSource: &storepb.DataSource{+ Host: pgContainer.GetHost(),+ Port: pgContainer.GetPort(),+ Username: "postgres",+ },+ Password: "root-password",+ ConnectionContext: db.ConnectionContext{DatabaseName: "postgres"},+ })+ require.NoError(t, err)+ defer driver.Close(ctx)++ conn, err := driver.GetDB().Conn(ctx)+ require.NoError(t, err)+ defer conn.Close()++ // Baseline: a plain SELECT returns the secret as a data row. This proves the+ // harness actually surfaces row data, so the negative assertion below is meaningful.+ selectResults, err := driver.QueryConn(ctx, conn, `SELECT secret FROM secret_doc`, db.QueryContext{Limit: 5000, MaximumSQLResultSize: 1 << 30})+ require.NoError(t, err)+ require.True(t, resultsContainValue(selectResults, secret),+ "baseline plain SELECT should return the secret as a data row")++ // Exploit attempt: a MERGE smuggled into a data-modifying CTE. After the fix it+ // is classified as a write and routed to Exec, so the RETURNING rows are dropped+ // and the secret is never returned as data.+ const mergeCTE = `WITH x AS (+ MERGE INTO secret_doc t USING secret_doc s ON t.id = s.id+ WHEN MATCHED THEN UPDATE SET id = t.id+ RETURNING t.secret AS secret+ ) SELECT secret FROM x`+ mergeResults, err := driver.QueryConn(ctx, conn, mergeCTE, db.QueryContext{Limit: 5000, MaximumSQLResultSize: 1 << 30})+ require.NoError(t, err)+ require.False(t, resultsContainValue(mergeResults, secret),+ "SECURITY: MERGE-in-CTE must not return the masked column as data rows; got %v", mergeResults)+}++// resultsContainValue reports whether any string cell in the results contains want.+func resultsContainValue(results []*v1pb.QueryResult, want string) bool {+ for _, r := range results {+ for _, row := range r.GetRows() {+ for _, v := range row.GetValues() {+ if sv, ok := v.GetKind().(*v1pb.RowValue_StringValue); ok && strings.Contains(sv.StringValue, want) {+ return true+ }+ }+ }+ }+ return false+}diff --git a/backend/plugin/parser/pg/query_antlr.go b/backend/plugin/parser/pg/query_antlr.goindex 299328638c..565a44a985 100644--- a/backend/plugin/parser/pg/query_antlr.go+++ b/backend/plugin/parser/pg/query_antlr.go@@ -15,7 +15,7 @@ import (// 1. Allow: SELECT statements// 2. Allow: EXPLAIN statements (but not EXPLAIN ANALYZE for non-SELECT)// 3. Allow: SHOW/SET statements (SET is considered executable)-// 4. Allow: CTEs with only SELECT (reject CTEs with INSERT/UPDATE/DELETE)+// 4. Allow: CTEs with only SELECT (reject CTEs with INSERT/UPDATE/DELETE/MERGE)// 5. Reject: All other statements (DDL, DML except SELECT)func validateQueryANTLR(statement string) (bool, bool, error) {stmts, err := ParsePg(statement)@@ -82,7 +82,12 @@ func isExplainAnalyze(n *ast.ExplainStmt) bool {return false}-// hasDMLInTree walks the AST tree and returns true if any INSERT/UPDATE/DELETE is found.+// hasDMLInTree walks the AST tree and returns true if any INSERT/UPDATE/DELETE/MERGE+// is found. MERGE is included so a data-modifying MERGE smuggled into a CTE+// (e.g. WITH x AS (MERGE ... RETURNING col) SELECT col FROM x) is classified as a+// write: this keeps the set aligned with classifyQueryType (query_type.go) and+// stops the editor from taking the row-returning read path for it, which would+// otherwise return masked columns unmasked.func hasDMLInTree(node ast.Node) bool {found := falseast.Inspect(node, func(n ast.Node) bool {@@ -90,7 +95,7 @@ func hasDMLInTree(node ast.Node) bool {return false}switch n.(type) {- case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt:+ case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt, *ast.MergeStmt:found = truereturn false}diff --git a/backend/plugin/parser/pg/query_test.go b/backend/plugin/parser/pg/query_test.goindex 6df7655982..ad2ffeaac2 100644--- a/backend/plugin/parser/pg/query_test.go+++ b/backend/plugin/parser/pg/query_test.go@@ -74,6 +74,23 @@ func TestValidateSQLForEditor(t *testing.T) {valid: false,allQuery: false,},+ {+ // Security regression: a MERGE smuggled into a data-modifying CTE must+ // be treated as a write (not read-only), so the editor routes it to Exec+ // (RETURNING rows discarded) and export rejects it. Before the fix,+ // hasDMLInTree missed MergeStmt, so this took the read path and returned+ // masked columns unmasked.+ sql: `WITH x AS (MERGE INTO t a USING t b ON a.id = b.id WHEN MATCHED THEN UPDATE SET id = a.id RETURNING a.secret) SELECT secret FROM x`,+ valid: false,+ allQuery: false,+ },+ {+ // Top-level MERGE ... RETURNING is already blocked by the default case;+ // pin it so the classifier sets stay aligned.+ sql: `MERGE INTO t a USING t b ON a.id = b.id WHEN MATCHED THEN UPDATE SET id = a.id RETURNING a.secret`,+ valid: false,+ allQuery: false,+ },{sql: "select * from t where a = 'klasjdfkljsa$tag$; -- lkjdlkfajslkdfj'",valid: true,