fix(beads): allow /var/tmp in repo-context safety boundary (#5028)

* fix(beads): be-odye4 allow /var/tmp in repo-context safety boundary

isPathInSafeBoundary() rejected any beads dir under /var/tmp, but this
host's go shim sets GOTMPDIR=/var/tmp/gotmp for build-like subcommands
when unset, and Go's t.TempDir() roots under GOTMPDIR when set (even
though os.TempDir() itself still reports /tmp). That left the existing
os.TempDir()-based carve-out unable to match, so any test resolving a
repo context under a fresh t.TempDir() in this sandbox tripped the
"unsafe location" check.

Add a /var/tmp carve-out using the same symlink-safe resolution as the
existing /Users/Shared carve-out, since /var/tmp is equally
world-writable. General FHS-standard fix, not sandbox-specific.

Diagnosed alongside the cmd/bd 3-minute timeout panic (contention, not
a hang -- no code fix needed there, see bead notes) and two unrelated
env-leak bugs split out separately (be-3b1fn, be-0vags).

* fix(beads): admit physical OS temp roots safely (mybd-nazg)

Recognize both lexical and canonical OS temp-root spellings before applying resolvedPathWithinRoot as the final symlink-safe containment check. This restores the production hunk dropped while merging #5044, preserves #5030's escape hardening, and keeps #5028's /var/tmp carve-out.

Agent-Signature: codex-gpt-5.6-sol-xhigh on behalf of maphew

---------

Co-authored-by: matt wilkie <maphew@gmail.com>
This commit is contained in:
Jim Wordelman 2026-07-25 14:02:41 -07:00 • committed by GitHub
commit af076b6289
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 52 additions and 0 deletions

View file

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

View file

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