devbox/internal/syncer/zz_repro_stale_ours_test.go
shoemoney f1a6ac226f fix(syncer): stop pull from clobbering an edit made during the pull window
manifest.Build snapshots `ours` once before Pull applies any writes. A local
edit landing after that snapshot but before writeEntry looked unchanged
(oursCh false) and got overwritten with the hub version verbatim, with no
conflict copy — the edit's bytes ended up in no file and no snapshot.

Re-stat/re-hash the on-disk file right before a hub-only write would apply
and compare it against the entry manifest.Build captured. A mismatch routes
through the existing preserve-as-conflict path instead of clobbering. Only
checked when hub is about to write over a path `ours` already knew about —
a pure hub-only add has nothing on disk yet to race with.

This narrows the window, it doesn't close it: a rename at pull.go:424-451
still isn't atomic with this check. A real fix needs a filesystem-level
lock or lease around the whole pull, tracked separately.

Includes the untracked repro (zz_repro_stale_ours_test.go) that found this.
2026-08-05 02:35:12 -05:00

95 lines
3.3 KiB
Go

package syncer
import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/shoemoney/devbox/internal/hooks"
"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: `ours` is snapshotted once (manifest.Build, pull.go:60)
// before any writes; a local edit landing after Build but before writeEntry still
// looks "unchanged vs base", so the hub version is applied verbatim with no
// conflict copy. We make the timing deterministic with a pre-pull hook, which
// runs exactly in that window (after Build, before the apply loop).
func TestAuditReproStaleOursOverwritesFreshEdit(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)
ig, _ := 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)
}
// A publishes f.txt = v1 (this becomes A's base).
rootA := t.TempDir()
writeFile(t, rootA, "f.txt", "v1\n")
snapA, _, err := Sync(A, rootA, "s", "", "", "alice", 1, ig, guard, nil)
if err != nil {
t.Fatalf("A sync: %v", err)
}
// B pulls, edits f.txt = v2-hub, pushes: hub-only change relative to A's base.
rootB := t.TempDir()
baseB, _, err := Sync(B, rootB, "s", "", "", "bob", 2, ig, guard, nil)
if err != nil {
t.Fatalf("B sync: %v", err)
}
writeFile(t, rootB, "f.txt", "v2-hub\n")
if _, _, err := Sync(B, rootB, "s", "", baseB, "bob", 3, ig, guard, nil); err != nil {
t.Fatalf("B sync2: %v", err)
}
// A's pre-pull hook simulates the user saving f.txt AFTER manifest.Build has
// snapshotted `ours` but BEFORE writeEntry applies the hub version.
hookDir := hooks.Dir(rootA)
if err := os.MkdirAll(hookDir, 0o755); err != nil {
t.Fatal(err)
}
hookScript := "#!/usr/bin/env bash\nprintf 'USER-EDIT-IN-WINDOW\\n' > \"$DEVBOX_MOUNT/f.txt\"\n"
if err := os.WriteFile(filepath.Join(hookDir, "pre-pull"), []byte(hookScript), 0o755); err != nil {
t.Fatal(err)
}
hk := hooks.New(rootA, "s", "alice", srv.URL)
pr, err := Pull(A, rootA, "s", "", snapA, "alice", 4, ig, guard, hk)
if err != nil {
t.Fatalf("A pull: %v", err)
}
t.Logf("pull: written=%v deleted=%v conflicts=%v skipped=%v", pr.Written, pr.Deleted, pr.Conflicts, pr.Skipped)
got := readFile(t, rootA, "f.txt")
entries, _ := os.ReadDir(rootA)
var conflictFiles []string
for _, e := range entries {
if strings.Contains(e.Name(), ".conflict-") {
conflictFiles = append(conflictFiles, e.Name())
}
}
t.Logf("f.txt=%q conflictFiles=%v", got, conflictFiles)
// The user's bytes must survive somewhere: either f.txt still holds them,
// or a conflict copy does. If neither, the edit was destroyed.
if got == "USER-EDIT-IN-WINDOW\n" {
return // edit survived in place
}
for _, cf := range conflictFiles {
if readFile(t, rootA, cf) == "USER-EDIT-IN-WINDOW\n" {
return // preserved as conflict copy
}
}
t.Fatalf("DATA LOSS CONFIRMED: local edit made during pull window destroyed — f.txt=%q, conflicts=%v (bytes exist in no file and no snapshot)", got, conflictFiles)
}