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.
41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
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)
|
|
}
|
|
}
|