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(doris,starrocks): let a session SET through the read-only editor path (#21732)
Danny Xuf000789852d6
diff --git a/backend/plugin/parser/doris/query.go b/backend/plugin/parser/doris/query.goindex 83b69a4b24..0e35cfa64a 100644--- a/backend/plugin/parser/doris/query.go+++ b/backend/plugin/parser/doris/query.go@@ -16,7 +16,7 @@ func init() {// suitable for the SQL editor / data-query path.//// Decision is AST-based: each parsed top-level statement must be a SELECT,-// SHOW, DESCRIBE, EXPLAIN, or HELP. Relying on leading-keyword classification+// SHOW, DESCRIBE, EXPLAIN, HELP, or a session SET. Relying on leading-keyword classification// alone would mis-accept CTE-prefixed DML such as `WITH x AS (...) UPDATE ...`,// which Classify would tag as SELECT because `WITH` is its first token.//@@ -24,7 +24,9 @@ func init() {// malformed read-only SQL before execution.//// The (bool, bool, error) return shape matches the bytebase QueryValidator-// contract: (isReadOnly, isExplicitReadOnly, syntaxError).+// contract: (isReadOnly, isExplicitReadOnly, syntaxError). isExplicitReadOnly is+// false once a statement needs executing rather than reading, which is how the+// driver decides between QueryContext and ExecContext.func validateQuery(statement string) (bool, bool, error) {// INTO OUTFILE exports to S3 or HDFS, past masking and the export gate.// Matched on text because omni's Doris grammar has no INTO node: it parses@@ -38,12 +40,33 @@ func validateQuery(statement string) (bool, bool, error) {if err != nil {return false, false, err}+ var hasExecute boolfor _, p := range parsed {- if !isReadOnlyAST(p.Node()) {+ switch {+ case isReadOnlyAST(p.Node()):+ case needsExecute(p.Node()):+ hasExecute = true+ default:return false, false, nil}}- return true, true, nil+ return true, !hasExecute, nil+}++// needsExecute reports whether a statement is allowed in the editor but is not a+// read. A session variable assignment returns no rows, so the driver has to run it+// with ExecContext rather than read a result set; validateQuery signals that through+// its second return value. MySQL and Postgres gate SET the same way.+//+// Only variable assignment qualifies. SET PASSWORD and SET DEFAULT STORAGE VAULT are+// separate nodes and stay refused, as they are for Postgres: they administer the+// server rather than prepare a read.+func needsExecute(node ast.Node) bool {+ switch node.(type) {+ case *ast.SetStmt, *ast.UnsetStmt:+ return true+ }+ return false}// isReadOnlyAST returns true when the given top-level AST node represents adiff --git a/backend/plugin/parser/doris/query_test.go b/backend/plugin/parser/doris/query_test.goindex 86aff1ab9e..c1e88fc553 100644--- a/backend/plugin/parser/doris/query_test.go+++ b/backend/plugin/parser/doris/query_test.go@@ -280,3 +280,38 @@ func TestValidateQuery(t *testing.T) {})}}++// A session SET is permitted but is not a read: validateQuery accepts it while+// reporting isExplicitReadOnly false, which is how the driver knows to run it with+// ExecContext instead of reading a result set. Doris had no case for *ast.SetStmt, so+// `SET time_zone = 'UTC'` was refused outright with "Support read-only command+// statements only" while MySQL, Postgres and TiDB all allowed it.+func TestValidateQuerySetStatements(t *testing.T) {+ for _, tc := range []struct {+ name string+ statement string+ wantValid bool+ wantExplicitRead bool+ }{+ {"a session variable", "SET time_zone = 'UTC'", true, false},+ {"the @@ spelling", "SET @@time_zone = 'UTC'", true, false},+ {"an explicit session scope", "SET SESSION time_zone = 'UTC'", true, false},+ // Global scope is permitted here as it is for MySQL and Postgres: this is not+ // the permission gate, it only picks the execution path.+ {"a global scope", "SET GLOBAL time_zone = 'UTC'", true, false},+ {"SET NAMES", "SET NAMES utf8mb4", true, false},+ {"UNSET VARIABLE", "UNSET VARIABLE time_zone", true, false},+ {"a read stays explicitly read-only", "SELECT 1", true, true},+ {"a SET anywhere in a script needs executing", "SELECT 1; SET time_zone = 'UTC'", true, false},+ {"a SET does not launder a DDL beside it", "SET time_zone = 'UTC'; DROP TABLE t", false, false},+ {"SET PASSWORD administers the server", "SET PASSWORD FOR 'u' = 'x'", false, false},+ {"SET DEFAULT STORAGE VAULT administers the server", "SET DEFAULT STORAGE VAULT v", false, false},+ } {+ t.Run(tc.name, func(t *testing.T) {+ valid, explicitReadOnly, err := validateQuery(tc.statement)+ require.NoError(t, err)+ require.Equal(t, tc.wantValid, valid)+ require.Equal(t, tc.wantExplicitRead, explicitReadOnly)+ })+ }+}diff --git a/backend/plugin/parser/starrocks/query.go b/backend/plugin/parser/starrocks/query.goindex c372697142..beb39b5e2c 100644--- a/backend/plugin/parser/starrocks/query.go+++ b/backend/plugin/parser/starrocks/query.go@@ -15,7 +15,7 @@ func init() {// suitable for the SQL editor / data-query path.//// Decision is AST-based: each parsed top-level statement must be a SELECT,-// SHOW, DESCRIBE, EXPLAIN, or HELP. Relying on leading-keyword classification+// SHOW, DESCRIBE, EXPLAIN, HELP, or a session SET. Relying on leading-keyword classification// alone would mis-accept CTE-prefixed DML such as `WITH x AS (...) UPDATE ...`,// which Classify would tag as SELECT because `WITH` is its first token.//@@ -23,18 +23,41 @@ func init() {// malformed read-only SQL before execution.//// The (bool, bool, error) return shape matches the bytebase QueryValidator-// contract: (isReadOnly, isExplicitReadOnly, syntaxError).+// contract: (isReadOnly, isExplicitReadOnly, syntaxError). isExplicitReadOnly is+// false once a statement needs executing rather than reading, which is how the+// driver decides between QueryContext and ExecContext.func validateQuery(statement string) (bool, bool, error) {parsed, err := parseStarRocksSQL(statement)if err != nil {return false, false, err}+ var hasExecute boolfor _, p := range parsed {- if !isReadOnlyAST(p.Node()) {+ switch {+ case isReadOnlyAST(p.Node()):+ case needsExecute(p.Node()):+ hasExecute = true+ default:return false, false, nil}}- return true, true, nil+ return true, !hasExecute, nil+}++// needsExecute reports whether a statement is allowed in the editor but is not a+// read. A session variable assignment returns no rows, so the driver has to run it+// with ExecContext rather than read a result set; validateQuery signals that through+// its second return value. MySQL and Postgres gate SET the same way.+//+// Only variable assignment qualifies. SET PASSWORD and SET DEFAULT STORAGE VAULT are+// separate nodes and stay refused, as they are for Postgres: they administer the+// server rather than prepare a read.+func needsExecute(node ast.Node) bool {+ switch node.(type) {+ case *ast.SetStmt, *ast.UnsetStmt:+ return true+ }+ return false}// isReadOnlyAST returns true when the given top-level AST node represents adiff --git a/backend/plugin/parser/starrocks/query_test.go b/backend/plugin/parser/starrocks/query_test.goindex 7ae808ccfe..6daadabd31 100644--- a/backend/plugin/parser/starrocks/query_test.go+++ b/backend/plugin/parser/starrocks/query_test.go@@ -284,3 +284,38 @@ func TestValidateQuery(t *testing.T) {})}}++// A session SET is permitted but is not a read: validateQuery accepts it while+// reporting isExplicitReadOnly false, which is how the driver knows to run it with+// ExecContext instead of reading a result set. StarRocks had no case for *ast.SetStmt, so+// `SET time_zone = 'UTC'` was refused outright with "Support read-only command+// statements only" while MySQL, Postgres and TiDB all allowed it.+func TestValidateQuerySetStatements(t *testing.T) {+ for _, tc := range []struct {+ name string+ statement string+ wantValid bool+ wantExplicitRead bool+ }{+ {"a session variable", "SET time_zone = 'UTC'", true, false},+ {"the @@ spelling", "SET @@time_zone = 'UTC'", true, false},+ {"an explicit session scope", "SET SESSION time_zone = 'UTC'", true, false},+ // Global scope is permitted here as it is for MySQL and Postgres: this is not+ // the permission gate, it only picks the execution path.+ {"a global scope", "SET GLOBAL time_zone = 'UTC'", true, false},+ {"SET NAMES", "SET NAMES utf8mb4", true, false},+ {"UNSET VARIABLE", "UNSET VARIABLE time_zone", true, false},+ {"a read stays explicitly read-only", "SELECT 1", true, true},+ {"a SET anywhere in a script needs executing", "SELECT 1; SET time_zone = 'UTC'", true, false},+ {"a SET does not launder a DDL beside it", "SET time_zone = 'UTC'; DROP TABLE t", false, false},+ {"SET PASSWORD administers the server", "SET PASSWORD FOR 'u' = 'x'", false, false},+ {"SET DEFAULT STORAGE VAULT administers the server", "SET DEFAULT STORAGE VAULT v", false, false},+ } {+ t.Run(tc.name, func(t *testing.T) {+ valid, explicitReadOnly, err := validateQuery(tc.statement)+ require.NoError(t, err)+ require.Equal(t, tc.wantValid, valid)+ require.Equal(t, tc.wantExplicitRead, explicitReadOnly)+ })+ }+}