merge: 🛡️ devbox local-exclude no longer deletes a shared file

This commit is contained in:
shoemoney 2026-08-05 11:38:03 -05:00
commit 6ea5c22cd1
3 changed files with 173 additions and 18 deletions

View file

@ -0,0 +1,63 @@
package syncer
import (
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/shoemoney/devbox/internal/hub"
"github.com/shoemoney/devbox/internal/hub/blobstore"
"github.com/shoemoney/devbox/internal/hub/meta"
"github.com/shoemoney/devbox/internal/secret"
)
// Companion to the locally-excluded-file preserve case: a path that's genuinely
// gone from disk (no .devignore rule hides it — the user just deleted it) must
// still delete on push. The carry-forward fix for excluded paths must not make
// real deletes un-deletable.
func TestPushRealDeletePropagates(t *testing.T) {
db, _ := meta.Open(":memory:")
defer db.Close()
store, _ := blobstore.NewDisk(t.TempDir())
srv := httptest.NewServer(hub.NewServer(db, store).Handler())
defer srv.Close()
guard, _ := secret.New(nil)
igEmpty, _ := LoadIgnore(t.TempDir())
A := joinDevice(t, db, srv.URL, "alice")
B := joinDevice(t, db, srv.URL, "bob")
if err := A.Publish("s"); err != nil {
t.Fatal(err)
}
rootA := t.TempDir()
writeFile(t, rootA, "data.bin", "precious\n")
writeFile(t, rootA, "app.txt", "x\n")
snapA, _, err := Sync(A, rootA, "s", "", "", "alice", 1, igEmpty, guard, nil)
if err != nil {
t.Fatalf("A sync: %v", err)
}
// B mounts the whole share (no exclude), then genuinely deletes data.bin.
rootB := t.TempDir()
baseB, _, err := Sync(B, rootB, "s", "", "", "bob", 2, igEmpty, guard, nil)
if err != nil {
t.Fatalf("B initial sync: %v", err)
}
if err := os.Remove(filepath.Join(rootB, "data.bin")); err != nil {
t.Fatalf("B delete: %v", err)
}
if _, _, err := Sync(B, rootB, "s", "", baseB, "bob", 3, igEmpty, guard, nil); err != nil {
t.Fatalf("B sync after delete: %v", err)
}
// A pulls: the real delete must land, unlike the excluded-file case.
pr, err := Pull(A, rootA, "s", "", snapA, "alice", 4, igEmpty, guard, nil)
if err != nil {
t.Fatalf("A pull: %v", err)
}
if _, err := os.Stat(filepath.Join(rootA, "data.bin")); !os.IsNotExist(err) {
t.Fatalf("B's genuine delete did not propagate to A: deleted=%v", pr.Deleted)
}
}

View file

@ -0,0 +1,60 @@
package syncer
import (
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/shoemoney/devbox/internal/hub"
"github.com/shoemoney/devbox/internal/hub/blobstore"
"github.com/shoemoney/devbox/internal/hub/meta"
"github.com/shoemoney/devbox/internal/secret"
)
// Repro for audit finding: device B's device-local exclude drops a shared file
// from the pushed head, and device A's next pull deletes it from disk.
func TestReproPushDropsLocallyExcludedFile(t *testing.T) {
db, _ := meta.Open(":memory:")
defer db.Close()
store, _ := blobstore.NewDisk(t.TempDir())
srv := httptest.NewServer(hub.NewServer(db, store).Handler())
defer srv.Close()
guard, _ := secret.New(nil)
igEmpty, _ := LoadIgnore(t.TempDir())
A := joinDevice(t, db, srv.URL, "alice")
B := joinDevice(t, db, srv.URL, "bob")
if err := A.Publish("s"); err != nil {
t.Fatal(err)
}
rootA := t.TempDir()
// A publishes data.bin + app.txt.
writeFile(t, rootA, "data.bin", "precious\n")
writeFile(t, rootA, "app.txt", "x\n")
snapA, _, err := Sync(A, rootA, "s", "", "", "alice", 1, igEmpty, guard, nil)
if err != nil {
t.Fatalf("A sync: %v", err)
}
// B mounts the share with a device-local exclude for data.bin and syncs.
rootB := t.TempDir()
igB, err := LoadIgnoreWith(rootB, []string{"data.bin"})
if err != nil {
t.Fatal(err)
}
if _, _, err := Sync(B, rootB, "s", "", "", "bob", 2, igB, guard, nil); err != nil {
t.Fatalf("B sync: %v", err)
}
// A pulls the new head. If push dropped data.bin from the head, A deletes it.
pr, err := Pull(A, rootA, "s", "", snapA, "alice", 3, igEmpty, guard, nil)
if err != nil {
t.Fatalf("A pull: %v", err)
}
t.Logf("A pull result: written=%v deleted=%v conflicts=%v", pr.Written, pr.Deleted, pr.Conflicts)
if _, err := os.Stat(filepath.Join(rootA, "data.bin")); os.IsNotExist(err) {
t.Fatalf("CONFIRMED: A's data.bin was deleted by B's device-local exclude (deleted=%v)", pr.Deleted)
}
}

