diff --git a/internal/beads/context.go b/internal/beads/context.go index 49d40d6d7..ceb689c49 100644 --- a/internal/beads/context.go +++ b/internal/beads/context.go @@ -333,6 +333,18 @@ func isPathInSafeBoundary(path string) bool { return true } + // Allow /var/tmp as the FHS-standard secondary temp directory (persists across + // reboots, unlike /tmp). This is distinct from the os.TempDir() carve-out + // above: a machine's build tooling can set GOTMPDIR to redirect Go's own + // test/compile temp dirs under /var/tmp even while os.TempDir() itself still + // reports /tmp, so t.TempDir() in a test binary can land here without the + // os.TempDir() check ever seeing it (be-odye4). Like /Users/Shared, /var/tmp is + // world-writable (drwxrwxrwt), so resolve symlinks before admitting (SEC-003): + // a symlink planted under it must not be followed into a rejected directory. + if absPath == "/var/tmp" || strings.HasPrefix(absPath, "/var/tmp/") { + return resolvedPathWithinRoot(absPath, "/var/tmp") + } + for _, prefix := range unsafePrefixes { if strings.HasPrefix(absPath, prefix+"/") || absPath == prefix { return false diff --git a/internal/beads/context_test.go b/internal/beads/context_test.go index b9f1a061a..0c06c6eda 100644 --- a/internal/beads/context_test.go +++ b/internal/beads/context_test.go @@ -390,6 +390,15 @@ func TestIsPathInSafeBoundary(t *testing.T) { // user's home — it must be accepted (be-vc1 / SEC-003 carve-out). {"macOS shared subdir", "/Users/Shared/portharbour/.beads", true}, {"macOS shared root", "/Users/Shared", true}, + + // /var/tmp is the FHS-standard secondary temp directory (persists across + // reboots, unlike /tmp) and must be accepted despite matching the /var + // unsafePrefixes entry above. Go's own test/build tooling can root + // GOTMPDIR-influenced temp dirs here even when os.TempDir() itself still + // reports /tmp, so the os.TempDir()-based carve-out above doesn't cover it + // (be-odye4). + {"var/tmp root", "/var/tmp", true}, + {"var/tmp GOTMPDIR-style subdir", "/var/tmp/gotmp/TestSomething1234/001/.beads", true}, } for _, tt := range tests { @@ -484,6 +493,37 @@ func TestIsPathInSafeBoundary_SharedSymlinkEscape(t *testing.T) { } } +// TestIsPathInSafeBoundary_VarTmpSymlinkEscape proves the /var/tmp carve-out +// (be-odye4) applies the same symlink-safe resolution as /Users/Shared: /var/tmp +// is world-writable (drwxrwxrwt) on Linux/BSD, so a co-located user could plant a +// symlink under it whose target escapes to a rejected system directory. Skipped +// when /var/tmp is absent or not writable, so it never fails spuriously. +func TestIsPathInSafeBoundary_VarTmpSymlinkEscape(t *testing.T) { + const varTmp = "/var/tmp" + if info, err := os.Stat(varTmp); err != nil || !info.IsDir() { + t.Skipf("%s not present as a directory: %v", varTmp, err) + } + + link := filepath.Join(varTmp, fmt.Sprintf(".be-odye4-escape-test-%d", os.Getpid())) + _ = os.Remove(link) + if err := os.Symlink("/etc", link); err != nil { + t.Skipf("cannot create symlink in %s (not writable?): %v", varTmp, err) + } + t.Cleanup(func() { _ = os.Remove(link) }) + + // A BEADS_DIR routed *through* the escaping symlink must be rejected: its bytes + // resolve into /etc, outside /var/tmp. + target := filepath.Join(link, ".beads") + if isPathInSafeBoundary(target) { + t.Errorf("isPathInSafeBoundary(%q) = true, want false (path through symlink escaping /var/tmp to /etc)", target) + } + + // The escaping symlink itself also resolves outside the boundary. + if isPathInSafeBoundary(link) { + t.Errorf("isPathInSafeBoundary(%q) = true, want false (symlink under /var/tmp escaping to /etc)", link) + } +} + // TestIsPathInSafeBoundary_TempDirSymlinkEscape mirrors // TestIsPathInSafeBoundary_SharedSymlinkEscape for the os.TempDir() carve-out // (be-kghzr SEC-003 hardening). Unlike /Users/Shared, os.TempDir() is