diff --git a/cmd/bd/dolt.go b/cmd/bd/dolt.go index fc29bbacc..3142fdd71 100644 --- a/cmd/bd/dolt.go +++ b/cmd/bd/dolt.go @@ -380,10 +380,8 @@ var doltIdleMonitorCmd = &cobra.Command{ os.Exit(1) } - // Write our PID and ensure cleanup on all exit paths - pidFile := filepath.Join(beadsDir, "dolt-monitor.pid") - _ = os.WriteFile(pidFile, []byte(strconv.Itoa(os.Getpid())), 0600) - defer func() { _ = os.Remove(pidFile) }() + // PID file and lock management is handled inside RunIdleMonitor + // to ensure single-instance enforcement (GH#2367). // Parse idle timeout from config idleTimeout := doltserver.DefaultIdleTimeout @@ -397,7 +395,8 @@ var doltIdleMonitorCmd = &cobra.Command{ } } - // Handle SIGTERM gracefully (os.Exit doesn't run defers) + // Handle SIGTERM gracefully — clean up PID file on signal + pidFile := filepath.Join(beadsDir, "dolt-monitor.pid") sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT) go func() { diff --git a/cmd/bd/init.go b/cmd/bd/init.go index 27f473985..6690d9f14 100644 --- a/cmd/bd/init.go +++ b/cmd/bd/init.go @@ -3,6 +3,7 @@ package main import ( "bufio" "context" + "crypto/rand" "fmt" "os" "os/exec" @@ -479,6 +480,13 @@ environment variable.`, cfg = configfile.DefaultConfig() } + // Generate project identity UUID if not already set (GH#2372). + // This UUID is stored in both metadata.json and the database, + // and verified on every connection to detect cross-project leakage. + if cfg.ProjectID == "" { + cfg.ProjectID = generateProjectID() + } + // Always store backend explicitly in metadata.json cfg.Backend = backend // Metadata.json.database should point to the Dolt directory (not beads.db). @@ -517,6 +525,13 @@ environment variable.`, // Non-fatal - continue anyway } + // Write project identity to database for cross-project verification (GH#2372) + if cfg.ProjectID != "" && store != nil { + if err := store.SetMetadata(ctx, "_project_id", cfg.ProjectID); err != nil { + fmt.Fprintf(os.Stderr, "Warning: failed to write project ID to database: %v\n", err) + } + } + // Create config.yaml template (prefix is stored in DB, not config.yaml) if err := createConfigYaml(beadsDir, false, ""); err != nil { fmt.Fprintf(os.Stderr, "Warning: failed to create config.yaml: %v\n", err) @@ -1167,6 +1182,19 @@ func promptContributorMode() (isContributor bool, err error) { // verifyMetadata writes a metadata field and verifies the write succeeded. // Returns true if write+verify succeeded, false with warning if either failed. +// generateProjectID creates a UUID v4 for project identity verification. +func generateProjectID() string { + b := make([]byte, 16) + if _, err := rand.Read(b); err != nil { + // Fallback: use timestamp + PID as a unique-enough identifier + return fmt.Sprintf("%d-%d", time.Now().UnixNano(), os.Getpid()) + } + // Set version (4) and variant (RFC 4122) + b[6] = (b[6] & 0x0f) | 0x40 + b[8] = (b[8] & 0x3f) | 0x80 + return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16]) +} + func verifyMetadata(ctx context.Context, store *dolt.DoltStore, key, value string) bool { if err := store.SetMetadata(ctx, key, value); err != nil { fmt.Fprintf(os.Stderr, "Warning: failed to write %s metadata: %v\n", key, err) diff --git a/internal/configfile/configfile.go b/internal/configfile/configfile.go index 38f73d569..a9db9b11a 100644 --- a/internal/configfile/configfile.go +++ b/internal/configfile/configfile.go @@ -31,6 +31,11 @@ type Config struct { DoltRemotesAPIPort int `json:"dolt_remotesapi_port,omitempty"` // Dolt remotesapi port for federation (default: 8080) // Note: Password should be set via BEADS_DOLT_PASSWORD env var for security + // Project identity — unique ID generated at bd init time. + // Used to detect cross-project data leakage when a client connects + // to the wrong Dolt server (GH#2372). + ProjectID string `json:"project_id,omitempty"` + // Stale closed issues check configuration // 0 = disabled (default), positive = threshold in days StaleClosedIssuesDays int `json:"stale_closed_issues_days,omitempty"` diff --git a/internal/doltserver/doltserver.go b/internal/doltserver/doltserver.go index 8e085a248..c97785417 100644 --- a/internal/doltserver/doltserver.go +++ b/internal/doltserver/doltserver.go @@ -276,14 +276,18 @@ func DefaultConfig(beadsDir string) *Config { } } - // Check if user configured an explicit port in metadata.json - if metaCfg, err := configfile.Load(beadsDir); err == nil && metaCfg != nil { - if metaCfg.DoltServerPort > 0 { - cfg.Port = metaCfg.DoltServerPort - } + // Check the port file (gitignored, local-only) — this is the primary + // persistent source. Start() writes the actual listening port here. + // Elevated to top priority (after env var) to prevent git-tracked values + // from causing cross-project data leakage (GH#2372). + if p := readPortFile(beadsDir); 0 < p { + cfg.Port = p + return cfg } // Check config.yaml / global config (~/.config/bd/config.yaml) (GH#2073) + // Note: project-level config.yaml dolt.port is git-tracked and could + // propagate to collaborators. Prefer the gitignored port file above. if cfg.Port == 0 { if p := config.GetYamlConfig("dolt.port"); p != "" { if port, err := strconv.Atoi(p); err == nil && port > 0 { @@ -292,12 +296,18 @@ func DefaultConfig(beadsDir string) *Config { } } - // Check the port file — Start() writes the actual listening port here, - // which may differ from the configured default when Start() falls back - // to DerivePort (e.g. another project's server occupied the default port). + // Deprecated: metadata.json DoltServerPort is git-tracked and propagates + // to all contributors, causing cross-project data leakage (GH#2372). + // Emit a one-time warning but still use the value as a fallback so + // existing setups don't break silently. if cfg.Port == 0 { - if p := readPortFile(beadsDir); 0 < p { - cfg.Port = p + if metaCfg, err := configfile.Load(beadsDir); err == nil && metaCfg != nil { + if metaCfg.DoltServerPort > 0 { + fmt.Fprintf(os.Stderr, "Warning: dolt_server_port in metadata.json is deprecated (can cause cross-project data leakage).\n") + fmt.Fprintf(os.Stderr, " The port file (.beads/dolt-server.port) is now the primary source.\n") + fmt.Fprintf(os.Stderr, " Remove dolt_server_port from .beads/metadata.json to silence this warning.\n") + cfg.Port = metaCfg.DoltServerPort + } } } @@ -1158,6 +1168,32 @@ func RunIdleMonitor(beadsDir string, idleTimeout time.Duration) { return } + // Single-instance enforcement: acquire an exclusive lock on the monitor + // lock file. If another monitor is already running, exit immediately. + // This prevents the accumulation bug (GH#2367) where Start() called from + // within the monitor's watchdog restart would fork yet another monitor. + monitorLockPath := monitorPidPath(beadsDir) + ".lock" + var monitorLock *os.File + if f, err := os.OpenFile(monitorLockPath, os.O_CREATE|os.O_RDWR, 0600); err == nil { //nolint:gosec // G304: path derived from trusted beadsDir + if lockErr := lockfile.FlockExclusiveNonBlocking(f); lockErr != nil { + _ = f.Close() + return // another monitor holds the lock — exit silently + } + monitorLock = f + } + // Keep lock held for lifetime of this process. Clean up on exit. + defer func() { + _ = os.Remove(monitorPidPath(beadsDir)) + if monitorLock != nil { + _ = lockfile.FlockUnlock(monitorLock) + _ = monitorLock.Close() + _ = os.Remove(monitorLockPath) + } + }() + + // Write our PID now that we hold the lock + _ = os.WriteFile(monitorPidPath(beadsDir), []byte(strconv.Itoa(os.Getpid())), 0600) + // Tracks when we stopped the server for idle timeout. Zero means we // haven't performed an idle shutdown (or the server was restarted since). var idleShutdownAt time.Time diff --git a/internal/storage/dolt/store.go b/internal/storage/dolt/store.go index 2ae88b66f..aec4109f1 100644 --- a/internal/storage/dolt/store.go +++ b/internal/storage/dolt/store.go @@ -687,6 +687,17 @@ func newServerMode(ctx context.Context, cfg *Config) (*DoltStore, error) { } } + // Project identity verification: detect cross-project data leakage (GH#2372). + // If the local metadata.json has a project_id and the database has one too, + // they must match. A mismatch means this client is connected to the wrong + // project's Dolt server — refuse to proceed. + if !cfg.CreateIfMissing { + if verifyErr := store.verifyProjectIdentity(ctx, cfg.BeadsDir); verifyErr != nil { + _ = db.Close() + return nil, verifyErr + } + } + // All writers operate on main — transaction isolation via RunInTransaction // replaces the former branch-per-polecat approach (BD_BRANCH). store.branch = "main" @@ -694,6 +705,46 @@ func newServerMode(ctx context.Context, cfg *Config) (*DoltStore, error) { return store, nil } +// verifyProjectIdentity checks that the database belongs to the expected project. +// If both the local metadata.json and the database have a project_id, they must match. +// Returns nil if verification passes or is not applicable (missing IDs = old setup). +func (s *DoltStore) verifyProjectIdentity(ctx context.Context, beadsDir string) error { + if beadsDir == "" { + return nil // can't verify without knowing beadsDir + } + + // Load local project ID from metadata.json + metaCfg, err := configfile.Load(beadsDir) + if err != nil || metaCfg == nil { + return nil // no local config — skip verification + } + localID := metaCfg.ProjectID + if localID == "" { + return nil // old-style metadata.json without project_id — skip + } + + // Read project ID from database metadata table + dbID, err := s.GetMetadata(ctx, "_project_id") + if err != nil || dbID == "" { + return nil // old database without project_id — skip + } + + if localID != dbID { + return fmt.Errorf( + "PROJECT IDENTITY MISMATCH — refusing to connect\n\n"+ + " Local project ID (metadata.json): %s\n"+ + " Database project ID: %s\n\n"+ + "This means the Dolt server is serving a DIFFERENT project's database.\n"+ + "This can happen when:\n"+ + " - Another project's server is running on the same port\n"+ + " - The server restarted with a different data directory\n\n"+ + "To diagnose: bd dolt status\n"+ + "Do NOT run 'bd init' — your data likely exists, just on a different server.", + localID, dbID) + } + return nil +} + // isLocalHost returns true if the host refers to the local machine. func isLocalHost(host string) bool { switch host {