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.
This commit is contained in:
parent
96fd6018a9
commit
c864461fa1
4 changed files with 101 additions and 2 deletions
|
|
@ -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-<host>-<ts>.ext, returning the conflict copy's relative path.
|
||||
func preserveAsConflict(root, relPath, host string, ts int64) (string, error) {
|
||||
|
|
|
|||
31
internal/syncer/safejoin_test.go
Normal file
31
internal/syncer/safejoin_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
41
internal/transport/integrity_test.go
Normal file
41
internal/transport/integrity_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue