fix(doctor,doltserver): escape SHOW DATABASES identifiers at raw-interpolation sites (#6434) (#6638)

* fix(doctor,doltserver): escape SHOW DATABASES identifiers at raw-interpolation sites

Three call sites interpolated a SHOW DATABASES-derived name into SQL without
doubling backticks, so a database name containing a backtick could break out
of the identifier quoting:

- cmd/bd/doctor/dolt.go probeForCorrectDatabase (issues probe)
- internal/doltserver/doltserver.go flush routine (dolt_status probe and USE)

Extract each query into a small helper (issuesProbeQuery, doltStatusQuery,
useDatabaseStatement) that applies the same escape already used by
inspectServerMetadataDatabases, and unit-test them without a live Dolt server.

Fixes #6434

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(doctor): escape the bd doctor --fix probe via a shared doltutil helper

Maintainer review fixes for PR #6638 (review iterations 1 and 2).

The review's one major (Change Impact / Blast Radius) is that the PR
escapes three of four identical SHOW DATABASES interpolation sites.
probeForCorrectDoltDatabase (cmd/bd/doctor/fix/metadata.go) is a
near-line-for-line clone of the probeForCorrectDatabase this PR fixed --
same enumeration, same system-DB skip map, same testdb_/doctest_/
doctortest_ prefix filter, byte-identical query string -- and the two are
the check and fix halves of one GH#2160 wrong-database repair. So after
this PR "bd doctor" detects a backticked database and emits
"Fix: Run 'bd doctor --fix' to set dolt_database=X", while "bd doctor
--fix" reaches the still-raw twin, whose SELECT errors, yielding
correctDB == "" and a bare return nil: no fix, no error, no output. The
PR turned a consistent double failure into a repair tool that tells the
user to run a command that silently does nothing.

Take the review's preferred single remedy, which also closes the
Architectural Consistency minor: land one exported
doltutil.QuoteIdentifier and route every site through it.
internal/storage/doltutil was already imported by the affected packages
(cmd/bd/doctor, cmd/bd/doctor/fix, internal/doltserver), so only cmd/bd
gains an import edge.

- new doltutil.QuoteIdentifier, returning the backtick-quoted form so
  quoting and escaping cannot be applied separately; unit-tested for
  plain, empty, embedded/leading backtick, and backtick-only names
- route all five hand-inlined backtick-doubling copies through it: the
  three builders this PR added, the pre-existing DROP DATABASE in
  cmd/bd/dolt_clean_databases.go, and inspectServerMetadataDatabases.
  Non-test inline copies go from five to one -- the helper itself
- drop the surviving //nolint:gosec rationale "dbName from SHOW
  DATABASES, not user input" at fix/metadata.go, which documented as
  safe exactly the premise this PR exists to refute
- pin the call site, not only the builder: a sqlmock test asserts
  probeForCorrectDoltDatabase issues the escaped query. Reverting the
  escape fails it with probeForCorrectDoltDatabase() = "", the silent
  no-op described above, while the plain-name case stays green

Left raw deliberately, as both review lanes concluded: doltserver.go's
CREATE DATABASE takes the constant GlobalDatabaseName, and the raw USE in
internal/testutil takes test-controlled names.

Review iteration 2 found no blocker or major. Its one minor is that the
consolidation re-created the ambiguity it removed, one level up: the repo
already exported a QuoteIdentifier in internal/storage/domain/db with the
opposite contract -- allowlist-based, returns an error, rejects anything
outside validIdentifier -- whose docstring claimed to be "the one place
this repository's identifiers are quoted", a sentence the new helper
falsified while staking a competing near-universal claim of its own.

- rename doltutil.QuoteIdentifier to QuoteIdentifierUnvalidated, and have
  both docstrings state their input class and cross-reference the other:
  db.QuoteIdentifier for bd-minted names that must satisfy the allowlist
  before the database or table is addressed, the doltutil helper for
  server-reported names bd does not control. The escape-over-reject
  behavior does not change -- rejecting a SHOW DATABASES name would skip
  the candidate, which is the defect this PR exists to fix. The new name
  also keeps Quote, so callers do not re-wrap an already-quoted result
- scope the helper's "every identifier that is not a compile-time constant
  or already validated" rule to non-test code: internal/testutil still
  interpolates raw, on test-controlled names, and has no non-test importer
- pin dropStaleDatabases (cmd/bd/dolt_clean_databases.go), the one
  destructive statement in the set. versioncontrolops.DBConn is already a
  parameter there, so a recording fake needs no server and no sqlmock, and
  the test is tagless where the sibling file is cgo-gated. Reverting that
  call site to raw interpolation fails it with
  "DROP DATABASE `evil`; DROP TABLE x`" -- the injected statement escaping
  the identifier -- while the plain-name statement stays byte-identical

The remaining unpinned site, inspectServerMetadataDatabases, opens its own
*sql.DB inside openServerCatalogDB, so pinning it needs a production seam
rather than a test. It was already correctly escaped before this PR and is
unchanged by it; left as reviewer-noted follow-up, as is nolintlint, which
.golangci.yml still disables.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
This commit is contained in:
Vishnu J 2026-09-26 14:26:04 -07:00 • committed by GitHub
commit 29bd4eeacd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 285 additions and 18 deletions

View file

@ -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))
}

View file

@ -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)
}
})
}
}

View file

@ -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) {

View file

@ -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)
}
})
}
}

View file

@ -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)

View file

@ -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)
}
}
}

View file

@ -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))
}

View file

@ -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)
}
})
}
}

View file

@ -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, "`", "``") + "`"
}

View file

@ -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)
}
})
}
}

View file

@ -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