Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: disallow multi-statement SQL editor queries and allow more types of EXPLAIN queries (#1020)
Danny Xu9437dba8eb9b
diff --git a/server/sql.go b/server/sql.goindex 36b4f82db2..520a56124a 100644--- a/server/sql.go+++ b/server/sql.go@@ -1,6 +1,7 @@package serverimport (+ "bufio""context""encoding/json""fmt"@@ -12,6 +13,7 @@ import ("github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common""github.com/bytebase/bytebase/plugin/db"+ "github.com/bytebase/bytebase/plugin/db/util""github.com/google/jsonapi""github.com/labstack/echo/v4""go.uber.org/zap"@@ -646,7 +648,21 @@ func getLatestSchemaVersion(ctx context.Context, driver db.Driver, databaseName}func validateSQLSelectStatement(sqlStatement string) bool {- whiteListRegs := []string{`^SELECT$`, `^EXPLAIN\s+?SELECT$`, `^(EXPLAIN\s+?)?SELECT\s`}+ // Check if the query has only one statement.+ count := 0+ sc := bufio.NewScanner(strings.NewReader(sqlStatement))+ if err := util.ApplyMultiStatements(sc, func(stmt string) error {+ count+++ return nil+ }); err != nil {+ return false+ }+ if count != 1 {+ return false+ }++ // Allow SELECT and EXPLAIN queries only.+ whiteListRegs := []string{`^SELECT\s+?`, `^EXPLAIN\s+?`}formatedStr := strings.ToUpper(strings.TrimSpace(sqlStatement))for _, reg := range whiteListRegs {matchResult, _ := regexp.MatchString(reg, formatedStr)diff --git a/server/sql_test.go b/server/sql_test.goindex 7c87026443..f41243f686 100644--- a/server/sql_test.go+++ b/server/sql_test.go@@ -31,11 +31,11 @@ func TestValidateSQLSelectStatement(t *testing.T) {},{sqlStatement: "select ",- want: true,+ want: false,},{sqlStatement: "select",- want: true,+ want: false,},{sqlStatement: "explain select",@@ -81,6 +81,10 @@ func TestValidateSQLSelectStatement(t *testing.T) {sqlStatement: "",want: false,},+ {+ sqlStatement: "SETEST 1; INSERT INTO tbl(num) VALUES(113);",+ want: false,+ },}for _, test := range tests {