View file

@ -70,8 +70,10 @@ func Push(c *transport.Client, root, share, subpath string, ig *ignore.Matcher,
} }
} }
// Splice the local subtree into the full share manifest (no-op when subpath == ""). // Splice the local subtree into the full share manifest, carrying forward any
fullM, err := splice(c, localM, subpath, parent) // parent-head entry this device can't see because of its own ignore rules (a
// device-local exclude must never delete a path from the shared head).
fullM, err := splice(c, localM, subpath, parent, ig, blocked)
if err != nil { if err != nil {
return Result{}, err return Result{}, err
} }
@ -133,23 +135,53 @@ func Push(c *transport.Client, root, share, subpath string, ig *ignore.Matcher,
}, nil }, nil
} }
// splice builds the full share manifest from a sub-path mount's local manifest: // splice builds the full share manifest from a mount's local manifest: it
// it re-prefixes local entries with subpath and keeps the parent snapshot's // re-prefixes local entries with subpath, keeps the parent snapshot's entries
// entries outside subpath. For a whole-share mount it returns localM unchanged. // outside subpath untouched, and — within subpath — carries forward any parent
func splice(c *transport.Client, localM manifest.Manifest, subpath, parent string) (manifest.Manifest, error) { // entry the local device didn't produce because ig excludes it (a path
if subpath == "" { // "excluded from view" is not a "path deleted by me"). An entry the guard
return localM, nil // blocks, or one that's genuinely gone from disk (no ignore rule hides it), is
} // NOT carried forward: the guard's whole job is to keep that content out of the
// head, and a real local delete must still delete. For a whole-share mount
// (subpath == "") this same pass runs over the full path space: under and
// strip both treat "" as "everything", so it's the same logic, not a special
// case.
func splice(c *transport.Client, localM manifest.Manifest, subpath, parent string, ig *ignore.Matcher, blocked []string) (manifest.Manifest, error) {
if parent == "" {
entries := make([]manifest.Entry, 0, len(localM.Entries)) entries := make([]manifest.Entry, 0, len(localM.Entries))
if parent != "" { for _, e := range localM.Entries {
e.Path = prefixPath(e.Path, subpath)
entries = append(entries, e)
}
sort.Slice(entries, func(i, j int) bool { return entries[i].Path < entries[j].Path })
return manifest.Manifest{Entries: entries}, nil
}
base, err := fetchManifest(c, parent) base, err := fetchManifest(c, parent)
if err != nil { if err != nil {
return manifest.Manifest{}, err return manifest.Manifest{}, err
} }
blockedSet := make(map[string]bool, len(blocked))
for _, p := range blocked {
blockedSet[p] = true
}
localSet := make(map[string]bool, len(localM.Entries))
for _, e := range localM.Entries {
localSet[e.Path] = true
}
entries := make([]manifest.Entry, 0, len(base.Entries)+len(localM.Entries))
for _, e := range base.Entries { for _, e := range base.Entries {
if !under(e.Path, subpath) { if !under(e.Path, subpath) {
entries = append(entries, e) entries = append(entries, e) // outside the mount: untouched
continue
} }
local := strip(e.Path, subpath)
if localSet[local] {
continue // present locally; localM's copy (appended below) wins
}
if !blockedSet[local] && ig != nil && ig.Match(local, false) {
entries = append(entries, e) // excluded from view, not deleted: preserve
} }
} }
for _, e := range localM.Entries { for _, e := range localM.Entries {