fix(hub): derive push chunk set from the manifest, not client-supplied req.Chunks
Some checks are pending
CI / vet-build-test (push) Waiting to run
Some checks are pending
CI / vet-build-test (push) Waiting to run
Trust boundary: handlePush previously verified that every hash in req.Chunks
existed in the blob store, but it trusted req.Chunks as the authoritative list
of chunks the pushed manifest references. A buggy or malicious client could:
(a) Push a manifest referencing chunk C while omitting C from req.Chunks →
hub commits a snapshot whose manifest references a blob that may not
exist → every subsequent pull 404s on C (dangling snapshot).
(b) Include in req.Chunks a chunk not referenced by the manifest →
refcount is bumped for a blob GC can never safely collect (phantom ref).
Fix: after confirming the manifest blob is present, we now fetch + unmarshal
it and derive the DISTINCT chunk hash set directly from Manifest.Entries[*].Chunks.
Each hash in that set is verified to exist in the store; any missing hash rejects
the push with 409 (same status as before). Only manifest-referenced hashes are
passed to db.AddSnapshot() for refcounting.
Size hint: meta.ChunkRef.Size is used only for metrics/dashboard totals and is
stored once (first INSERT; ON CONFLICT only bumps refcount, not size). GC
decisions are made on refcount, not size. We keep req.Chunks as a size-hint
lookup (reqSizes map): if the client is honest the size is correct; if a hash is
absent from req.Chunks the size defaults to 0, which is harmless.
Happy-path unchanged: an honest client (syncer.go) always derives req.Chunks
from the same manifest, so all hashes are present with correct sizes.
Tests: TestHubHappyPath updated to push a real manifest JSON (required now that
the hub parses it); TestPushManifestChunkIntegrity asserts the malicious pattern
is rejected and the head does not advance, then confirms an honest push succeeds.
This commit is contained in:
parent
132978e63d
commit
f0fe810ab6
2 changed files with 131 additions and 13 deletions
|
|
@ -26,6 +26,7 @@ import (
|
||||||
"github.com/shoemoney/devbox/internal/hub/dashboard"
|
"github.com/shoemoney/devbox/internal/hub/dashboard"
|
||||||
"github.com/shoemoney/devbox/internal/hub/meta"
|
"github.com/shoemoney/devbox/internal/hub/meta"
|
||||||
"github.com/shoemoney/devbox/internal/identity"
|
"github.com/shoemoney/devbox/internal/identity"
|
||||||
|
"github.com/shoemoney/devbox/internal/manifest"
|
||||||
"github.com/shoemoney/devbox/pkg/proto"
|
"github.com/shoemoney/devbox/pkg/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -510,7 +511,11 @@ func (s *Server) handlePush(w http.ResponseWriter, r *http.Request, deviceID str
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// The manifest blob and every referenced chunk must already be uploaded.
|
// The manifest blob and every chunk it references must already be uploaded.
|
||||||
|
// Derive the authoritative chunk set from the manifest — not from
|
||||||
|
// req.Chunks — so a buggy or malicious client cannot omit a chunk the
|
||||||
|
// manifest references (dangling snapshot) or inject phantom chunk refs
|
||||||
|
// that inflate refcounts for blobs not actually referenced by the manifest.
|
||||||
if missing, err := s.missingBlob(req.ManifestHash); err != nil {
|
if missing, err := s.missingBlob(req.ManifestHash); err != nil {
|
||||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
|
|
@ -518,16 +523,42 @@ func (s *Server) handlePush(w http.ResponseWriter, r *http.Request, deviceID str
|
||||||
writeErr(w, http.StatusConflict, "missing manifest blob "+req.ManifestHash)
|
writeErr(w, http.StatusConflict, "missing manifest blob "+req.ManifestHash)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
chunks := make([]meta.ChunkRef, 0, len(req.Chunks))
|
manifestBytes, err := s.store.Get(req.ManifestHash)
|
||||||
|
if err != nil {
|
||||||
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mf, err := manifest.Unmarshal(manifestBytes)
|
||||||
|
if err != nil {
|
||||||
|
writeErr(w, http.StatusBadRequest, "invalid manifest: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Size hint from req.Chunks (used for metrics/dashboard only).
|
||||||
|
// Membership is manifest-authoritative; if a hash isn't in req.Chunks the
|
||||||
|
// size lands as 0, which is harmless (size is set once on first INSERT;
|
||||||
|
// GC decisions are made on refcount, not size).
|
||||||
|
// ponytail: size hint, not a trust boundary.
|
||||||
|
reqSizes := make(map[string]int64, len(req.Chunks))
|
||||||
for _, c := range req.Chunks {
|
for _, c := range req.Chunks {
|
||||||
if missing, err := s.missingBlob(c.Hash); err != nil {
|
reqSizes[c.Hash] = c.Size
|
||||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
}
|
||||||
return
|
seen := make(map[string]bool)
|
||||||
} else if missing {
|
var chunks []meta.ChunkRef
|
||||||
writeErr(w, http.StatusConflict, "missing chunk blob "+c.Hash)
|
for _, e := range mf.Entries {
|
||||||
return
|
for _, h := range e.Chunks {
|
||||||
|
if seen[h] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[h] = true
|
||||||
|
if missing, err := s.missingBlob(h); err != nil {
|
||||||
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
} else if missing {
|
||||||
|
writeErr(w, http.StatusConflict, "missing chunk blob "+h)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
chunks = append(chunks, meta.ChunkRef{Hash: h, Size: reqSizes[h]})
|
||||||
}
|
}
|
||||||
chunks = append(chunks, meta.ChunkRef{Hash: c.Hash, Size: c.Size})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
snapshotID := req.ManifestHash
|
snapshotID := req.ManifestHash
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"github.com/shoemoney/devbox/internal/chunk"
|
"github.com/shoemoney/devbox/internal/chunk"
|
||||||
"github.com/shoemoney/devbox/internal/hub/blobstore"
|
"github.com/shoemoney/devbox/internal/hub/blobstore"
|
||||||
"github.com/shoemoney/devbox/internal/hub/meta"
|
"github.com/shoemoney/devbox/internal/hub/meta"
|
||||||
|
"github.com/shoemoney/devbox/internal/manifest"
|
||||||
"github.com/shoemoney/devbox/pkg/proto"
|
"github.com/shoemoney/devbox/pkg/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -144,10 +145,20 @@ func TestHubHappyPath(t *testing.T) {
|
||||||
t.Fatalf("missing after upload = %v, want none", have.Missing)
|
t.Fatalf("missing after upload = %v, want none", have.Missing)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Upload the manifest blob, then push the snapshot.
|
// 3. Upload the manifest blob (must be valid JSON referencing the uploaded chunks),
|
||||||
manifest := []byte("manifest-bytes")
|
// then push the snapshot.
|
||||||
manifestHash := chunk.Hash(manifest)
|
mf := manifest.Manifest{
|
||||||
if status, body := do(t, "PUT", base+proto.PathBlob+manifestHash, bearer, manifest); status != http.StatusOK {
|
Entries: []manifest.Entry{
|
||||||
|
{Path: "a.txt", Mode: 0644, Size: 100, Chunks: []string{hashA}},
|
||||||
|
{Path: "b.txt", Mode: 0644, Size: 200, Chunks: []string{hashB}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
mfBytes, err := mf.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("manifest marshal: %v", err)
|
||||||
|
}
|
||||||
|
manifestHash := chunk.Hash(mfBytes)
|
||||||
|
if status, body := do(t, "PUT", base+proto.PathBlob+manifestHash, bearer, mfBytes); status != http.StatusOK {
|
||||||
t.Fatalf("PUT manifest status = %d, body = %s", status, body)
|
t.Fatalf("PUT manifest status = %d, body = %s", status, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -574,6 +585,82 @@ func TestMetricsOpenWhenNoToken(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestPushManifestChunkIntegrity verifies that the push path derives the
|
||||||
|
// authoritative chunk set from the manifest blob, not from req.Chunks. A push
|
||||||
|
// whose manifest references a chunk that was never uploaded (and is omitted from
|
||||||
|
// req.Chunks) must be REJECTED and the head must not advance. An honest push
|
||||||
|
// (all manifest-referenced chunks present) must still succeed.
|
||||||
|
func TestPushManifestChunkIntegrity(t *testing.T) {
|
||||||
|
base, db := testHub(t)
|
||||||
|
now := time.Now().Unix()
|
||||||
|
if err := db.CreateToken(HashToken("itok"), now+3600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
bearer := joinWith(t, base, "itok").Bearer
|
||||||
|
if st, b := do(t, "POST", base+proto.PathPublish, bearer, mustJSON(t, proto.PublishRequest{Share: "proj"})); st != http.StatusOK {
|
||||||
|
t.Fatalf("publish: %d %s", st, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upload chunkA only; chunkB is intentionally NOT uploaded.
|
||||||
|
chunkA := bytes.Repeat([]byte("a"), 100)
|
||||||
|
hashA := chunk.Hash(chunkA)
|
||||||
|
chunkB := bytes.Repeat([]byte("b"), 200)
|
||||||
|
hashB := chunk.Hash(chunkB)
|
||||||
|
if st, _ := do(t, "PUT", base+proto.PathBlob+hashA, bearer, chunkA); st != http.StatusOK {
|
||||||
|
t.Fatalf("upload chunkA: %d", st)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build a manifest that references BOTH hashA and hashB.
|
||||||
|
mf := manifest.Manifest{
|
||||||
|
Entries: []manifest.Entry{
|
||||||
|
{Path: "file.txt", Mode: 0644, Size: 300, Chunks: []string{hashA, hashB}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
mfBytes, _ := mf.Marshal()
|
||||||
|
mfHash := chunk.Hash(mfBytes)
|
||||||
|
if st, _ := do(t, "PUT", base+proto.PathBlob+mfHash, bearer, mfBytes); st != http.StatusOK {
|
||||||
|
t.Fatalf("upload manifest: %d", st)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Malicious/buggy push: omits hashB from req.Chunks even though the manifest
|
||||||
|
// references it. Hub must reject — no dangling snapshot.
|
||||||
|
st, body := do(t, "POST", base+proto.PathPush, bearer, mustJSON(t, proto.PushRequest{
|
||||||
|
Share: "proj",
|
||||||
|
ManifestHash: mfHash,
|
||||||
|
Chunks: []proto.ChunkRef{{Hash: hashA, Size: 100}},
|
||||||
|
}))
|
||||||
|
if st == http.StatusOK {
|
||||||
|
t.Fatalf("push with missing manifest chunk must be rejected, got 200: %s", body)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Head must NOT have advanced.
|
||||||
|
if st2, hb := do(t, "GET", base+proto.PathHead+"?share=proj", bearer, nil); st2 != http.StatusOK {
|
||||||
|
t.Fatalf("head: %d %s", st2, hb)
|
||||||
|
} else {
|
||||||
|
var hr proto.HeadResponse
|
||||||
|
if err := json.Unmarshal(hb, &hr); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if hr.Head != "" {
|
||||||
|
t.Fatalf("head advanced to %q after rejected push; want empty", hr.Head)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upload chunkB — now all manifest-referenced chunks are present.
|
||||||
|
if st, _ := do(t, "PUT", base+proto.PathBlob+hashB, bearer, chunkB); st != http.StatusOK {
|
||||||
|
t.Fatalf("upload chunkB: %d", st)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Honest push must now succeed.
|
||||||
|
if st3, b3 := do(t, "POST", base+proto.PathPush, bearer, mustJSON(t, proto.PushRequest{
|
||||||
|
Share: "proj",
|
||||||
|
ManifestHash: mfHash,
|
||||||
|
Chunks: []proto.ChunkRef{{Hash: hashA, Size: 100}, {Hash: hashB, Size: 200}},
|
||||||
|
})); st3 != http.StatusOK {
|
||||||
|
t.Fatalf("honest push must succeed, got %d: %s", st3, b3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestAccessLogMiddleware verifies that the wrapper passes status and body through unchanged.
|
// TestAccessLogMiddleware verifies that the wrapper passes status and body through unchanged.
|
||||||
func TestAccessLogMiddleware(t *testing.T) {
|
func TestAccessLogMiddleware(t *testing.T) {
|
||||||
inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue