devbox/internal/syncer/safejoin_test.go
shoemoney c864461fa1 fix(sec): contain manifest paths + verify blob integrity on download 🛡️🧊
Two ways a buggy/malicious hub could corrupt the local tree:
- writeEntry/deleteFile joined hub-supplied Entry.Path into root with no
  containment check, so '../../.ssh/authorized_keys' wrote/deleted OUTSIDE the
  mount. New safeJoin() uses filepath.IsLocal to refuse absolute/.. /escaping
  paths before any write or delete.
- GetBlob returned bytes with only an HTTP-status check. Blobs (chunks AND
  manifests) are content-addressed, so now verify chunk.Hash(body)==hash —
  a corrupt/truncated transfer or hostile hub can't write wrong content
  undetected. (manifest ID == chunk.Hash(Marshal()), so the check holds there.)
Regression tests for both.
2026-06-23 02:24:32 -05:00

31 lines
733 B
Go

package syncer
import (
"strings"
"testing"
)
// A hub-supplied manifest path must never resolve outside the mount root.
func TestSafeJoinRejectsEscapes(t *testing.T) {
root := "/srv/project"
for _, p := range []string{
"../escape.txt",
"../../.ssh/authorized_keys",
"/etc/passwd",
"..",
"a/../../b",
"",
} {
if got, err := safeJoin(root, p); err == nil {
t.Errorf("safeJoin(%q) = %q, nil — escape not blocked", p, got)
}
}
for _, p := range []string{"a.txt", "sub/dir/file.go", "a/../b"} {
got, err := safeJoin(root, p)
if err != nil {
t.Errorf("safeJoin(%q) = err %v, want ok", p, err)
} else if !strings.HasPrefix(got, root) {
t.Errorf("safeJoin(%q) = %q, escaped root", p, got)
}
}
}