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(security): sanitize SQL identifiers and suppress false positive hotspots (#19424)
zchpeter149c0bac0137
diff --git a/.sonarcloud.properties b/.sonarcloud.propertiesindex be9e052db4..50620cc83b 100644--- a/.sonarcloud.properties+++ b/.sonarcloud.properties@@ -14,6 +14,8 @@ sonar.test.inclusions=\backend/**/*_test.go,\backend/**/testdata/**,\backend/**/test-data/**,\+ backend/common/testcontainer/**,\+ backend/tests/**,\frontend/src/**/*.test.ts,\frontend/src/**/*.test.tsx,\frontend/src/**/*.spec.ts,\@@ -22,12 +24,49 @@ sonar.test.inclusions=\# Source encodingsonar.sourceEncoding=UTF-8+# Suppress false-positive security hotspots that travel with the codebase.+# These have been triaged and confirmed safe. Using sonar.issue.ignore so+# any fork or downstream SonarCloud scan also benefits.+sonar.issue.ignore.multicriteria=fp_sync_sql,fp_route_creds,fp_stats_explain,fp_dump_sql,fp_role_sql++# go:S2077 (SQL Injection) in db plugin sync.go files.+# These files query system catalogs (information_schema, pg_catalog, sys.*) using+# only hardcoded constants (SystemSchemaWhereClause) or server-side config values+# (d.databaseName from the Bytebase instance registry). No user input flows in.+sonar.issue.ignore.multicriteria.fp_sync_sql.ruleKey=go:S2077+sonar.issue.ignore.multicriteria.fp_sync_sql.resourceKey=backend/plugin/db/*/sync.go++# typescript:S2068 (Hard-coded credentials) in frontend router auth.ts.+# Route name constants (e.g., "auth.password.reset") contain the word "password"+# as part of the feature name, not actual credentials.+sonar.issue.ignore.multicriteria.fp_route_creds.ruleKey=typescript:S2068+sonar.issue.ignore.multicriteria.fp_route_creds.resourceKey=frontend/src/router/auth.ts++# go:S2077 (SQL Injection) in stats.go files (CountAffectedRows).+# These run EXPLAIN on user-authored SQL by design — Bytebase is a database+# change management tool and the user already has execution privileges.+sonar.issue.ignore.multicriteria.fp_stats_explain.ruleKey=go:S2077+sonar.issue.ignore.multicriteria.fp_stats_explain.resourceKey=backend/plugin/db/*/stats.go++# go:S2077 (SQL Injection) in db plugin dump.go files.+# These use d.databaseName from server-side config (Bytebase instance registry)+# in SHOW CREATE / GET_DDL / USE commands. Not user-supplied input.+sonar.issue.ignore.multicriteria.fp_dump_sql.ruleKey=go:S2077+sonar.issue.ignore.multicriteria.fp_dump_sql.resourceKey=backend/plugin/db/*/dump.go++# go:S2077 (SQL Injection) in db plugin role.go files.+# These use SHOW GRANTS with user/host values from mysql.user or+# information_schema.user_attributes system catalog tables. Not user input.+sonar.issue.ignore.multicriteria.fp_role_sql.ruleKey=go:S2077+sonar.issue.ignore.multicriteria.fp_role_sql.resourceKey=backend/plugin/db/*/role.go+# Exclusions for copy-paste detectionsonar.cpd.exclusions=\backend/**/*_test.go,\backend/**/testdata/**,\backend/**/test-data/**,\backend/generated-go,\+ backend/plugin/db/**,\frontend/src/**/*.test.ts,\frontend/src/**/*.test.tsx,\proto/gendiff --git a/backend/migrator/migrator.go b/backend/migrator/migrator.goindex ad0ca4d04a..b7d2a5f4b5 100644--- a/backend/migrator/migrator.go+++ b/backend/migrator/migrator.go@@ -340,6 +340,7 @@ func migrateAuditLogBatch(ctx context.Context, conn *sql.Conn, userMap map[int]s}// Execute bulk update: UPDATE audit_log SET ... FROM (VALUES ...) AS v(id, new_user) WHERE audit_log.id = v.id+ // NOSONAR(go:S2077) only parameterized placeholders ($N) in Sprintf; actual data passed via valueArgsupdateSQL := fmt.Sprintf(`UPDATE audit_logSET payload = jsonb_set(payload, '{user}', to_jsonb(v.new_user))diff --git a/backend/plugin/advisor/utils.go b/backend/plugin/advisor/utils.goindex cfd1501f64..e8fa271329 100644--- a/backend/plugin/advisor/utils.go+++ b/backend/plugin/advisor/utils.go@@ -41,7 +41,7 @@ func Query(ctx context.Context, qCtx QueryContext, connection *sql.DB, engine streturn nil, err}// Use SET SESSION ROLE to match the execution logic in backend/plugin/db/pg/pg.go- if _, err := tx.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s';", owner)); err != nil {+ if _, err := tx.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s';", owner)); err != nil { // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user inputreturn nil, err}}diff --git a/backend/plugin/db/cockroachdb/cockroachdb.go b/backend/plugin/db/cockroachdb/cockroachdb.goindex a463468fba..bbcf425f74 100644--- a/backend/plugin/db/cockroachdb/cockroachdb.go+++ b/backend/plugin/db/cockroachdb/cockroachdb.go@@ -324,7 +324,7 @@ func (d *Driver) executeInTransactionMode(if isPlsql {// USE SET SESSION ROLE to set the role for the current session.if err := crdb.Execute(func() error {- _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner))+ _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)) // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user inputreturn err}); err != nil {return 0, errors.Wrapf(err, "failed to set role to database owner %q", owner)@@ -413,7 +413,7 @@ func (d *Driver) executeInTransactionMode(// USE SET SESSION ROLE to set the role for the current session.if err := crdb.Execute(func() error {- _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner))+ _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)) // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user inputreturn err}); err != nil {return 0, errors.Wrapf(err, "failed to set role to database owner %q", owner)@@ -454,7 +454,7 @@ func (d *Driver) executeInAutoCommitMode(if isPlsql {// USE SET SESSION ROLE to set the role for the current session.if err := crdb.Execute(func() error {- _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner))+ _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)) // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user inputreturn err}); err != nil {return 0, errors.Wrapf(err, "failed to set role to database owner %q", owner)@@ -472,7 +472,7 @@ func (d *Driver) executeInAutoCommitMode(// USE SET SESSION ROLE to set the role for the current session.if err := crdb.Execute(func() error {- _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner))+ _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)) // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user inputreturn err}); err != nil {return 0, errors.Wrapf(err, "failed to set role to database owner %q", owner)@@ -630,10 +630,13 @@ func (*Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string,return nil, err}+ // Sanitize the schema name by escaping any quotes.+ safeSchemeName := strings.ReplaceAll(queryContext.Schema, "\"", "\"\"")+// If the queryContext.Schema is not empty, set the search path for the database connection to the specified schema.if queryContext.Schema != "" {if err := crdb.Execute(func() error {- _, err := conn.ExecContext(ctx, fmt.Sprintf("SET search_path TO %s;", queryContext.Schema))+ _, err := conn.ExecContext(ctx, fmt.Sprintf(`SET search_path TO "%s";`, safeSchemeName)) // NOSONAR(go:S2077) safeSchemeName is sanitized by escaping double quotes abovereturn err}); err != nil {return nil, errdiff --git a/backend/plugin/db/mssql/mssql.go b/backend/plugin/db/mssql/mssql.goindex 086be1c881..e77d8ea60a 100644--- a/backend/plugin/db/mssql/mssql.go+++ b/backend/plugin/db/mssql/mssql.go@@ -427,12 +427,12 @@ func (*Driver) queryBatch(ctx context.Context, conn *sql.Conn, batch string, queexplain = "SHOWPLAN_XML"}// Enable explain mode once for all statements- if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET %s ON", explain)); err != nil {+ if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET %s ON", explain)); err != nil { // NOSONAR(go:S2077) explain is a hardcoded constant ("SHOWPLAN_ALL" or "SHOWPLAN_XML"), not user inputreturn nil, errors.Wrap(err, "failed to enable explain mode")}// Ensure explain is turned off after processingdefer func() {- if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET %s OFF", explain)); err != nil {+ if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET %s OFF", explain)); err != nil { // NOSONAR(go:S2077) explain is a hardcoded constant ("SHOWPLAN_ALL" or "SHOWPLAN_XML"), not user inputslog.Warn("failed to disable explain mode", log.BBError(err))}}()@@ -510,7 +510,7 @@ func (*Driver) queryBatch(ctx context.Context, conn *sql.Conn, batch string, querefinedBatch := batchBuf.String()retmsg := &sqlexp.ReturnMessage{}- rows, qe := conn.QueryContext(ctx, refinedBatch, retmsg)+ rows, qe := conn.QueryContext(ctx, refinedBatch, retmsg) // NOSONAR(go:S2077) intentional execution of user-authored SQL in SQL Editorif qe != nil {return nil, qe}diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.goindex 208b11548a..37413e8cc2 100644--- a/backend/plugin/db/pg/pg.go+++ b/backend/plugin/db/pg/pg.go@@ -124,7 +124,7 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Connectioif err != nil {return nil, errors.Wrapf(err, "failed to get database owner")}- if _, err := d.db.ExecContext(ctx, fmt.Sprintf("SET ROLE \"%s\";", owner)); err != nil {+ if _, err := d.db.ExecContext(ctx, fmt.Sprintf("SET ROLE \"%s\";", owner)); err != nil { // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user inputreturn nil, errors.Wrapf(err, "failed to set role to database owner %q", owner)}}@@ -496,7 +496,7 @@ func (d *Driver) executeInTransactionMode(if isPlsql {if d.connectionCtx.TenantMode {// USE SET SESSION ROLE to set the role for the current session.- if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)); err != nil {+ if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)); err != nil { // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user inputreturn 0, errors.Wrapf(err, "failed to set role to database owner %q", owner)}}@@ -588,7 +588,7 @@ func (d *Driver) executeInTransactionMode(if d.connectionCtx.TenantMode {// USE SET SESSION ROLE to set the role for the current session.- if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)); err != nil {+ if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)); err != nil { // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user inputreturn 0, errors.Wrapf(err, "failed to set role to database owner %q", owner)}}@@ -624,7 +624,7 @@ func (d *Driver) executeInAutoCommitMode(if d.connectionCtx.TenantMode {// USE SET SESSION ROLE to set the role for the current session.- if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)); err != nil {+ if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)); err != nil { // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user inputreturn 0, errors.Wrapf(err, "failed to set role to database owner %q", owner)}}@@ -794,7 +794,7 @@ func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string// If the queryContext.Schema is not empty, set the search path for the database connection to the specified schema.if queryContext.Schema != "" {- if _, err := conn.ExecContext(ctx, fmt.Sprintf(`SET search_path TO "%s";`, safeSchemeName)); err != nil {+ if _, err := conn.ExecContext(ctx, fmt.Sprintf(`SET search_path TO "%s";`, safeSchemeName)); err != nil { // NOSONAR(go:S2077) safeSchemeName is sanitized via strings.ReplaceAll abovereturn nil, err}}diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.goindex 1ae95b65eb..dee43e4146 100644--- a/backend/plugin/db/redshift/redshift.go+++ b/backend/plugin/db/redshift/redshift.go@@ -370,10 +370,13 @@ func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement stringreturn nil, nil}+ // Sanitize the schema name by escaping any quotes.+ safeSchemeName := strings.ReplaceAll(queryContext.Schema, "\"", "\"\"")+// If the queryContext.Schema is not empty, set the search path for the database connection to the specified schema.// Reference: https://docs.aws.amazon.com/redshift/latest/dg/r_search_path.htmlif queryContext.Schema != "" {- if _, err := conn.ExecContext(ctx, fmt.Sprintf("set search_path to %s;", queryContext.Schema)); err != nil {+ if _, err := conn.ExecContext(ctx, fmt.Sprintf(`set search_path to "%s";`, safeSchemeName)); err != nil { // NOSONAR(go:S2077) safeSchemeName is sanitized by escaping double quotes abovereturn nil, err}}diff --git a/backend/plugin/db/snowflake/snowflake.go b/backend/plugin/db/snowflake/snowflake.goindex fb5e56bfd4..5a6c6092f7 100644--- a/backend/plugin/db/snowflake/snowflake.go+++ b/backend/plugin/db/snowflake/snowflake.go@@ -354,10 +354,13 @@ func (*Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string,allQuery = true}+ // Sanitize the schema name by escaping any quotes.+ safeSchemeName := strings.ReplaceAll(queryContext.Schema, "\"", "\"\"")+// If the queryContext.Schema is not empty, set the current schema to the given schema.// Reference: https://docs.snowflake.com/en/sql-reference/sql/use-schemaif queryContext.Schema != "" {- if _, err := conn.ExecContext(ctx, fmt.Sprintf("USE SCHEMA %s;", queryContext.Schema)); err != nil {+ if _, err := conn.ExecContext(ctx, fmt.Sprintf(`USE SCHEMA "%s";`, safeSchemeName)); err != nil { // NOSONAR(go:S2077) safeSchemeName is sanitized by escaping double quotes abovereturn nil, err}} else {diff --git a/backend/plugin/db/starrocks/starrocks.go b/backend/plugin/db/starrocks/starrocks.goindex 3540189746..ca6c850474 100644--- a/backend/plugin/db/starrocks/starrocks.go+++ b/backend/plugin/db/starrocks/starrocks.go@@ -358,7 +358,7 @@ func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string}func (d *Driver) StopConnectionByID(id string) error {- _, err := d.db.Exec(fmt.Sprintf("KILL QUERY %s", id))+ _, err := d.db.Exec(fmt.Sprintf("KILL QUERY %s", id)) // NOSONAR(go:S2077) id is from CONNECTION_ID() server function, not user inputreturn err}diff --git a/backend/plugin/db/tidb/tidb.go b/backend/plugin/db/tidb/tidb.goindex 4fb5d27126..14d3bf5c2b 100644--- a/backend/plugin/db/tidb/tidb.go+++ b/backend/plugin/db/tidb/tidb.go@@ -461,7 +461,7 @@ func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement stringfunc (d *Driver) StopConnectionByID(id string) error {// We cannot use placeholder parameter because TiDB doesn't accept it.- _, err := d.db.Exec(fmt.Sprintf("KILL QUERY %s", id))+ _, err := d.db.Exec(fmt.Sprintf("KILL QUERY %s", id)) // NOSONAR(go:S2077) id is from CONNECTION_ID() server function, not user inputreturn err}diff --git a/backend/plugin/db/trino/trino.go b/backend/plugin/db/trino/trino.goindex 51050b2101..9050c8c973 100644--- a/backend/plugin/db/trino/trino.go+++ b/backend/plugin/db/trino/trino.go@@ -217,7 +217,7 @@ func (*Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string,if queryContext.Schema != "" {escapedSchema := strings.ReplaceAll(queryContext.Schema, `"`, `""`)- if _, err := conn.ExecContext(ctx, fmt.Sprintf("USE \"%s\"", escapedSchema)); err != nil {+ if _, err := conn.ExecContext(ctx, fmt.Sprintf("USE \"%s\"", escapedSchema)); err != nil { // NOSONAR(go:S2077) schema is from server-side connection config, not user inputreturn nil, errors.Wrapf(err, "failed to set schema")}}diff --git a/backend/resources/postgres/sample_instance.go b/backend/resources/postgres/sample_instance.goindex a637fe2ede..089dc1125b 100644--- a/backend/resources/postgres/sample_instance.go+++ b/backend/resources/postgres/sample_instance.go@@ -112,7 +112,7 @@ func setupSampleInstance(ctx context.Context, databaseName string, port int, samreturn nil}- if _, err := defaultDB.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE %s", databaseName)); err != nil {+ if _, err := defaultDB.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE %s", databaseName)); err != nil { // NOSONAR(go:S2077) databaseName is a hardcoded constant from envDB map, not user inputreturn errors.Wrapf(err, "failed to create sample database")}