Skip to content

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

fix: get the explicit the database name from schema info (#3731)

rebelice493374a03cca
diff --git a/plugin/db/util/driverutil_test.go b/plugin/db/util/driverutil_test.go
index 58033e5e87..f14be4f9cc 100644
--- a/plugin/db/util/driverutil_test.go
+++ b/plugin/db/util/driverutil_test.go
@@ -278,6 +278,17 @@ func TestExtractSensitiveField(t *testing.T) {
schemaInfo *db.SensitiveSchemaInfo
fieldList []db.SensitiveField
}{
+ {
+ // Test for explicit database name.
+ statement: `select concat(db.t.a, db.t.b, db.t.c) from t`,
+ schemaInfo: defaultDatabaseSchema,
+ fieldList: []db.SensitiveField{
+ {
+ Name: "concat(db.t.a, db.t.b, db.t.c)",
+ Sensitive: true,
+ },
+ },
+ },
{
// Test for Recursive Common Table Expression dependent closures.
statement: `
diff --git a/plugin/db/util/mask_sensitive_data.go b/plugin/db/util/mask_sensitive_data.go
index f73d9bfbc3..c6a3350e39 100644
--- a/plugin/db/util/mask_sensitive_data.go
+++ b/plugin/db/util/mask_sensitive_data.go
@@ -533,7 +533,7 @@ func (extractor *sensitiveFieldExtractor) extractTableSource(node *tidbast.Table
return res, nil
}
-func (extractor *sensitiveFieldExtractor) findTableSchema(databaseName string, tableName string) (db.TableSchema, error) {
+func (extractor *sensitiveFieldExtractor) findTableSchema(databaseName string, tableName string) (string, db.TableSchema, error) {
// Each CTE name in one WITH clause must be unique, but we can use the same name in the different level CTE, such as:
//
// with tt2 as (
@@ -546,7 +546,7 @@ func (extractor *sensitiveFieldExtractor) findTableSchema(databaseName string, t
for i := len(extractor.cteOuterSchemaInfo) - 1; i >= 0; i-- {
table := extractor.cteOuterSchemaInfo[i]
if databaseName == "" && table.Name == tableName {
- return table, nil
+ return "", table, nil
}
}
@@ -554,16 +554,20 @@ func (extractor *sensitiveFieldExtractor) findTableSchema(databaseName string, t
if databaseName == database.Name || (databaseName == "" && extractor.currentDatabase == database.Name) {
for _, table := range database.TableList {
if tableName == table.Name {
- return table, nil
+ explicitDatabase := databaseName
+ if explicitDatabase == "" {
+ explicitDatabase = extractor.currentDatabase
+ }
+ return explicitDatabase, table, nil
}
}
}
}
- return db.TableSchema{}, errors.Errorf("Table %q.%q not found", databaseName, tableName)
+ return "", db.TableSchema{}, errors.Errorf("Table %q.%q not found", databaseName, tableName)
}
func (extractor *sensitiveFieldExtractor) extractTableName(node *tidbast.TableName) ([]fieldInfo, error) {
- tableSchema, err := extractor.findTableSchema(node.Schema.O, node.Name.O)
+ databaseName, tableSchema, err := extractor.findTableSchema(node.Schema.O, node.Name.O)
if err != nil {
return nil, err
}
@@ -573,7 +577,7 @@ func (extractor *sensitiveFieldExtractor) extractTableName(node *tidbast.TableNa
res = append(res, fieldInfo{
name: column.Name,
table: tableSchema.Name,
- database: node.Schema.O,
+ database: databaseName,
sensitive: column.Sensitive,
})
}