From 4af8d5e9efe4ac09c94983a65c44b4f206c30368 Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Tue, 6 Oct 2026 23:16:16 +0000 Subject: [PATCH] fix(dolt): skip the no-op DOLT_CHECKOUT on fresh connections Two fresh-connection paths ran CALL DOLT_CHECKOUT unconditionally: beginTxOnConn (the fallback path for the ignored-tables tx) and pinStoreBranch (used by the long-timeout versioned reads and the blocked recompute). A fresh session is normally already on the requested branch, so the checkout is a no-op. A least-privilege Dolt user may be granted EXECUTE on dolt_add/dolt_commit alone, and it is denied even a no-op checkout. For such a user: bd create --graph / bd mol wisp -> failed to checkout ignored tx branch main: Error 1105 (HY000): command denied to user ... bd history, bd recompute-blocked -> checkout active branch "main": ... command denied Both sites now share checkoutBranchIfNeeded, which reads active_branch() and checks out only when the session is on a different branch, matching what beginBorrowedTx already does. A real switch still happens when one is needed. pinStoreBranch now documents that conn must be a single session. Tests: mock-driver tests cover the skip, a real switch, and a failed branch read. Two real-server tests run as a least-privilege user (SELECT on the database, DML on the data tables, EXECUTE on dolt_add/dolt_commit only, pool pinned to one connection). They cover a wisp write through the fallback, RecomputeAllBlocked and History; each fails with the denial before the fix. Co-Authored-By: Claude Opus 5.5 (1M context) --- internal/storage/dolt/BUILD.bazel | 1 + internal/storage/dolt/connection_pool_test.go | 4 +- internal/storage/dolt/store.go | 27 ++- internal/storage/dolt/transaction.go | 15 +- .../dolt/transaction_ignored_checkout_test.go | 228 ++++++++++++++++++ 5 files changed, 265 insertions(+), 10 deletions(-) create mode 100644 internal/storage/dolt/transaction_ignored_checkout_test.go diff --git a/internal/storage/dolt/BUILD.bazel b/internal/storage/dolt/BUILD.bazel index 7263ec7bf..9f3aa9e52 100644 --- a/internal/storage/dolt/BUILD.bazel +++ b/internal/storage/dolt/BUILD.bazel @@ -347,6 +347,7 @@ go_test( "testmain_helper_sentinel_test.go", "testmain_test.go", "transaction_borrow_test.go", + "transaction_ignored_checkout_test.go", "transaction_phase_test.go", "transaction_test.go", "transaction_wisp_lifecycle_persistence_test.go", diff --git a/internal/storage/dolt/connection_pool_test.go b/internal/storage/dolt/connection_pool_test.go index 31eb8fa62..091f5179d 100644 --- a/internal/storage/dolt/connection_pool_test.go +++ b/internal/storage/dolt/connection_pool_test.go @@ -41,8 +41,8 @@ type mockDriver struct { qmu sync.Mutex // guards queries queries []string // every query string passed to Prepare (borrow-path assertions) - // failCheckout makes Prepare of a DOLT_CHECKOUT statement fail, so borrow-path - // tests can force beginTxOnConn to error and fall through to the fallback. + // failCheckout makes Prepare of a DOLT_CHECKOUT statement fail, modelling a + // user who is denied DOLT_CHECKOUT (a least-privilege operator). failCheckout atomic.Bool // activeBranch is what a SELECT active_branch() query reports; empty means diff --git a/internal/storage/dolt/store.go b/internal/storage/dolt/store.go index 0ee4434e4..2dc8e0a13 100644 --- a/internal/storage/dolt/store.go +++ b/internal/storage/dolt/store.go @@ -1092,6 +1092,25 @@ func (s *DoltStore) withReadTx(ctx context.Context, fn func(tx *sql.Tx) error) e // and a single pinned *sql.Conn (see recomputeAllBlocked/recomputeBlockedTx). type execer interface { ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) + QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row +} + +// checkoutBranchIfNeeded puts conn's session on branch, issuing DOLT_CHECKOUT +// only when the session is on a different branch. A fresh session usually +// already sits on the requested branch, and a least-privilege operator user +// (EXECUTE on dolt_add/dolt_commit alone) is denied even a no-op checkout. conn +// must be a single session: on a multi-connection *sql.DB the read and the +// checkout could land on different connections. +func checkoutBranchIfNeeded(ctx context.Context, conn execer, branch string) error { + var active string + if err := conn.QueryRowContext(ctx, "SELECT active_branch()").Scan(&active); err != nil { + return fmt.Errorf("read active branch: %w", err) + } + if active == branch { + return nil + } + _, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", branch) + return err } // pinStoreBranch reproduces the store's real active branch on conn. Branch @@ -1121,18 +1140,22 @@ type execer interface { // in practice rather than by construction. The s.branch fallback does not // close the gap either: it fires only when the query errors, not when it // succeeds with another connection's answer. +// +// conn must be a single session (a *sql.Conn, or a *sql.DB capped at one open +// connection that keeps it idle-cached): the branch read and any checkout +// must land on the same connection. func (s *DoltStore) pinStoreBranch(ctx context.Context, conn execer) error { var branch string if scanErr := s.db.QueryRowContext(ctx, "SELECT active_branch()").Scan(&branch); scanErr == nil { if branch != "" { - if _, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", branch); err != nil { + if err := checkoutBranchIfNeeded(ctx, conn, branch); err != nil { return fmt.Errorf("checkout active branch %q: %w", branch, err) } } } else if s.branch != "" { // Fall back to the store's recorded branch rather than failing the // whole call outright. - if _, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", s.branch); err != nil { + if err := checkoutBranchIfNeeded(ctx, conn, s.branch); err != nil { return fmt.Errorf("checkout fallback branch %q: %w", s.branch, err) } } diff --git a/internal/storage/dolt/transaction.go b/internal/storage/dolt/transaction.go index 8bebf0b9f..ff2be619b 100644 --- a/internal/storage/dolt/transaction.go +++ b/internal/storage/dolt/transaction.go @@ -464,14 +464,17 @@ func beginBorrowedTx(ctx context.Context, conn *sql.Conn, branch string) (*sql.T return tx, nil } -// beginTxOnConn checks a connection out to branch and begins a transaction on -// it. Only the fallback path uses it: the fallback owns a dedicated +// beginTxOnConn puts a connection on branch and begins a transaction on it. +// Only the fallback path uses it: the fallback owns a dedicated // single-connection pool, so checking its session out is safe. Every Dolt SQL -// session has its own active branch, so the explicit checkout is required on -// a fresh dial. +// session has its own active branch, and a fresh dial lands on the database's +// default branch — usually already the requested one. DOLT_CHECKOUT is issued +// only when the session is on a different branch: a capped operator user may +// be granted EXECUTE on dolt_add/dolt_commit alone, and an unconditional no-op +// checkout would be denied and fail every wisp write on the fallback path. func beginTxOnConn(ctx context.Context, conn *sql.Conn, branch string) (*sql.Tx, error) { - if _, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", branch); err != nil { - return nil, fmt.Errorf("failed to checkout ignored tx branch %s: %w", branch, err) + if err := checkoutBranchIfNeeded(ctx, conn, branch); err != nil { + return nil, fmt.Errorf("failed to put ignored tx conn on branch %s: %w", branch, err) } tx, err := conn.BeginTx(ctx, nil) if err != nil { diff --git a/internal/storage/dolt/transaction_ignored_checkout_test.go b/internal/storage/dolt/transaction_ignored_checkout_test.go new file mode 100644 index 000000000..6cdf10fe1 --- /dev/null +++ b/internal/storage/dolt/transaction_ignored_checkout_test.go @@ -0,0 +1,228 @@ +package dolt + +import ( + "context" + "fmt" + "strings" + "testing" + + "github.com/steveyegge/beads/internal/storage" + "github.com/steveyegge/beads/internal/types" +) + +// TestBeginTxOnConnSkipsCheckoutWhenAlreadyOnBranch pins that the fresh-dial +// fallback does not issue DOLT_CHECKOUT when the session already sits on the +// requested branch. A fresh session lands on the database's default branch, so +// this is the normal case — and a capped operator user that may EXECUTE only +// dolt_add/dolt_commit is denied DOLT_CHECKOUT outright. +func TestBeginTxOnConnSkipsCheckoutWhenAlreadyOnBranch(t *testing.T) { + ctx := context.Background() + db, drv := openMockDB(t) + drv.failCheckout.Store(true) // any checkout attempt fails, like the capped user + + conn, err := db.Conn(ctx) + if err != nil { + t.Fatalf("acquire conn: %v", err) + } + defer conn.Close() + + tx, err := beginTxOnConn(ctx, conn, "main") + if err != nil { + t.Fatalf("beginTxOnConn on the session's own branch: %v", err) + } + if err := tx.Commit(); err != nil { + t.Fatalf("commit: %v", err) + } + if got := drv.countQuery("DOLT_CHECKOUT"); got != 0 { + t.Fatalf("DOLT_CHECKOUT ran %d times, want 0 (session already on the requested branch)", got) + } + if got := drv.countQuery("active_branch"); got != 1 { + t.Fatalf("active_branch ran %d times, want 1", got) + } +} + +// TestBeginTxOnConnChecksOutDifferentBranch pins that the fallback still +// switches a fresh session that is NOT on the requested branch. +func TestBeginTxOnConnChecksOutDifferentBranch(t *testing.T) { + ctx := context.Background() + db, drv := openMockDB(t) + drv.activeBranch.Store("main") + + conn, err := db.Conn(ctx) + if err != nil { + t.Fatalf("acquire conn: %v", err) + } + defer conn.Close() + + tx, err := beginTxOnConn(ctx, conn, "feature-x") + if err != nil { + t.Fatalf("beginTxOnConn on a different branch: %v", err) + } + if err := tx.Commit(); err != nil { + t.Fatalf("commit: %v", err) + } + if got := drv.countQuery("DOLT_CHECKOUT"); got != 1 { + t.Fatalf("DOLT_CHECKOUT ran %d times, want 1 (session must be switched to the requested branch)", got) + } +} + +// TestBeginTxOnConnSurfacesActiveBranchError pins that a failed branch read is +// reported rather than papered over with a blind checkout. +func TestBeginTxOnConnSurfacesActiveBranchError(t *testing.T) { + ctx := context.Background() + db, drv := openMockDB(t) + drv.failActiveBranch.Store(true) + + conn, err := db.Conn(ctx) + if err != nil { + t.Fatalf("acquire conn: %v", err) + } + defer conn.Close() + + tx, err := beginTxOnConn(ctx, conn, "main") + if err == nil { + _ = tx.Rollback() + t.Fatal("expected an error when active_branch() fails") + } + if !strings.Contains(err.Error(), "active branch") { + t.Fatalf("error = %q, want it to name the active-branch read", err.Error()) + } + if got := drv.countQuery("DOLT_CHECKOUT"); got != 0 { + t.Fatalf("DOLT_CHECKOUT ran %d times, want 0", got) + } +} + +// readOnlyMigrationTables are the schema-version tables a least-privilege operator +// only reads: covered +// by the db-level SELECT, never granted DML. +var readOnlyMigrationTables = map[string]bool{ + "schema_migrations": true, + "ignored_schema_migrations": true, +} + +// openCappedOperatorStore opens a store as a least-privilege operator user, as a +// shared Dolt server may grant one: SELECT on the database, DML on the data tables, +// and EXECUTE on only dolt_add/dolt_commit. The pool is pinned to one +// connection, so the ignored tx must take the fresh-dial fallback. The caller +// builds its own context after this returns (see openStoreWithOwnBudget). +func openCappedOperatorStore(t *testing.T) (admin, op *DoltStore) { + t.Helper() + admin, cleanup := setupConcurrentTestStore(t) + t.Cleanup(cleanup) // registered first so it runs after the DROP USER cleanup below + + ctx, cancel := testContext(t) + defer cancel() + + dbName := admin.database + user := "capped_op_" + strings.TrimPrefix(dbName, "testdb_") + const password = "capped-pw" + + exec := func(stmt string) { + t.Helper() + if _, err := admin.db.ExecContext(ctx, stmt); err != nil { + t.Fatalf("%s: %v", stmt, err) + } + } + exec(fmt.Sprintf("CREATE USER '%s'@'%%' IDENTIFIED BY '%s'", user, password)) + t.Cleanup(func() { + _, _ = admin.db.ExecContext(context.Background(), fmt.Sprintf("DROP USER IF EXISTS '%s'@'%%'", user)) + }) + exec(fmt.Sprintf("GRANT SELECT ON `%s`.* TO '%s'@'%%'", dbName, user)) + + rows, err := admin.db.QueryContext(ctx, "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'") + if err != nil { + t.Fatalf("list tables: %v", err) + } + var tables []string + for rows.Next() { + var name, kind string + if err := rows.Scan(&name, &kind); err != nil { + _ = rows.Close() + t.Fatalf("scan table: %v", err) + } + if !readOnlyMigrationTables[name] && !strings.HasPrefix(name, "dolt_") { + tables = append(tables, name) + } + } + if err := rows.Err(); err != nil { + t.Fatalf("iterate tables: %v", err) + } + _ = rows.Close() + if len(tables) == 0 { + t.Fatal("no data tables found to grant") + } + for _, table := range tables { + exec(fmt.Sprintf("GRANT INSERT, UPDATE, DELETE ON `%s`.`%s` TO '%s'@'%%'", dbName, table, user)) + } + for _, proc := range []string{"dolt_add", "dolt_commit"} { + exec(fmt.Sprintf("GRANT EXECUTE ON PROCEDURE `%s`.`%s` TO '%s'@'%%'", dbName, proc, user)) + } + + op, err = openStoreWithOwnBudget(t, &Config{ + Path: t.TempDir(), + CommitterName: "test", + CommitterEmail: "test@example.com", + Database: dbName, + ServerUser: user, + ServerPassword: password, + MaxOpenConns: 1, + }) + if err != nil { + t.Fatalf("open store as capped operator: %v", err) + } + t.Cleanup(func() { _ = op.Close() }) + return admin, op +} + +// TestIgnoredTxFallbackUnderCappedOperatorUser reproduces the failure end to +// end: writing a wisp in a transaction as a capped operator +// used to fail with "failed to checkout ignored tx branch main: ... command +// denied" (bd create --graph, bd mol wisp). +func TestIgnoredTxFallbackUnderCappedOperatorUser(t *testing.T) { + admin, op := openCappedOperatorStore(t) + ctx, cancel := testContext(t) + defer cancel() + + wisp := &types.Issue{ + ID: "test-capped-wisp", + Title: "wisp written by a capped operator", + Status: types.StatusOpen, + Priority: 2, + IssueType: types.TypeTask, + Ephemeral: true, + } + if err := op.RunInTransaction(ctx, "test: capped operator writes a wisp", func(tx storage.Transaction) error { + return tx.CreateIssue(ctx, wisp, "tester") + }); err != nil { + t.Fatalf("RunInTransaction as capped operator: %v", err) + } + + assertWispCount(ctx, t, admin.db, wisp.ID, 1) +} + +// TestPinnedConnReadsUnderCappedOperatorUser covers the other fresh-connection +// site, pinStoreBranch: the long-timeout history read and the pinned-conn +// blocked recompute used to fail with "checkout active branch \"main\": ... +// command denied" for the same capped operator. +func TestPinnedConnReadsUnderCappedOperatorUser(t *testing.T) { + _, op := openCappedOperatorStore(t) + ctx, cancel := testContext(t) + defer cancel() + + issue := &types.Issue{ + ID: "test-capped-history", + Title: "durable issue read back by a capped operator", + Status: types.StatusOpen, + Priority: 2, + IssueType: types.TypeTask, + } + if err := op.CreateIssue(ctx, issue, "tester"); err != nil { + t.Fatalf("CreateIssue as capped operator: %v", err) + } + if _, err := op.RecomputeAllBlocked(ctx); err != nil { + t.Fatalf("RecomputeAllBlocked as capped operator: %v", err) + } + if _, err := op.History(ctx, issue.ID); err != nil { + t.Fatalf("History as capped operator: %v", err) + } +}