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: handle nil pointer collation allowlist (#9326)
zll600e8bbca433cc6
diff --git a/backend/plugin/advisor/mysqlwip/advisor_collation_allowlist.go b/backend/plugin/advisor/mysqlwip/advisor_collation_allowlist.goindex 05feb1e396..61707a33ad 100644--- a/backend/plugin/advisor/mysqlwip/advisor_collation_allowlist.go+++ b/backend/plugin/advisor/mysqlwip/advisor_collation_allowlist.go@@ -84,7 +84,7 @@ func (checker *collationAllowlistChecker) EnterQuery(ctx *mysql.QueryContext) {// EnterCreateDatabase is called when production createDatabase is entered.func (checker *collationAllowlistChecker) EnterCreateDatabase(ctx *mysql.CreateDatabaseContext) {for _, option := range ctx.AllCreateDatabaseOption() {- if option.DefaultCollation() != nil {+ if option != nil && option.DefaultCollation() != nil && option.DefaultCollation().CollationName() != nil {collation := mysqlparser.NormalizeMySQLCollationName(option.DefaultCollation().CollationName())checker.checkCollation(collation, ctx.GetStart().GetLine())}@@ -108,9 +108,8 @@ func (checker *collationAllowlistChecker) checkCollation(collation string, lineNfunc (checker *collationAllowlistChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {if ctx.CreateTableOptions() != nil {for _, option := range ctx.CreateTableOptions().AllCreateTableOption() {- if option.DefaultCollation() != nil {+ if option != nil && option.DefaultCollation() != nil && option.DefaultCollation().CollationName() != nil {collation := mysqlparser.NormalizeMySQLCollationName(option.DefaultCollation().CollationName())- collation = strings.ToLower(collation)checker.checkCollation(collation, option.GetStart().GetLine())}}@@ -118,22 +117,20 @@ func (checker *collationAllowlistChecker) EnterCreateTable(ctx *mysql.CreateTablif ctx.TableElementList() != nil {for _, tableElement := range ctx.TableElementList().AllTableElement() {- if tableElement.ColumnDefinition() != nil {- if tableElement.ColumnDefinition() == nil {- continue- }- columnDef := tableElement.ColumnDefinition()- if columnDef.FieldDefinition() == nil {- continue- }- if columnDef.FieldDefinition().AllColumnAttribute() == nil {- continue- }- for _, attr := range columnDef.FieldDefinition().AllColumnAttribute() {- if attr.Collate() != nil {- collation := mysqlparser.NormalizeMySQLCollationName(attr.Collate().CollationName())- checker.checkCollation(collation, tableElement.GetStart().GetLine())- }+ if tableElement == nil || tableElement.ColumnDefinition() == nil {+ continue+ }+ columnDef := tableElement.ColumnDefinition()+ if columnDef.FieldDefinition() == nil {+ continue+ }+ if columnDef.FieldDefinition().AllColumnAttribute() == nil {+ continue+ }+ for _, attr := range columnDef.FieldDefinition().AllColumnAttribute() {+ if attr != nil && attr.Collate() != nil && attr.Collate().CollationName() != nil {+ collation := mysqlparser.NormalizeMySQLCollationName(attr.Collate().CollationName())+ checker.checkCollation(collation, tableElement.GetStart().GetLine())}}}@@ -143,7 +140,7 @@ func (checker *collationAllowlistChecker) EnterCreateTable(ctx *mysql.CreateTabl// EnterAlterDatabase is called when production alterDatabase is entered.func (checker *collationAllowlistChecker) EnterAlterDatabase(ctx *mysql.AlterDatabaseContext) {for _, option := range ctx.AllAlterDatabaseOption() {- if option.CreateDatabaseOption() == nil || option.CreateDatabaseOption().DefaultCollation() == nil {+ if option == nil || option.CreateDatabaseOption() == nil || option.CreateDatabaseOption().DefaultCollation() == nil || option.CreateDatabaseOption().DefaultCollation().CollationName() == nil {continue}charset := mysqlparser.NormalizeMySQLCollationName(option.CreateDatabaseOption().DefaultCollation().CollationName())@@ -164,17 +161,11 @@ func (checker *collationAllowlistChecker) EnterAlterTable(ctx *mysql.AlterTableC}for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {- if item.FieldDefinition() == nil {- continue- }- if item.FieldDefinition().DataType() == nil {- continue- }- if item.FieldDefinition().DataType().CharsetWithOptBinary() == nil {+ if item == nil || item.FieldDefinition() == nil {continue}for _, attr := range item.FieldDefinition().AllColumnAttribute() {- if attr.Collate() == nil {+ if attr == nil || attr.Collate() == nil || attr.Collate().CollationName() == nil {continue}collation := mysqlparser.NormalizeMySQLCollationName(attr.Collate().CollationName())@@ -190,7 +181,7 @@ func (checker *collationAllowlistChecker) EnterAlterTable(ctx *mysql.AlterTableCif tableOption == nil {continue}- if tableOption.DefaultCollation() == nil {+ if tableOption.DefaultCollation() == nil || tableOption.DefaultCollation().CollationName() == nil {continue}collation := mysqlparser.NormalizeMySQLCollationName(tableOption.DefaultCollation().CollationName())diff --git a/backend/plugin/advisor/mysqlwip/test/system_collation_allowlist.yaml b/backend/plugin/advisor/mysqlwip/test/system_collation_allowlist.yamlindex f38ef2b24f..093a39fa5e 100644--- a/backend/plugin/advisor/mysqlwip/test/system_collation_allowlist.yaml+++ b/backend/plugin/advisor/mysqlwip/test/system_collation_allowlist.yaml@@ -1,3 +1,63 @@+- statement: |-+ CREATE TABLE test(id INT PRIMARY KEY COMMENT 'comment',name VARCHAR(255) NOT NULL DEFAULT '' COMMENT 'comment',room_id INT NOT NULL DEFAULT 0 COMMENT 'comment',creator_id INT NOT NULL DEFAULT 0 COMMENT 'comment',created_ts TIMESTAMP NOT NULL DEFAULT NOW() COMMENT 'comment',updater_id INT NOT NULL DEFAULT 0 COMMENT 'comment',updated_ts TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',INDEX idx_test_name(name),UNIQUE KEY uk_test_id_name(id, name)) ENGINE = INNODB COMMENT 'comment'+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: |+ CREATE TABLE userTable(+ id INT NOT NULL,+ name VARCHAR(255) CHARSET ascii,+ roomId INT,+ time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',+ time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',+ content BLOB NOT NULL COMMENT 'comment',+ json_content JSON NOT NULL COMMENT 'comment',+ INDEX idx1(name),+ UNIQUE KEY uk1(id, name),+ FOREIGN KEY fk1(roomId) REFERENCES room(id),+ INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin;+ want:+ - status: WARN+ code: 1201+ title: system.collation.allowlist+ content: |-+ "CREATE TABLE userTable(+ id INT NOT NULL,+ name VARCHAR(255) CHARSET ascii,+ roomId INT,+ time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',+ time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',+ content BLOB NOT NULL COMMENT 'comment',+ json_content JSON NOT NULL COMMENT 'comment',+ INDEX idx1(name),+ UNIQUE KEY uk1(id, name),+ FOREIGN KEY fk1(roomId) REFERENCES room(id),+ INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin;" used disabled collation 'latin1_bin'+ line: 12+ details: ""+- statement: |+ CREATE TABLE user(+ id INT PRIMARY KEY COMMENT 'comment',+ name VARCHAR(255) NOT NULL DEFAULT '' COMMENT 'comment',+ room_id INT NOT NULL DEFAULT 0 COMMENT 'comment',+ creator_id INT NOT NULL DEFAULT 0 COMMENT 'comment',+ created_ts TIMESTAMP NOT NULL DEFAULT NOW() COMMENT 'comment',+ updater_id INT NOT NULL DEFAULT 0 COMMENT 'comment',+ updated_ts TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',+ INDEX idx_user_name(name),+ UNIQUE KEY uk_user_id_name(id, name)+ ) ENGINE = INNODB COMMENT 'comment';+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""- statement: CREATE TABLE t(a int) COLLATE utf8mb4_polish_ci;want:- status: WARN