From e44d66bbe83f1cbfc17637c158868842b6d8c6da Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:32:50 +0000 Subject: [PATCH 1/8] fix(store): reject stale game save-path overwrites at SQL time --- internal/store/games.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/internal/store/games.go b/internal/store/games.go index c6414b74..385205d4 100644 --- a/internal/store/games.go +++ b/internal/store/games.go @@ -147,10 +147,27 @@ func (s *Store) ListGames() ([]Game, error) { return games, nil } -// UpdateGame overwrites the mutable fields of a game (settings/patch-style -// update — id, save_path are updatable too since the UI allows relocating -// a tracked save folder). +// ErrGamePathChanged means the path at SQL commit differs from the path +// observed by a caller. A stale metadata edit must not restore an old path. +var ErrGamePathChanged = errors.New("game save path changed; refresh and retry") + +// UpdateGame changes metadata only while its observed path is still current. +// Intentional relocation must use UpdateGamePathIf after native admission. func (s *Store) UpdateGame(g Game) error { + return s.updateGameAtPath(g, g.SavePath) +} + +// UpdateGamePathIf changes a path only while its expected old value remains +// current. The caller is responsible for native writer/topology admission. +func (s *Store) UpdateGamePathIf(g Game, expectedOldPath string) error { + return s.updateGameAtPath(g, expectedOldPath) +} + +func (s *Store) updateGameAtPath(g Game, expectedOldPath string) error { + arg := struct { + Game + ExpectedSavePath string `db:"expected_save_path"` + }{Game: g, ExpectedSavePath: expectedOldPath} res, err := s.db.NamedExec(` UPDATE games SET name = :name, @@ -165,11 +182,15 @@ func (s *Store) UpdateGame(g Game) error { last_manifest_hash = :last_manifest_hash, sync_ignore = :sync_ignore, sync_device_settings = :sync_device_settings - WHERE id = :id`, g) + WHERE id = :id AND save_path = :expected_save_path`, arg) if err != nil { return fmt.Errorf("update game %s: %w", g.ID, err) } - return checkRowAffected(res) + n, err := res.RowsAffected() + if err != nil { return err } + if n == 1 { return nil } + if _, err := s.GetGame(g.ID); err != nil { return err } + return ErrGamePathChanged } // SetLastManifestHash records the manifest hash captured at auto-snapshot From fe020a6a876b2c8743ead9aa05e683a0220f3b27 Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:33:07 +0000 Subject: [PATCH 2/8] fix(path): pass expected old path through internal/api/routes.go --- internal/api/routes.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/api/routes.go b/internal/api/routes.go index 465439af..7f543242 100644 --- a/internal/api/routes.go +++ b/internal/api/routes.go @@ -482,8 +482,18 @@ func (s *Server) handleUpdateGame(w http.ResponseWriter, r *http.Request) { game.LastManifestHash = s.Daemon.LastSnapshotHashUnder(game, oldIgnore) } - if err := s.Daemon.Store.UpdateGame(game); err != nil { - writeError(w, notFoundToStatus(err), err.Error()) + var updateErr error + if game.SavePath != oldSavePath { + updateErr = s.Daemon.Store.UpdateGamePathIf(game, oldSavePath) + } else { + updateErr = s.Daemon.Store.UpdateGame(game) + } + if updateErr != nil { + if errors.Is(updateErr, store.ErrGamePathChanged) { + writeError(w, http.StatusConflict, updateErr.Error()) + } else { + writeError(w, notFoundToStatus(updateErr), updateErr.Error()) + } return } if game.Name != oldName { From c312bb7981d5a5af0341f7caf8902b47776bef66 Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:33:14 +0000 Subject: [PATCH 3/8] fix(path): pass expected old path through internal/cliapp/commands_games.go --- internal/cliapp/commands_games.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/cliapp/commands_games.go b/internal/cliapp/commands_games.go index 95be9ed1..eef44c2e 100644 --- a/internal/cliapp/commands_games.go +++ b/internal/cliapp/commands_games.go @@ -34,6 +34,7 @@ func cmdGame(d *daemon.Daemon, args []string) int { if err != nil { return fail(asJSON, unknownGameError(d, gameID, err)) } + originalPath := game.SavePath switch key { case "name": @@ -78,7 +79,14 @@ func cmdGame(d *daemon.Daemon, args []string) int { return fail(asJSON, fmt.Errorf("unknown setting %q\n\n%s", key, gameUsage)) } - if err := d.Store.UpdateGame(game); err != nil { + if key == "path" { + // This caller supplies the path it read, so concurrent relocation is + // rejected rather than unintentionally overwritten. + err = d.Store.UpdateGamePathIf(game, originalPath) + } else { + err = d.Store.UpdateGame(game) + } + if err != nil { return fail(asJSON, err) } // Re-watch so a path or auto-sync change takes effect immediately rather From e5320d22cf4a03f6c36a62acb98b9962bd9180fb Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:33:22 +0000 Subject: [PATCH 4/8] fix(path): pass expected old path through internal/daemon/missing.go --- internal/daemon/missing.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/daemon/missing.go b/internal/daemon/missing.go index e9aa0690..cadf1222 100644 --- a/internal/daemon/missing.go +++ b/internal/daemon/missing.go @@ -191,7 +191,7 @@ func (d *Daemon) AcceptRelocation(gameID, path string) error { } old := game.SavePath game.SavePath = newPath - if err := d.Store.UpdateGame(game); err != nil { + if err := d.Store.UpdateGamePathIf(game, old); err != nil { return fmt.Errorf("could not switch %q to %s: %w", game.Name, newPath, err) } d.missingMu.Lock() From 2959bed65c3837a29129c616b33bb432277c87e7 Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:33:40 +0000 Subject: [PATCH 5/8] test(store): prove stale metadata cannot resurrect an old path --- .../store/game_stale_path_admission_test.go | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 internal/store/game_stale_path_admission_test.go diff --git a/internal/store/game_stale_path_admission_test.go b/internal/store/game_stale_path_admission_test.go new file mode 100644 index 00000000..bbd701ec --- /dev/null +++ b/internal/store/game_stale_path_admission_test.go @@ -0,0 +1,32 @@ +package store + +import ( + "errors" + "testing" +) + +func TestMetadataCannotResurrectPriorSavePath(t *testing.T) { + s := openTestStore(t) + if err := s.CreateGame(Game{ID: "stale", Name: "Original", SavePath: "/old"}); err != nil { t.Fatal(err) } + stale, err := s.GetGame("stale") + if err != nil { t.Fatal(err) } + moved := stale + moved.SavePath = "/new" + if err := s.UpdateGamePathIf(moved, "/old"); err != nil { t.Fatal(err) } + stale.Name = "Renamed" + if err := s.UpdateGame(stale); !errors.Is(err, ErrGamePathChanged) { t.Fatalf("stale metadata mutation: %v", err) } + actual, err := s.GetGame("stale") + if err != nil { t.Fatal(err) } + if actual.SavePath != "/new" || actual.Name != "Original" { t.Fatalf("stale metadata changed record: %#v", actual) } +} + +func TestIntentionalPathMoveNeedsExpectedPreviousPath(t *testing.T) { + s := openTestStore(t) + if err := s.CreateGame(Game{ID: "stale", Name: "Original", SavePath: "/old"}); err != nil { t.Fatal(err) } + g, err := s.GetGame("stale") + if err != nil { t.Fatal(err) } + g.SavePath = "/new" + if err := s.UpdateGame(g); !errors.Is(err, ErrGamePathChanged) { t.Fatalf("unadmitted path change accepted: %v", err) } + if err := s.UpdateGamePathIf(g, "/other"); !errors.Is(err, ErrGamePathChanged) { t.Fatalf("stale expected old accepted: %v", err) } + if err := s.UpdateGamePathIf(g, "/old"); err != nil { t.Fatal(err) } +} From e73f48971c9b51bb6b4f3cfbdd098e4de4f493ac Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 19:26:10 +0000 Subject: [PATCH 6/8] test: use explicit expected-old path for intentional relocation fixtures --- internal/daemon/relocate_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/daemon/relocate_test.go b/internal/daemon/relocate_test.go index 603d2fc5..e8b75e90 100644 --- a/internal/daemon/relocate_test.go +++ b/internal/daemon/relocate_test.go @@ -162,8 +162,9 @@ func TestRelocateToOtherDrive_AnOfferIsStaleOnceTheFolderIsSetByHand(t *testing. byHand := t.TempDir() g, _ := d.Store.GetGame("g") + previousPath := g.SavePath g.SavePath = byHand - if err := d.Store.UpdateGame(g); err != nil { + if err := d.Store.UpdateGamePathIf(g, previousPath); err != nil { t.Fatal(err) } From 59985ba4bed9aa6d946b8bec1bb34245979060a8 Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 19:26:17 +0000 Subject: [PATCH 7/8] test: use explicit expected-old path for intentional relocation fixtures --- internal/snapshot/snapshot_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/snapshot/snapshot_test.go b/internal/snapshot/snapshot_test.go index d570c8c8..aa018181 100644 --- a/internal/snapshot/snapshot_test.go +++ b/internal/snapshot/snapshot_test.go @@ -273,8 +273,9 @@ func TestSingleFileSaveMode(t *testing.T) { if err != nil { t.Fatal(err) } + previousPath := game.SavePath game.SavePath = saveFile - if err := env.store.UpdateGame(game); err != nil { + if err := env.store.UpdateGamePathIf(game, previousPath); err != nil { t.Fatal(err) } From 40df22ecd8557f73a6d882e327d3f282f98a85d9 Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 20:12:36 +0000 Subject: [PATCH 8/8] test(presets): cherry-pick deterministic no-index rebuild assertion from #26 --- internal/presets/index_nowait_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/presets/index_nowait_test.go b/internal/presets/index_nowait_test.go index b9bc53c3..b4a864c7 100644 --- a/internal/presets/index_nowait_test.go +++ b/internal/presets/index_nowait_test.go @@ -114,9 +114,15 @@ New Game: if err := os.Chtimes(yamlPath, downloaded, downloaded); err != nil { t.Fatal(err) } + // Force the rebuild to remain in progress for this assertion. On a fast + // runner it can otherwise finish before the nonblocking read returns, + // which is valid production behaviour and makes this test intermittent. + release, builds := slowBuilds(t, sc) if got := sc.loadManifestIndexNoWait(); len(got) != 0 { t.Fatalf("got %+v, want nothing until the index is built", got) } + waitUntil(t, "the background rebuild to start", func() bool { return builds.Load() > 0 }) + release() deadline := time.Now().Add(30 * time.Second) for { if _, err := os.Stat(indexPath); err == nil {