From abce458186bee0c69236b2650b3a66a252202345 Mon Sep 17 00:00:00 2001 From: sivadaboi Date: Wed, 28 Oct 2026 00:48:50 +0530 Subject: [PATCH 1/4] Folder-trust table as migration 0044 --- ...0038_peer_folder_trust.sql => 0044_peer_folder_trust.sql} | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) rename internal/store/migrations/{0038_peer_folder_trust.sql => 0044_peer_folder_trust.sql} (75%) diff --git a/internal/store/migrations/0038_peer_folder_trust.sql b/internal/store/migrations/0044_peer_folder_trust.sql similarity index 75% rename from internal/store/migrations/0038_peer_folder_trust.sql rename to internal/store/migrations/0044_peer_folder_trust.sql index c918a139..7686aff3 100644 --- a/internal/store/migrations/0038_peer_folder_trust.sql +++ b/internal/store/migrations/0044_peer_folder_trust.sql @@ -6,8 +6,9 @@ -- (CVE-2026-103398). -- -- A table of its own rather than a column on peers, so an older version, --- which reads peers rows strictly, still opens the database. -CREATE TABLE peer_folder_trust ( +-- which reads peers rows strictly, still opens the database. IF NOT EXISTS: +-- dev builds made it as 0038_peer_folder_trust. +CREATE TABLE IF NOT EXISTS peer_folder_trust ( peer_id TEXT PRIMARY KEY, since_ms INTEGER NOT NULL ); From 4b0a18fa77053ba66ed562f9661d5af4b2fff9eb Mon Sep 17 00:00:00 2001 From: sivadaboi Date: Wed, 28 Oct 2026 00:48:52 +0530 Subject: [PATCH 2/4] Say an offer once, not at every sync --- internal/p2p/cve_2026_103398_test.go | 44 ++++++++++++++++++++++++++++ internal/p2p/routes.go | 25 ++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/internal/p2p/cve_2026_103398_test.go b/internal/p2p/cve_2026_103398_test.go index d9da824e..73502b17 100644 --- a/internal/p2p/cve_2026_103398_test.go +++ b/internal/p2p/cve_2026_103398_test.go @@ -1,17 +1,21 @@ package p2p import ( + "fmt" "io" "net/http" "net/http/httptest" "net/url" "os" "path/filepath" + "strconv" "strings" "testing" + "time" "github.com/go-chi/chi/v5" + "github.com/opensave/opensave/internal/e2ee" "github.com/opensave/opensave/internal/store" ) @@ -244,3 +248,43 @@ func TestTrustNeedsTheProvenDevice(t *testing.T) { t.Fatal("a folder was trusted for a request no signature proved") } } + +// A folder this device cannot vouch for is offered, and said so once: the +// other device asks at every sync, and the log was one warning every few +// seconds for as long as the offer stood. +func TestAnOfferIsLoggedOnce(t *testing.T) { + c := newCVEFixture(t) + var warned int + c.engine.Log = func(level, msg string) { + if strings.Contains(msg, "offered on Home instead") { + warned++ + } + } + // Three syncs from the other device, each a request of its own: signed + // afresh, as a device signs every request (askManifest reuses a nonce). + local, err := c.store.GetSettings() + if err != nil { + t.Fatal(err) + } + for i := 0; i < 3; i++ { + q := url.Values{"name": {"Totally A Game"}, "savePath": {c.secret}} + req := httptest.NewRequest(http.MethodGet, "/api/p2p/manifest/offered-once?"+q.Encode(), nil) + nonce, at := fmt.Sprintf("offer-nonce-%d", i), time.Now().UnixMilli() + req.Header.Set(lanAuthPeerHeader, c.peer.ID) + req.Header.Set(lanAuthNonceHeader, nonce) + req.Header.Set(lanAuthTimeHeader, strconv.FormatInt(at, 10)) + req.Header.Set(lanAuthHeader, e2ee.RequestMAC(c.authKey, c.peer.ID, local.NodeID, req.URL.RequestURI(), http.MethodGet, nil, nonce, at)) + req.RemoteAddr = "192.0.2.10:40000" + rec := httptest.NewRecorder() + c.router.ServeHTTP(rec, req) + if rec.Code != http.StatusNotFound { + t.Fatalf("request %d: %d %s, want the 404 an offer answers", i, rec.Code, rec.Body.String()) + } + } + if warned != 1 { + t.Errorf("the offer was logged %d times over three requests, want once", warned) + } + if offered, _ := c.store.ListOfferedGames(); len(offered) != 1 { + t.Errorf("offers: %+v, want the one", offered) + } +} diff --git a/internal/p2p/routes.go b/internal/p2p/routes.go index 8f5ada63..1d2e216d 100644 --- a/internal/p2p/routes.go +++ b/internal/p2p/routes.go @@ -615,9 +615,13 @@ func (e *Engine) ensureManifestGame(gameID string, q manifestGameQuery, peerID, e.Log("info", fmt.Sprintf("tracking %q at %s, the folder a device you let choose folders named", q.Name, logging.Quote(localPath))) } if !known { - e.Log("warn", fmt.Sprintf( - "a paired device asked to sync %q at %s, which this device does not know as a save folder — "+ - "it is offered on Home instead, for you to place", q.Name, logging.Quote(localPath))) + // Said once, when the offer is new: the other device asks again at + // every sync, every few seconds, for as long as the offer stands. + if !e.alreadyOffered(gameID, peerID) { + e.Log("warn", fmt.Sprintf( + "a paired device asked to sync %q at %s, which this device does not know as a save folder — "+ + "it is offered on Home instead, for you to place", q.Name, logging.Quote(localPath))) + } return e.offerGame(gameID, peerID, q) } @@ -665,6 +669,21 @@ func (e *Engine) ensureManifestGame(gameID string, q manifestGameQuery, peerID, // this device, and tells the peer this device is waiting rather than that the // game does not exist: the same answer, which the peer cannot see past, is // given for a game deliberately untracked. +// alreadyOffered reports whether this device already offers the game as one +// that device syncs. +func (e *Engine) alreadyOffered(gameID, peerID string) bool { + offers, err := e.Store.OfferedGame(gameID) + if err != nil { + return false + } + for _, o := range offers { + if o.PeerID == peerID { + return true + } + } + return false +} + func (e *Engine) offerGame(gameID, peerID string, q manifestGameQuery) (store.Game, error) { if err := e.Store.RecordOfferedGame(store.OfferedGame{ GameID: gameID, PeerID: peerID, Name: q.Name, From 81c91139ded51bbbf30e69d6f212be6b78a74a63 Mon Sep 17 00:00:00 2001 From: sivadaboi Date: Wed, 28 Oct 2026 00:53:06 +0530 Subject: [PATCH 3/4] Set the trust flag after the peer entry is built --- internal/api/server.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/api/server.go b/internal/api/server.go index 9353b127..94663ed7 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -99,6 +99,10 @@ func (s *Server) peersPayload() map[string]any { // "update from this device" when a peer runs a newer build. entry := struct { store.Peer + // Whether this device may choose the folders of games it syncs + // here (Devices; migration 0044). + ChoosesFolders bool `json:"choosesFolders"` + AppVersion string `json:"appVersion,omitempty"` BuildTimeMs int64 `json:"buildTimeMs,omitempty"` HasNewerBuild bool `json:"hasNewerBuild,omitempty"` @@ -107,15 +111,13 @@ func (s *Server) peersPayload() map[string]any { // differs per pairing and the reader cannot work out which case // they are in from a general statement. p2p.PeerProtection - // Whether this device may choose the folders of games it syncs - // here (Devices; migration 0038). - ChoosesFolders bool `json:"choosesFolders"` - }{Peer: p, PeerProtection: s.Daemon.P2P.PeerProtection(p), ChoosesFolders: s.Daemon.Store.PeerChoosesFolders(p.ID)} + }{Peer: p, PeerProtection: s.Daemon.P2P.PeerProtection(p)} if b, ok := builds[p.ID]; ok { entry.AppVersion = b.AppVersion entry.BuildTimeMs = b.BuildTimeMs entry.HasNewerBuild = version.NewerThanLocal(b.AppVersion, b.BuildTimeMs) } + entry.ChoosesFolders = s.Daemon.Store.PeerChoosesFolders(p.ID) peerMap[p.ID] = entry } discovered := []any{} From fcfe98b041277f6bce91d4259af85fd39cffc654 Mon Sep 17 00:00:00 2001 From: Omar Date: Sat, 7 Nov 2026 16:07:59 +0530 Subject: [PATCH 4/4] Folder trust: a database from a build with migration 0038 opens The table is migration 0044 now, with IF NOT EXISTS, so a database that made it as 0038 keeps it and what it holds. A test opens such a database. --- internal/p2p/routes.go | 2 +- internal/store/peer_folder_trust.go | 2 +- .../store/peer_folder_trust_migration_test.go | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 internal/store/peer_folder_trust_migration_test.go diff --git a/internal/p2p/routes.go b/internal/p2p/routes.go index 1d2e216d..690fa50b 100644 --- a/internal/p2p/routes.go +++ b/internal/p2p/routes.go @@ -497,7 +497,7 @@ func (e *Engine) backfillCover(game store.Game, q manifestGameQuery) store.Game // set to ask before tracking, to record who is waiting; empty is tolerated // (an unidentified caller simply produces an offer with no named device). // chooser is the device a signature proved made the request, or "": the only -// one whose naming of a folder may be trusted (Devices, migration 0038). +// one whose naming of a folder may be trusted (Devices, migration 0044). func (e *Engine) ensureManifestGame(gameID string, q manifestGameQuery, peerID, chooser string) (store.Game, error) { if game, err := e.Store.GetGame(gameID); err == nil { return e.backfillCover(game, q), nil diff --git a/internal/store/peer_folder_trust.go b/internal/store/peer_folder_trust.go index ae6c813d..024b22f2 100644 --- a/internal/store/peer_folder_trust.go +++ b/internal/store/peer_folder_trust.go @@ -6,7 +6,7 @@ import ( ) // SetPeerChoosesFolders lets a paired device choose the folders of games it -// syncs to this one, or stops it. See migration 0038. +// syncs to this one, or stops it. See migration 0044. func (s *Store) SetPeerChoosesFolders(peerID string, allowed bool) error { if !allowed { if _, err := s.db.Exec(`DELETE FROM peer_folder_trust WHERE peer_id = ?`, peerID); err != nil { diff --git a/internal/store/peer_folder_trust_migration_test.go b/internal/store/peer_folder_trust_migration_test.go new file mode 100644 index 00000000..34af779f --- /dev/null +++ b/internal/store/peer_folder_trust_migration_test.go @@ -0,0 +1,34 @@ +package store + +import ( + "path/filepath" + "testing" +) + +// A database from a build that made the folder-trust table as migration 0038 +// opens, keeping what it holds, now that the table is migration 0044. +func TestFolderTrustTableFromAnEarlierBuildOpens(t *testing.T) { + path := filepath.Join(t.TempDir(), "opensave.db") + s, err := Open(path) + if err != nil { + t.Fatal(err) + } + if _, err := s.db.Exec(`INSERT INTO peer_folder_trust (peer_id, since_ms) VALUES ('peer-1', 1)`); err != nil { + t.Fatal(err) + } + if _, err := s.db.Exec(`UPDATE schema_migrations SET name = '0038_peer_folder_trust.sql' WHERE name = '0044_peer_folder_trust.sql'`); err != nil { + t.Fatal(err) + } + if err := s.Close(); err != nil { + t.Fatal(err) + } + + s, err = Open(path) + if err != nil { + t.Fatalf("a database from such a build no longer opens: %v", err) + } + defer s.Close() + if !s.PeerChoosesFolders("peer-1") { + t.Error("the trust it held was lost") + } +}