diff --git a/cmd/bd/doctor/dolt.go b/cmd/bd/doctor/dolt.go index 159cc7563..529e23929 100644 --- a/cmd/bd/doctor/dolt.go +++ b/cmd/bd/doctor/dolt.go @@ -705,9 +705,7 @@ func probeForCorrectDatabase(conn *doltConn) string { for _, dbName := range candidates { var count int // USE + query to check if the database has the issues table - //nolint:gosec // G201: dbName is from SHOW DATABASES, not user input - err := conn.db.QueryRowContext(ctx, - fmt.Sprintf("SELECT COUNT(*) FROM `%s`.issues LIMIT 1", dbName)).Scan(&count) + err := conn.db.QueryRowContext(ctx, issuesProbeQuery(dbName)).Scan(&count) if err == nil { return dbName } @@ -820,3 +818,11 @@ func CheckCorruptManifest(path string) DoctorCheck { Category: CategoryRuntime, } } + +// issuesProbeQuery builds the issues-table probe for a database name taken from +// SHOW DATABASES. The name is identifier-quoted because it reflects whatever +// created the database, not a bd-controlled value. +func issuesProbeQuery(dbName string) string { + //nolint:gosec // G201: identifier quoted+escaped via doltutil.QuoteIdentifierUnvalidated + return fmt.Sprintf("SELECT COUNT(*) FROM %s.issues LIMIT 1", doltutil.QuoteIdentifierUnvalidated(dbName)) +} diff --git a/cmd/bd/doctor/dolt_test.go b/cmd/bd/doctor/dolt_test.go index 4551c9aed..ee113e7c8 100644 --- a/cmd/bd/doctor/dolt_test.go +++ b/cmd/bd/doctor/dolt_test.go @@ -294,3 +294,21 @@ func TestDoltLocksAndDoltStatusShareOneFilter(t *testing.T) { } } } + +func TestIssuesProbeQuery(t *testing.T) { + tests := []struct { + name string + dbName string + want string + }{ + {"plain", "beads_x", "SELECT COUNT(*) FROM `beads_x`.issues LIMIT 1"}, + {"backtick", "evil`; DROP TABLE x", "SELECT COUNT(*) FROM `evil``; DROP TABLE x`.issues LIMIT 1"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := issuesProbeQuery(tt.dbName); got != tt.want { + t.Errorf("issuesProbeQuery(%q) = %q, want %q", tt.dbName, got, tt.want) + } + }) + } +} diff --git a/cmd/bd/doctor/fix/metadata.go b/cmd/bd/doctor/fix/metadata.go index 454b5dfe9..07281e5d0 100644 --- a/cmd/bd/doctor/fix/metadata.go +++ b/cmd/bd/doctor/fix/metadata.go @@ -281,9 +281,9 @@ func probeForCorrectDoltDatabase(db *sql.DB, skipDB string) string { for _, dbName := range candidates { var count int - //nolint:gosec // G201: dbName from SHOW DATABASES, not user input + //nolint:gosec // G201: identifier quoted+escaped via doltutil.QuoteIdentifierUnvalidated err := db.QueryRowContext(ctx, - fmt.Sprintf("SELECT COUNT(*) FROM `%s`.issues LIMIT 1", dbName)).Scan(&count) + fmt.Sprintf("SELECT COUNT(*) FROM %s.issues LIMIT 1", doltutil.QuoteIdentifierUnvalidated(dbName))).Scan(&count) if err == nil { return dbName } @@ -445,21 +445,20 @@ func inspectServerMetadataDatabases(beadsDir string, cfg *configfile.Config) ([] } meta := serverDatabaseMetadata{Name: dbName} - // Escape backticks in database name to prevent SQL injection (` → ``) - safeName := strings.ReplaceAll(dbName, "`", "``") + quotedName := doltutil.QuoteIdentifierUnvalidated(dbName) var count int - //nolint:gosec // G201: identifier-escaped, dbName from SHOW DATABASES - if err := db.QueryRowContext(ctx, fmt.Sprintf("SELECT COUNT(*) FROM `%s`.issues LIMIT 1", safeName)).Scan(&count); err == nil { + //nolint:gosec // G201: identifier quoted+escaped via doltutil.QuoteIdentifierUnvalidated + if err := db.QueryRowContext(ctx, fmt.Sprintf("SELECT COUNT(*) FROM %s.issues LIMIT 1", quotedName)).Scan(&count); err == nil { meta.HasSchema = true } else if !isExpectedProbeError(err) { return nil, fmt.Errorf("probing database %q for schema: %w", dbName, err) } var projectID string - //nolint:gosec // G201: identifier-escaped, dbName from SHOW DATABASES + //nolint:gosec // G201: identifier quoted+escaped via doltutil.QuoteIdentifierUnvalidated if err := db.QueryRowContext(ctx, - fmt.Sprintf("SELECT value FROM `%s`.metadata WHERE `key` = '_project_id' LIMIT 1", safeName), + fmt.Sprintf("SELECT value FROM %s.metadata WHERE `key` = '_project_id' LIMIT 1", quotedName), ).Scan(&projectID); err == nil { meta.ProjectID = projectID } else if !isExpectedProbeError(err) { diff --git a/cmd/bd/doctor/fix/metadata_probe_test.go b/cmd/bd/doctor/fix/metadata_probe_test.go new file mode 100644 index 000000000..ac17335c5 --- /dev/null +++ b/cmd/bd/doctor/fix/metadata_probe_test.go @@ -0,0 +1,58 @@ +package fix + +import ( + "regexp" + "testing" + + "github.com/DATA-DOG/go-sqlmock" +) + +// TestProbeForCorrectDoltDatabaseQuotesIdentifier pins the call site, not just +// the escape helper. probeForCorrectDoltDatabase is the `bd doctor --fix` half +// of the GH#2160 wrong-database repair; its twin in package doctor +// (probeForCorrectDatabase) emits the "run bd doctor --fix" instruction. While +// this probe interpolated the SHOW DATABASES name raw, a backtick-bearing name +// made the SELECT error, so the repair returned "" and did nothing at all — +// no fix, no error, no output — for exactly the names the check had just told +// the user to repair. Expectations are exact strings, so reverting to raw +// interpolation fails this test rather than silently regressing. +func TestProbeForCorrectDoltDatabaseQuotesIdentifier(t *testing.T) { + tests := []struct { + name string + dbName string + wantQuery string + }{ + { + name: "plain name is unchanged", + dbName: "beads_x", + wantQuery: "SELECT COUNT(*) FROM `beads_x`.issues LIMIT 1", + }, + { + name: "backtick cannot break out of the identifier", + dbName: "evil`; DROP TABLE x", + wantQuery: "SELECT COUNT(*) FROM `evil``; DROP TABLE x`.issues LIMIT 1", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + db, mock, err := sqlmock.New() + if err != nil { + t.Fatalf("sqlmock.New: %v", err) + } + defer db.Close() + + mock.ExpectQuery("SHOW DATABASES"). + WillReturnRows(sqlmock.NewRows([]string{"Database"}).AddRow(tt.dbName)) + mock.ExpectQuery(regexp.QuoteMeta(tt.wantQuery)). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) + + if got := probeForCorrectDoltDatabase(db, "beads"); got != tt.dbName { + t.Errorf("probeForCorrectDoltDatabase() = %q, want %q", got, tt.dbName) + } + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unmet expectations: %v", err) + } + }) + } +} diff --git a/cmd/bd/dolt_clean_databases.go b/cmd/bd/dolt_clean_databases.go index ecb1d51ed..b8911d84d 100644 --- a/cmd/bd/dolt_clean_databases.go +++ b/cmd/bd/dolt_clean_databases.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/steveyegge/beads/internal/storage/doltutil" "github.com/steveyegge/beads/internal/storage/versioncontrolops" ) @@ -126,8 +127,8 @@ func dropStaleDatabases(ctx context.Context, conn versioncontrolops.DBConn, stal } dropCtx, dropCancel := context.WithTimeout(ctx, cleanDatabasesDropTimeout) - safeName := strings.ReplaceAll(name, "`", "``") - _, err := conn.ExecContext(dropCtx, fmt.Sprintf("DROP DATABASE `%s`", safeName)) //nolint:gosec // G201: identifier-escaped + //nolint:gosec // G201: identifier quoted+escaped via doltutil.QuoteIdentifierUnvalidated + _, err := conn.ExecContext(dropCtx, fmt.Sprintf("DROP DATABASE %s", doltutil.QuoteIdentifierUnvalidated(name))) dropCancel() if err != nil { fmt.Fprintf(os.Stderr, " FAIL: %s: %v\n", name, err) diff --git a/cmd/bd/dolt_clean_databases_quote_test.go b/cmd/bd/dolt_clean_databases_quote_test.go new file mode 100644 index 000000000..94cf63940 --- /dev/null +++ b/cmd/bd/dolt_clean_databases_quote_test.go @@ -0,0 +1,71 @@ +package main + +import ( + "context" + "database/sql" + "testing" + + "github.com/steveyegge/beads/internal/storage/versioncontrolops" +) + +// recordingDropConn records the statements dropStaleDatabases issues. Only +// ExecContext is reachable from that function; the other two methods exist to +// satisfy versioncontrolops.DBConn and fail loudly if that ever changes. +type recordingDropConn struct { + t *testing.T + queries []string +} + +var _ versioncontrolops.DBConn = (*recordingDropConn)(nil) + +func (c *recordingDropConn) ExecContext(_ context.Context, query string, _ ...any) (sql.Result, error) { + c.queries = append(c.queries, query) + return nil, nil +} + +func (c *recordingDropConn) QueryContext(_ context.Context, query string, _ ...any) (*sql.Rows, error) { + c.t.Fatalf("unexpected QueryContext(%q)", query) + return nil, nil +} + +func (c *recordingDropConn) QueryRowContext(_ context.Context, query string, _ ...any) *sql.Row { + c.t.Fatalf("unexpected QueryRowContext(%q)", query) + return nil +} + +// TestDropStaleDatabasesQuotesIdentifiers pins the one destructive statement in +// the SHOW DATABASES set to the escaping helper. `bd dolt clean-databases` +// enumerates server database names and DROPs the stale ones, so the name it +// interpolates is whatever created the database, never a bd-minted value. The +// PR this test ships with fixed a sibling probe that had been left on raw +// interpolation precisely because nothing tied its call site to a builder; +// DROP DATABASE is where that mistake would cost the most, and the seam is +// free — dropStaleDatabases already takes versioncontrolops.DBConn as a +// parameter, so no server and no sqlmock are needed. Expectations are exact +// strings, so reverting the call site to raw `%s` interpolation fails the +// backtick case here rather than silently regressing. +func TestDropStaleDatabasesQuotesIdentifiers(t *testing.T) { + conn := &recordingDropConn{t: t} + stale := []string{"beads_x", "evil`; DROP TABLE x"} + + dropped, err := dropStaleDatabases(t.Context(), conn, stale) + if err != nil { + t.Fatalf("dropStaleDatabases: %v", err) + } + if dropped != len(stale) { + t.Fatalf("dropped = %d, want %d", dropped, len(stale)) + } + + want := []string{ + "DROP DATABASE `beads_x`", + "DROP DATABASE `evil``; DROP TABLE x`", + } + if len(conn.queries) != len(want) { + t.Fatalf("issued %d statements (%q), want %d", len(conn.queries), conn.queries, len(want)) + } + for i, wantQuery := range want { + if conn.queries[i] != wantQuery { + t.Errorf("statement %d = %q, want %q", i, conn.queries[i], wantQuery) + } + } +} diff --git a/internal/doltserver/doltserver.go b/internal/doltserver/doltserver.go index 4be44120a..1accffec5 100644 --- a/internal/doltserver/doltserver.go +++ b/internal/doltserver/doltserver.go @@ -1703,7 +1703,7 @@ func FlushWorkingSet(host string, port int) error { for _, dbName := range databases { // Check for uncommitted changes via dolt_status var hasChanges bool - row := db.QueryRowContext(ctx, fmt.Sprintf("SELECT COUNT(*) > 0 FROM `%s`.dolt_status", dbName)) + row := db.QueryRowContext(ctx, doltStatusQuery(dbName)) if err := row.Scan(&hasChanges); err != nil { // dolt_status may not exist for non-beads databases; skip continue @@ -1713,7 +1713,7 @@ func FlushWorkingSet(host string, port int) error { } // Commit all uncommitted changes - _, err := db.ExecContext(ctx, fmt.Sprintf("USE `%s`", dbName)) + _, err := db.ExecContext(ctx, useDatabaseStatement(dbName)) if err != nil { fmt.Fprintf(os.Stderr, "flush: failed to USE %s: %v\n", dbName, err) continue @@ -2097,3 +2097,17 @@ func IsPreV56DoltDir(doltDir string) bool { _, err := os.Stat(markerPath) return os.IsNotExist(err) } + +// doltStatusQuery builds the dolt_status probe for a SHOW DATABASES name, +// identifier-quoting it so the name cannot break out of the identifier. +func doltStatusQuery(dbName string) string { + //nolint:gosec // G201: identifier quoted+escaped via doltutil.QuoteIdentifierUnvalidated + return fmt.Sprintf("SELECT COUNT(*) > 0 FROM %s.dolt_status", doltutil.QuoteIdentifierUnvalidated(dbName)) +} + +// useDatabaseStatement builds a USE statement for a SHOW DATABASES name, +// identifier-quoting it so the name cannot break out of the identifier. +func useDatabaseStatement(dbName string) string { + //nolint:gosec // G201: identifier quoted+escaped via doltutil.QuoteIdentifierUnvalidated + return fmt.Sprintf("USE %s", doltutil.QuoteIdentifierUnvalidated(dbName)) +} diff --git a/internal/doltserver/doltserver_test.go b/internal/doltserver/doltserver_test.go index e04d85c14..8ae7cd2ee 100644 --- a/internal/doltserver/doltserver_test.go +++ b/internal/doltserver/doltserver_test.go @@ -2666,3 +2666,39 @@ func TestExternalNonLocalhostHost_GH3518(t *testing.T) { } }) } + +func TestDoltStatusQuery(t *testing.T) { + tests := []struct { + name string + dbName string + want string + }{ + {"plain", "beads_x", "SELECT COUNT(*) > 0 FROM `beads_x`.dolt_status"}, + {"backtick", "evil`; DROP TABLE x", "SELECT COUNT(*) > 0 FROM `evil``; DROP TABLE x`.dolt_status"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := doltStatusQuery(tt.dbName); got != tt.want { + t.Errorf("doltStatusQuery(%q) = %q, want %q", tt.dbName, got, tt.want) + } + }) + } +} + +func TestUseDatabaseStatement(t *testing.T) { + tests := []struct { + name string + dbName string + want string + }{ + {"plain", "beads_x", "USE `beads_x`"}, + {"backtick", "evil`; DROP TABLE x", "USE `evil``; DROP TABLE x`"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := useDatabaseStatement(tt.dbName); got != tt.want { + t.Errorf("useDatabaseStatement(%q) = %q, want %q", tt.dbName, got, tt.want) + } + }) + } +} diff --git a/internal/storage/doltutil/identifier.go b/internal/storage/doltutil/identifier.go new file mode 100644 index 000000000..78ddac232 --- /dev/null +++ b/internal/storage/doltutil/identifier.go @@ -0,0 +1,33 @@ +package doltutil + +import "strings" + +// QuoteIdentifierUnvalidated renders name as a backtick-quoted MySQL/Dolt +// identifier, doubling any embedded backtick so the name cannot break out of +// the quotes. Doubling is the complete escape inside a backtick-quoted +// identifier: no other character is special there, and identifiers cannot +// contain NUL. +// +// Use it in non-test code for every identifier that is not a compile-time +// constant or already validated. Database names returned by SHOW DATABASES are +// the motivating case: they reflect whatever created the database (typically an +// on-disk directory name), not a bd-controlled value, so interpolating one raw +// lets it terminate the identifier and append arbitrary SQL. +// +// Unvalidated names the contract, not a caveat: this helper accepts every +// input, which is what names bd does not control require. For a bd-minted name +// that must satisfy the identifier allowlist before the database or table is +// addressed, use the validating db.QuoteIdentifier +// (internal/storage/domain/db) instead. The two are not interchangeable: +// rejecting a SHOW DATABASES name would skip the candidate rather than address +// it, and escaping a bd-minted name would let an invalid one through. +// +// The result is already quoted; interpolate it with a bare %s rather than +// wrapping it in backticks again. +// +// Callers still need a `//nolint:gosec` on the surrounding fmt.Sprintf: G201 +// fires on any non-constant format argument and cannot see that this function +// makes the interpolation safe. +func QuoteIdentifierUnvalidated(name string) string { + return "`" + strings.ReplaceAll(name, "`", "``") + "`" +} diff --git a/internal/storage/doltutil/identifier_test.go b/internal/storage/doltutil/identifier_test.go new file mode 100644 index 000000000..3c39d2a68 --- /dev/null +++ b/internal/storage/doltutil/identifier_test.go @@ -0,0 +1,25 @@ +package doltutil + +import "testing" + +func TestQuoteIdentifierUnvalidated(t *testing.T) { + tests := []struct { + name string + in string + want string + }{ + {"plain", "beads_x", "`beads_x`"}, + {"empty", "", "``"}, + {"backtick breaks out", "evil`; DROP TABLE x", "`evil``; DROP TABLE x`"}, + {"leading backtick", "`beads", "```beads`"}, + {"only backtick", "`", "````"}, + {"dot is not special", "a.b", "`a.b`"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := QuoteIdentifierUnvalidated(tt.in); got != tt.want { + t.Errorf("QuoteIdentifierUnvalidated(%q) = %q, want %q", tt.in, got, tt.want) + } + }) + } +} diff --git a/internal/storage/domain/db/ddl.go b/internal/storage/domain/db/ddl.go index babdeb608..f6ec52223 100644 --- a/internal/storage/domain/db/ddl.go +++ b/internal/storage/domain/db/ddl.go @@ -77,10 +77,16 @@ func (r *ddlSQLRepository) UseDatabase(ctx context.Context, database string) err } // QuoteIdentifier validates name and returns it back-quoted, ready to embed in -// a statement. It is the one place this repository's identifiers are quoted; -// callers outside the package that must build a schema-qualified name (see -// uow's convergence-probe database selector) go through it rather than +// a statement. It is the one place this repository's bd-minted identifiers are +// quoted; callers outside the package that must build a schema-qualified name +// (see uow's convergence-probe database selector) go through it rather than // re-deriving the rule. +// +// It rejects rather than escapes, so it is the wrong helper for an identifier +// bd does not control: a name reported by SHOW DATABASES reflects whatever +// created the database, and refusing it would skip the candidate instead of +// addressing it. Those callers use doltutil.QuoteIdentifierUnvalidated +// (internal/storage/doltutil), which escapes any input. func QuoteIdentifier(name string) (string, error) { if err := ValidateIdentifier(name); err != nil { return "", err