Skip to content

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

fix: only block DDL/SELECT/EXPLAIN statement in CHANGE DATA (#2484)

rebelice65ee700c7acc
diff --git a/plugin/parser/ast/alter_table_stmt.go b/plugin/parser/ast/alter_table_stmt.go
index 3cc7acab2d..cf410ee172 100644
--- a/plugin/parser/ast/alter_table_stmt.go
+++ b/plugin/parser/ast/alter_table_stmt.go
@@ -2,7 +2,7 @@ package ast
// AlterTableStmt is the struct for alter table or view statement.
type AlterTableStmt struct {
- node
+ ddl
Table *TableDef
AlterItemList []Node
diff --git a/plugin/parser/ast/create_index_stmt.go b/plugin/parser/ast/create_index_stmt.go
index 1483aca57d..b90f1a3e5d 100644
--- a/plugin/parser/ast/create_index_stmt.go
+++ b/plugin/parser/ast/create_index_stmt.go
@@ -2,7 +2,7 @@ package ast
// CreateIndexStmt is the struct for create index statement.
type CreateIndexStmt struct {
- node
+ ddl
Index *IndexDef
}
diff --git a/plugin/parser/ast/create_table_stmt.go b/plugin/parser/ast/create_table_stmt.go
index 4a3fb73f69..c9f32ea5d9 100644
--- a/plugin/parser/ast/create_table_stmt.go
+++ b/plugin/parser/ast/create_table_stmt.go
@@ -2,7 +2,7 @@ package ast
// CreateTableStmt is the strcut for create table stmt.
type CreateTableStmt struct {
- node
+ ddl
IfNotExists bool
Name *TableDef
diff --git a/plugin/parser/ast/ddl_node.go b/plugin/parser/ast/ddl_node.go
new file mode 100644
index 0000000000..bee00dd10e
--- /dev/null
+++ b/plugin/parser/ast/ddl_node.go
@@ -0,0 +1,18 @@
+package ast
+
+var (
+ _ DDLNode = (*ddl)(nil)
+)
+
+// DDLNode is the interface for DDL.
+type DDLNode interface {
+ Node
+
+ ddlNode()
+}
+
+type ddl struct {
+ node
+}
+
+func (*ddl) ddlNode() {}
diff --git a/plugin/parser/ast/drop_database_stmt.go b/plugin/parser/ast/drop_database_stmt.go
index dd6ebc6e51..092672a150 100644
--- a/plugin/parser/ast/drop_database_stmt.go
+++ b/plugin/parser/ast/drop_database_stmt.go
@@ -2,7 +2,7 @@ package ast
// DropDatabaseStmt is the struct for drop database statement.
type DropDatabaseStmt struct {
- node
+ ddl
DatabaseName string
IfExists bool
diff --git a/plugin/parser/ast/drop_index_stmt.go b/plugin/parser/ast/drop_index_stmt.go
index 8f677bfa03..94edff40d8 100644
--- a/plugin/parser/ast/drop_index_stmt.go
+++ b/plugin/parser/ast/drop_index_stmt.go
@@ -2,7 +2,7 @@ package ast
// DropIndexStmt is the struct for drop index statement.
type DropIndexStmt struct {
- node
+ ddl
// Here use IndexDef because the drop index statement needs the schema name for PostgreSQL.
// If the drop index statement doesn't contain schema name, the Table of this index is nil.
diff --git a/plugin/parser/ast/drop_table_stmt.go b/plugin/parser/ast/drop_table_stmt.go
index 4bbb6f6b77..2b9aa99f6a 100644
--- a/plugin/parser/ast/drop_table_stmt.go
+++ b/plugin/parser/ast/drop_table_stmt.go
@@ -2,7 +2,7 @@ package ast
// DropTableStmt is the struct for drop table or view statement.
type DropTableStmt struct {
- node
+ ddl
TableList []*TableDef
}
diff --git a/plugin/parser/ast/rename_index_stmt.go b/plugin/parser/ast/rename_index_stmt.go
index 7b43de3f7b..25accec5d8 100644
--- a/plugin/parser/ast/rename_index_stmt.go
+++ b/plugin/parser/ast/rename_index_stmt.go
@@ -2,7 +2,7 @@ package ast
// RenameIndexStmt is the struct for the rename index statement.
type RenameIndexStmt struct {
- node
+ ddl
// For PostgreSQL, we only use TableDef.Schema.
// If this rename index statement doesn't contain schema name, Table will be not nil and the Schema name is empty string.
diff --git a/server/task_check_executor_statement_type.go b/server/task_check_executor_statement_type.go
index 309ddf62a0..4741cc38e5 100644
--- a/server/task_check_executor_statement_type.go
+++ b/server/task_check_executor_statement_type.go
@@ -100,9 +100,12 @@ func mysqlStatementTypeCheck(statement string, charset string, collation string,
switch taskType {
case api.TaskDatabaseDataUpdate:
for _, node := range stmts {
- _, isDML := node.(tidbast.DMLNode)
+ _, isDDL := node.(tidbast.DDLNode)
_, isQuery := node.(*tidbast.SelectStmt)
- if !isDML || isQuery {
+ _, isExplain := node.(*tidbast.ExplainStmt)
+ // We only want to disallow DDL, QUERY and EXPLAIN statements in CHANGE DATA.
+ // We need to run some common statements, e.g. COMMIT.
+ if isDDL || isQuery || isExplain {
result = append(result, api.TaskCheckResult{
Status: api.TaskCheckStatusError,
Namespace: api.BBNamespace,
@@ -151,7 +154,12 @@ func postgresqlStatementTypeCheck(statement string, taskType api.TaskType) (resu
switch taskType {
case api.TaskDatabaseDataUpdate:
for _, node := range stmts {
- if _, ok := node.(ast.DMLNode); !ok {
+ _, isDDL := node.(ast.DDLNode)
+ _, isSelect := node.(*ast.SelectStmt)
+ _, isExplain := node.(*ast.ExplainStmt)
+ // We only want to disallow DDL, QUERY and EXPLAIN statements in CHANGE DATA.
+ // We need to run some common statements, e.g. COMMIT.
+ if isDDL || isSelect || isExplain {
result = append(result, api.TaskCheckResult{
Status: api.TaskCheckStatusError,
Namespace: api.BBNamespace,
diff --git a/server/task_check_executor_statement_type_test.go b/server/task_check_executor_statement_type_test.go
new file mode 100644
index 0000000000..217f48ab3d
--- /dev/null
+++ b/server/task_check_executor_statement_type_test.go
@@ -0,0 +1,99 @@
+package server
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
+
+ // Register pingcap parser driver.
+ _ "github.com/pingcap/tidb/types/parser_driver"
+ // Register postgres parser driver.
+ _ "github.com/bytebase/bytebase/plugin/parser/engine/pg"
+)
+
+type testData struct {
+ stmt string
+ taskType api.TaskType
+ want []api.TaskCheckResult
+}
+
+func TestStatementTypeCheck(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "CREATE TABLE t(a int, b int)",
+ taskType: api.TaskDatabaseDataUpdate,
+ want: []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeNotDML.Int(),
+ Title: "Data change can only run DML",
+ Content: "\"CREATE TABLE t(a int, b int)\" is not DML",
+ },
+ },
+ },
+ {
+ stmt: "ALTER TABLE t ADD COLUMN a int",
+ taskType: api.TaskDatabaseDataUpdate,
+ want: []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeNotDML.Int(),
+ Title: "Data change can only run DML",
+ Content: "\"ALTER TABLE t ADD COLUMN a int\" is not DML",
+ },
+ },
+ },
+ {
+ stmt: "INSERT INTO t values(1, 2, 3)",
+ taskType: api.TaskDatabaseDataUpdate,
+ want: []api.TaskCheckResult(nil),
+ },
+ {
+ stmt: "COMMIT;",
+ taskType: api.TaskDatabaseDataUpdate,
+ want: []api.TaskCheckResult(nil),
+ },
+ {
+ stmt: "CREATE TABLE t(a int, b int)",
+ taskType: api.TaskDatabaseSchemaUpdate,
+ want: []api.TaskCheckResult(nil),
+ },
+ {
+ stmt: "ALTER TABLE t ADD COLUMN a int",
+ taskType: api.TaskDatabaseSchemaUpdate,
+ want: []api.TaskCheckResult(nil),
+ },
+ {
+ stmt: "INSERT INTO t values(1, 2, 3)",
+ taskType: api.TaskDatabaseSchemaUpdate,
+ want: []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTypeNotDDL.Int(),
+ Title: "Alter schema can only run DDL",
+ Content: "\"INSERT INTO t values(1, 2, 3)\" is not DDL",
+ },
+ },
+ },
+ {
+ stmt: "COMMIT;",
+ taskType: api.TaskDatabaseSchemaUpdate,
+ want: []api.TaskCheckResult(nil),
+ },
+ }
+
+ for _, test := range tests {
+ res, err := mysqlStatementTypeCheck(test.stmt, "", "", test.taskType)
+ require.NoError(t, err)
+ require.Equal(t, test.want, res)
+ res, err = postgresqlStatementTypeCheck(test.stmt, test.taskType)
+ require.NoError(t, err)
+ require.Equal(t, test.want, res)
+ }
+}