Skip to content

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

chore: disallow CREATE/DROP DATABASE statements in task (#4317)

rebelice4d4bab3634f7
diff --git a/common/error.go b/common/error.go
index 41017f4576..6687c97816 100644
--- a/common/error.go
+++ b/common/error.go
@@ -38,8 +38,10 @@ const (
TaskTimingNotAllowed Code = 301
// 401 task sql type error.
- TaskTypeNotDML Code = 401
- TaskTypeNotDDL Code = 402
+ TaskTypeNotDML Code = 401
+ TaskTypeNotDDL Code = 402
+ TaskTypeDropDatabase Code = 403
+ TaskTypeCreateDatabase Code = 404
)
// Int returns the int type of code.
diff --git a/server/runner/taskcheck/statement_type_executor.go b/server/runner/taskcheck/statement_type_executor.go
index 3d86cbfdef..57ad04e230 100644
--- a/server/runner/taskcheck/statement_type_executor.go
+++ b/server/runner/taskcheck/statement_type_executor.go
@@ -64,6 +64,32 @@ func (*StatementTypeExecutor) Run(_ context.Context, taskCheckRun *api.TaskCheck
return result, nil
}
+func mysqlCreateAndDropDatabaseCheck(nodeList []tidbast.StmtNode) []api.TaskCheckResult {
+ var result []api.TaskCheckResult
+ for _, node := range nodeList {
+ switch node.(type) {
+ case *tidbast.DropDatabaseStmt:
+ result = append(result, api.TaskCheckResult{
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeDropDatabase.Int(),
+ Title: "Cannot drop database",
+ Content: fmt.Sprintf(`The statement "%s" drops database`, node.Text()),
+ })
+ case *tidbast.CreateDatabaseStmt:
+ result = append(result, api.TaskCheckResult{
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeCreateDatabase.Int(),
+ Title: "Cannot create database",
+ Content: fmt.Sprintf(`The statement "%s" creates database`, node.Text()),
+ })
+ }
+ }
+
+ return result
+}
+
func mysqlStatementTypeCheck(statement string, charset string, collation string, taskType api.TaskType) (result []api.TaskCheckResult, err error) {
// Due to the limitation of TiDB parser, we should split the multi-statement into single statements, and extract
// the TiDB unsupported statements, otherwise, the parser will panic or return the error.
@@ -116,6 +142,9 @@ func mysqlStatementTypeCheck(statement string, charset string, collation string,
}, nil
}
+ // Disallow CREATE/DROP DATABASE statements.
+ result = append(result, mysqlCreateAndDropDatabaseCheck(stmts)...)
+
switch taskType {
case api.TaskDatabaseDataUpdate:
for _, node := range stmts {
@@ -153,6 +182,31 @@ func mysqlStatementTypeCheck(statement string, charset string, collation string,
return result, nil
}
+func postgresqlCreateAndDropDatabaseCheck(nodeList []ast.Node) []api.TaskCheckResult {
+ var result []api.TaskCheckResult
+ for _, node := range nodeList {
+ switch node.(type) {
+ case *ast.DropDatabaseStmt:
+ result = append(result, api.TaskCheckResult{
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeDropDatabase.Int(),
+ Title: "Cannot drop database",
+ Content: fmt.Sprintf(`The statement "%s" drops database`, node.Text()),
+ })
+ case *ast.CreateDatabaseStmt:
+ result = append(result, api.TaskCheckResult{
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeCreateDatabase.Int(),
+ Title: "Cannot create database",
+ Content: fmt.Sprintf(`The statement "%s" creates database`, node.Text()),
+ })
+ }
+ }
+ return result
+}
+
func postgresqlStatementTypeCheck(statement string, taskType api.TaskType) (result []api.TaskCheckResult, err error) {
stmts, err := parser.Parse(parser.Postgres, parser.ParseContext{}, statement)
if err != nil {
@@ -168,6 +222,9 @@ func postgresqlStatementTypeCheck(statement string, taskType api.TaskType) (resu
}, nil
}
+ // Disallow CREATE/DROP DATABASE statements.
+ result = append(result, postgresqlCreateAndDropDatabaseCheck(stmts)...)
+
switch taskType {
case api.TaskDatabaseDataUpdate:
for _, node := range stmts {
diff --git a/server/runner/taskcheck/statement_type_executor_test.go b/server/runner/taskcheck/statement_type_executor_test.go
index 07cfcac516..99616a4d53 100644
--- a/server/runner/taskcheck/statement_type_executor_test.go
+++ b/server/runner/taskcheck/statement_type_executor_test.go
@@ -22,6 +22,73 @@ type testData struct {
func TestStatementTypeCheck(t *testing.T) {
tests := []testData{
+ {
+ stmt: "CREATE DATABASE db",
+ taskType: api.TaskDatabaseSchemaUpdate,
+ want: []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeCreateDatabase.Int(),
+ Title: "Cannot create database",
+ Content: "The statement \"CREATE DATABASE db\" creates database",
+ },
+ },
+ },
+
+ {
+ stmt: "DROP DATABASE db",
+ taskType: api.TaskDatabaseSchemaUpdate,
+ want: []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeDropDatabase.Int(),
+ Title: "Cannot drop database",
+ Content: "The statement \"DROP DATABASE db\" drops database",
+ },
+ },
+ },
+ {
+ stmt: "CREATE DATABASE db",
+ taskType: api.TaskDatabaseDataUpdate,
+ want: []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeCreateDatabase.Int(),
+ Title: "Cannot create database",
+ Content: "The statement \"CREATE DATABASE db\" creates database",
+ },
+ {
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeNotDML.Int(),
+ Title: "Data change can only run DML",
+ Content: "\"CREATE DATABASE db\" is not DML",
+ },
+ },
+ },
+ {
+ stmt: "DROP DATABASE db",
+ taskType: api.TaskDatabaseDataUpdate,
+ want: []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeDropDatabase.Int(),
+ Title: "Cannot drop database",
+ Content: "The statement \"DROP DATABASE db\" drops database",
+ },
+ {
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeNotDML.Int(),
+ Title: "Data change can only run DML",
+ Content: "\"DROP DATABASE db\" is not DML",
+ },
+ },
+ },
{
stmt: "CREATE TABLE t(a int, b int)",
taskType: api.TaskDatabaseDataUpdate,