Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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{}
Expand Down
44 changes: 44 additions & 0 deletions internal/p2p/cve_2026_103398_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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)
}
}
27 changes: 23 additions & 4 deletions internal/p2p/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
2 changes: 1 addition & 1 deletion internal/store/peer_folder_trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions internal/store/peer_folder_trust_migration_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading