From c864461fa1969b42fba069cd2ca6c39e735e4741 Mon Sep 17 00:00:00 2001 From: shoemoney Date: Tue, 23 Jun 2026 02:24:32 -0500 Subject: [PATCH] =?UTF-8?q?fix(sec):=20contain=20manifest=20paths=20+=20ve?= =?UTF-8?q?rify=20blob=20integrity=20on=20download=20=F0=9F=9B=A1=EF=B8=8F?= =?UTF-8?q?=F0=9F=A7=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- internal/syncer/pull.go | 24 ++++++++++++++-- internal/syncer/safejoin_test.go | 31 +++++++++++++++++++++ internal/transport/client.go | 7 +++++ internal/transport/integrity_test.go | 41 ++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 internal/syncer/safejoin_test.go create mode 100644 internal/transport/integrity_test.go diff --git a/internal/syncer/pull.go b/internal/syncer/pull.go index 54fee61..c01c699 100644 --- a/internal/syncer/pull.go +++ b/internal/syncer/pull.go @@ -340,7 +340,10 @@ func writeEntry(c *transport.Client, root string, e manifest.Entry) (fsErr bool, } buf = append(buf, b...) } - dst := filepath.Join(root, filepath.FromSlash(e.Path)) + dst, serr := safeJoin(root, e.Path) + if serr != nil { + return true, serr // unsafe manifest path: skip it, don't apply outside root + } if merr := os.MkdirAll(filepath.Dir(dst), 0o755); merr != nil { return true, merr } @@ -355,13 +358,30 @@ func writeEntry(c *transport.Client, root string, e manifest.Entry) (fsErr bool, } func deleteFile(root, relPath string) error { - err := os.Remove(filepath.Join(root, filepath.FromSlash(relPath))) + p, serr := safeJoin(root, relPath) + if serr != nil { + return serr + } + err := os.Remove(p) if os.IsNotExist(err) { return nil } return err } +// safeJoin joins a hub-supplied, slash-separated relative path under root, +// refusing anything that would escape the mount (absolute path, "..", empty, or +// a Windows reserved name). Manifest paths come from the hub/peers and are +// applied to the local filesystem, so an unchecked "../../.ssh/authorized_keys" +// would let a malicious or buggy hub write or delete files outside the mount. +func safeJoin(root, relPath string) (string, error) { + clean := filepath.FromSlash(relPath) + if !filepath.IsLocal(clean) { + return "", fmt.Errorf("refusing unsafe manifest path %q", relPath) + } + return filepath.Join(root, clean), nil +} + // preserveAsConflict renames the current local file at relPath to a sibling named // path.conflict--.ext, returning the conflict copy's relative path. func preserveAsConflict(root, relPath, host string, ts int64) (string, error) { diff --git a/internal/syncer/safejoin_test.go b/internal/syncer/safejoin_test.go new file mode 100644 index 0000000..4b72c31 --- /dev/null +++ b/internal/syncer/safejoin_test.go @@ -0,0 +1,31 @@ +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) + } + } +} diff --git a/internal/transport/client.go b/internal/transport/client.go index 8b7dc09..eef05dd 100644 --- a/internal/transport/client.go +++ b/internal/transport/client.go @@ -20,6 +20,7 @@ import ( "golang.org/x/time/rate" + "git.shoemoney.ai/shoemoney/devbox/internal/chunk" "git.shoemoney.ai/shoemoney/devbox/pkg/proto" ) @@ -134,6 +135,12 @@ func (c *Client) GetBlob(hash string) ([]byte, error) { if resp.StatusCode < 200 || resp.StatusCode >= 300 { return nil, apiError(resp.StatusCode, body) } + // Blobs (chunks and manifests) are content-addressed: the key is BLAKE3 of the + // bytes. Verify it so a corrupt/truncated transfer or a buggy/malicious hub + // can't write wrong content into the user's tree undetected. + if chunk.Hash(body) != hash { + return nil, fmt.Errorf("devbox: blob %s failed integrity check (got %s)", hash, chunk.Hash(body)) + } return body, nil } diff --git a/internal/transport/integrity_test.go b/internal/transport/integrity_test.go new file mode 100644 index 0000000..fcd684d --- /dev/null +++ b/internal/transport/integrity_test.go @@ -0,0 +1,41 @@ +package transport + +import ( + "net/http" + "net/http/httptest" + "testing" + + "git.shoemoney.ai/shoemoney/devbox/internal/chunk" +) + +// GetBlob must reject bytes whose BLAKE3 hash doesn't match the requested key — +// a corrupt/truncated transfer or a malicious hub can't slip wrong content in. +func TestGetBlobVerifiesIntegrity(t *testing.T) { + good := []byte("the real chunk bytes") + goodHash := chunk.Hash(good) + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("TAMPERED payload, wrong content")) // not what goodHash addresses + })) + defer srv.Close() + + c := New(srv.URL) + c.SetBearer("x") + if _, err := c.GetBlob(goodHash); err == nil { + t.Fatal("GetBlob accepted bytes that don't match the content hash") + } + + // Sanity: a server returning the correct bytes verifies cleanly. + srvOK := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write(good) + })) + defer srvOK.Close() + c2 := New(srvOK.URL) + c2.SetBearer("x") + b, err := c2.GetBlob(goodHash) + if err != nil || string(b) != string(good) { + t.Fatalf("GetBlob(valid) = %q, %v", b, err) + } +}