Fix bd-36870264: Prevent nested .beads directories with path canonicalization

- Add filepath.Abs() + EvalSymlinks() to FindDatabasePath() to normalize all database paths
- Add nested .beads directory detection in setupDaemonLock() with helpful error messages
- Prevents infinite .beads/.beads/.beads/ recursion when using relative BEADS_DB paths
- All acceptance criteria passed: singleton enforcement, lock release, no recursion

Amp-Thread-ID: https://ampcode.com/threads/T-c7fc78b8-a935-48dc-8453-a1bd47a14f72
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-01 19:50:34 -07:00
parent ce9a5164f7
commit 537844cb11
3 changed files with 78 additions and 59 deletions

View File

@@ -125,11 +125,25 @@ func NewSQLiteStorage(dbPath string) (Storage, error) {
func FindDatabasePath() string {
// 1. Check environment variable
if envDB := os.Getenv("BEADS_DB"); envDB != "" {
return envDB
// Canonicalize the path to prevent nested .beads directories
if absDB, err := filepath.Abs(envDB); err == nil {
if canonical, err := filepath.EvalSymlinks(absDB); err == nil {
return canonical
}
return absDB // Return absolute path even if symlink resolution fails
}
return envDB // Fallback to original if Abs fails
}
// 2. Search for .beads/*.db in current directory and ancestors
if foundDB := findDatabaseInTree(); foundDB != "" {
// Canonicalize found path
if absDB, err := filepath.Abs(foundDB); err == nil {
if canonical, err := filepath.EvalSymlinks(absDB); err == nil {
return canonical
}
return absDB
}
return foundDB
}