From 57ebed35b542b53b48513caca6c7d50eae3cc955 Mon Sep 17 00:00:00 2001 From: shoemoney Date: Tue, 23 Jun 2026 02:28:51 -0500 Subject: [PATCH] =?UTF-8?q?fix(dataloss):=20don't=20clobber=20a=20.devigno?= =?UTF-8?q?re'd/guarded=20local=20file=20on=20pull=20=F0=9F=A9=B8?= =?UTF-8?q?=F0=9F=9B=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pull merge compared the hub against 'ours' = a .devignore/secret-guard FILTERED manifest. A file still on disk but excluded from 'ours' looked like a local delete, so a hub edit to that path hit 'case tOK && !oOK' and overwrote the on-disk bytes with NO conflict copy — silently destroying ignored configs and the very .env/keys the guard exists to protect (a teammate pushing .env could wipe your local secret). Now: in that branch, stat the real file first; if it's actually present, preserve it as a .conflict-- copy before applying the hub version — honoring the 'never lose a byte' promise. Regression test proves the local bytes survive. --- internal/syncer/preserve_ignored_test.go | 73 ++++++++++++++++++++++++ internal/syncer/pull.go | 18 +++++- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 internal/syncer/preserve_ignored_test.go diff --git a/internal/syncer/preserve_ignored_test.go b/internal/syncer/preserve_ignored_test.go new file mode 100644 index 0000000..d72db5c --- /dev/null +++ b/internal/syncer/preserve_ignored_test.go @@ -0,0 +1,73 @@ +package syncer + +import ( + "net/http/httptest" + "strings" + "testing" + + "git.shoemoney.ai/shoemoney/devbox/internal/hub" + "git.shoemoney.ai/shoemoney/devbox/internal/hub/blobstore" + "git.shoemoney.ai/shoemoney/devbox/internal/hub/meta" + "git.shoemoney.ai/shoemoney/devbox/internal/secret" +) + +// Regression for the critical data-loss bug: a hub edit to a path that the local +// device .devignore's (so it's filtered out of `ours`) must NOT silently clobber +// the on-disk file — its bytes are preserved as a conflict copy first. +func TestPullPreservesIgnoredLocalFile(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() + + // snap1 (B's base): the file exists on the hub. + writeFile(t, rootA, "ignored.dat", "hub-v1\n") + writeFile(t, rootA, "app.txt", "x\n") + snap1, _, err := Sync(A, rootA, "s", "", "", "alice", 1, igEmpty, guard, nil) + if err != nil { + t.Fatalf("A sync1: %v", err) + } + // snap2 (head): A edits the file. + writeFile(t, rootA, "ignored.dat", "hub-edit\n") + if _, _, err = Sync(A, rootA, "s", "", snap1, "alice", 2, igEmpty, guard, nil); err != nil { + t.Fatalf("A sync2: %v", err) + } + + // B has the file on disk with precious LOCAL bytes, but .devignore's it. + rootB := t.TempDir() + writeFile(t, rootB, "ignored.dat", "mine-local\n") + writeFile(t, rootB, ".devignore", "ignored.dat\n") + igB, err := LoadIgnore(rootB) + if err != nil { + t.Fatal(err) + } + + // B pulls from base=snap1; the hub's edit lands on a path B filters out. + pr, err := Pull(B, rootB, "s", "", snap1, "bob", 3, igB, guard, nil) + if err != nil { + t.Fatalf("B pull: %v", err) + } + + // The local bytes must survive as a conflict copy (no byte lost). The copy is + // named with the conflict marker inserted before the extension, e.g. + // "ignored.conflict-bob-3.dat". + var conflict string + for _, c := range pr.Conflicts { + if strings.Contains(c, ".conflict-") && read(t, rootB, c) == "mine-local\n" { + conflict = c + } + } + if conflict == "" { + t.Fatalf("ignored file's local bytes were clobbered (no conflict copy preserved them). conflicts=%v", pr.Conflicts) + } +} diff --git a/internal/syncer/pull.go b/internal/syncer/pull.go index c01c699..13d793b 100644 --- a/internal/syncer/pull.go +++ b/internal/syncer/pull.go @@ -118,7 +118,23 @@ func Pull(c *transport.Client, root, share, subpath, base, host string, now int6 } case tOK && !oOK: - // Ours deleted, hub edited: hub wins (a delete has no bytes to keep). + // p is absent from `ours` — but `ours` is .devignore/secret-guard + // FILTERED, so this is NOT necessarily a local delete. The file may + // still be on disk with valuable content (an ignored config, or the + // very .env/key the guard protects). Overwriting it blindly is data + // loss — exactly the bytes we promised never to lose. So if the path + // really exists on disk, preserve it as a conflict copy first; only a + // genuinely-absent path is a true delete where the hub wins. + if dst, jerr := safeJoin(root, p); jerr == nil { + if _, statErr := os.Stat(dst); statErr == nil { + cp, cerr := preserveAsConflict(root, p, host, now) + if cerr != nil { + res.Skipped = append(res.Skipped, p) // couldn't preserve; never clobber + continue + } + res.Conflicts = append(res.Conflicts, cp) + } + } if fsErr, err := writeEntry(c, root, te); err != nil { if !fsErr { return PullResult{}, err