Skip to content

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

fix(pg): fail-safe whitelist for read-only CTE detection + SELECT INTO (#20645)

Vincent Huanged3149a53a49
diff --git a/backend/plugin/parser/pg/query_antlr.go b/backend/plugin/parser/pg/query_antlr.go
index 565a44a985..31f855d74c 100644
--- a/backend/plugin/parser/pg/query_antlr.go
+++ b/backend/plugin/parser/pg/query_antlr.go
@@ -12,11 +12,12 @@ import (
// Returns (isReadOnly, allQueriesReturnData, error)
//
// Validation rules:
-// 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/MERGE)
-// 5. Reject: All other statements (DDL, DML except SELECT)
+// 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 whose every term is a read (SELECT/VALUES/TABLE/set-op/RECURSIVE);
+// reject a SELECT with a data-modifying CTE term — a fail-safe whitelist.
+// 5. Reject: SELECT ... INTO (creates a table) and all other statements (DDL, DML).
func validateQueryANTLR(statement string) (bool, bool, error) {
stmts, err := ParsePg(statement)
if err != nil {
@@ -35,19 +36,17 @@ func validateQueryANTLR(statement string) (bool, bool, error) {
switch n := stmt.AST.(type) {
case *ast.SelectStmt:
- // SELECT is allowed. Check for DML in CTEs.
- if n.WithClause != nil && hasDMLInTree(n) {
+ // SELECT is read-only unless it writes: SELECT ... INTO creates a table,
+ // or a data-modifying CTE smuggles a write into the read path.
+ if isWriteSelect(n) {
return false, false, nil
}
case *ast.ExplainStmt:
if isExplainAnalyze(n) {
- // EXPLAIN ANALYZE is only valid for SELECT
- if _, ok := n.Query.(*ast.SelectStmt); !ok {
- return false, false, nil
- }
- // Check for DML in CTEs within the explained SELECT
- if hasDMLInTree(n.Query) {
+ // EXPLAIN ANALYZE executes the query, so it must be a read-only SELECT.
+ sel, ok := n.Query.(*ast.SelectStmt)
+ if !ok || isWriteSelect(sel) {
return false, false, nil
}
}
@@ -82,22 +81,40 @@ func isExplainAnalyze(n *ast.ExplainStmt) bool {
return false
}
-// 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 {
+// isWriteSelect reports whether a SelectStmt actually writes — and so must not take
+// the read-only editor path. A SELECT writes if it has an INTO target (SELECT ...
+// INTO creates a table) or any of its CTE terms is data-modifying.
+//
+// This is the unified write-detection primitive for the read-only gate;
+// classifyQueryType (query_type.go) keeps reporting the root statement type for its
+// own consumers — only the detection is shared, not the classifiers' outputs.
+func isWriteSelect(n *ast.SelectStmt) bool {
+ if n == nil {
+ return false
+ }
+ if hasOmniIntoClause(n) {
+ return true
+ }
+ return containsWriteCTE(n)
+}
+
+// containsWriteCTE reports whether any common table expression in the tree is
+// data-modifying. Fail-safe by construction — a whitelist, not a denylist: a CTE
+// term is read-only ONLY if it is an *ast.SelectStmt, which is how pg models
+// SELECT, VALUES, TABLE, set operations and WITH RECURSIVE. Anything else —
+// INSERT/UPDATE/DELETE/MERGE today, or any write node added to the grammar
+// tomorrow — counts as a write, so the next forgotten write type fails closed.
+func containsWriteCTE(node ast.Node) bool {
found := false
ast.Inspect(node, func(n ast.Node) bool {
if found {
return false
}
- switch n.(type) {
- case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt, *ast.MergeStmt:
- found = true
- return false
+ if cte, ok := n.(*ast.CommonTableExpr); ok {
+ if _, isSelect := cte.Ctequery.(*ast.SelectStmt); !isSelect {
+ found = true
+ return false
+ }
}
return true
})
diff --git a/backend/plugin/parser/pg/query_test.go b/backend/plugin/parser/pg/query_test.go
index ad2ffeaac2..fecc57bda5 100644
--- a/backend/plugin/parser/pg/query_test.go
+++ b/backend/plugin/parser/pg/query_test.go
@@ -91,6 +91,38 @@ func TestValidateSQLForEditor(t *testing.T) {
valid: false,
allQuery: false,
},
+ {
+ // BYT-9751 RED→GREEN: SELECT ... INTO creates a table (a write), but the
+ // validator treated it as read-only — it never checked the INTO clause.
+ sql: `SELECT a INTO t2 FROM t`,
+ valid: false,
+ allQuery: false,
+ },
+ {
+ // no-over-block guard: a data-modifying CTE must reject, but read-only
+ // CTE terms pg models as SelectStmt must stay allowed — VALUES.
+ sql: `WITH x AS (VALUES (1), (2)) SELECT * FROM x`,
+ valid: true,
+ allQuery: true,
+ },
+ {
+ // no-over-block guard: TABLE t is a SelectStmt CTE term.
+ sql: `WITH x AS (TABLE t) SELECT * FROM x`,
+ valid: true,
+ allQuery: true,
+ },
+ {
+ // no-over-block guard: a set-operation CTE term is a SelectStmt.
+ sql: `WITH x AS (SELECT 1 UNION SELECT 2) SELECT * FROM x`,
+ valid: true,
+ allQuery: true,
+ },
+ {
+ // no-over-block guard: WITH RECURSIVE — the CTE term is still a SelectStmt.
+ sql: `WITH RECURSIVE x AS (SELECT 1 UNION SELECT n + 1 FROM x WHERE n < 5) SELECT * FROM x`,
+ valid: true,
+ allQuery: true,
+ },
{
sql: "select * from t where a = 'klasjdfkljsa$tag$; -- lkjdlkfajslkdfj'",
valid: true,