Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

fix(core): reject late friend intent during device pairing

baiyuze53ee171d357b
diff --git a/client-core/internal/peers/friends.go b/client-core/internal/peers/friends.go
index 372a9cc..a692792 100644
--- a/client-core/internal/peers/friends.go
+++ b/client-core/internal/peers/friends.go
@@ -584,10 +584,12 @@ func (m *Manager) handlePairIntent(from string, in *pb.PairIntent) {
// evaluateIntent 在两边的用途都知道以后:一致的加好友弹出确认框;不一致就作废这次配对。
func (m *Manager) evaluateIntent(p *pendingPair) {
m.mu.Lock()
- if p.peerIntent == "" || p.intentDone {
+ if m.pending[p.session] != p || p.peerIntent == "" ||
+ (p.intentDone && p.intent == p.peerIntent) {
m.mu.Unlock()
return
}
+ wasKnown := p.intentDone
p.intentDone = true
announce := false
switch {
@@ -602,7 +604,11 @@ func (m *Manager) evaluateIntent(p *pendingPair) {
delete(m.pending, p.session)
m.mismatched[p.session] = ErrIsFriendCode
}
- close(p.intentKnown)
+ // A delayed reply from an earlier attempt can initially report matching
+ // purposes. A later mismatch still invalidates this pending session.
+ if !wasKnown {
+ close(p.intentKnown)
+ }
m.mu.Unlock()
if p.mismatch != nil {
m.log.Info("配对用途不一致,作废", "peer", p.peer.ID, "本端", p.intent, "对方", p.peerIntent)
diff --git a/client-core/internal/peers/friends_test.go b/client-core/internal/peers/friends_test.go
index b4189d9..3b53b2b 100644
--- a/client-core/internal/peers/friends_test.go
+++ b/client-core/internal/peers/friends_test.go
@@ -3,6 +3,7 @@ package peers
import (
"context"
"errors"
+ "log/slog"
"net/http/httptest"
"strings"
"testing"
@@ -243,3 +244,30 @@ func TestAddFriendViaServer(t *testing.T) {
}
var _ = p2p.StateOffline
+
+func TestPairingRejectsFriendIntentAfterEarlierDeviceIntent(t *testing.T) {
+ p := &pendingPair{
+ session: "current-session", intent: lan.IntentDevice,
+ peerIntent: lan.IntentDevice, intentKnown: make(chan struct{}),
+ }
+ p.peer.ID = "peer"
+ m := &Manager{
+ pending: map[string]*pendingPair{p.session: p},
+ mismatched: make(map[string]error), log: slog.Default(),
+ }
+ // A reply from the preceding device-code attempt can arrive before the
+ // current friend-code attempt explains its purpose. It must not make the
+ // current session ignore the subsequent friend intent.
+ m.evaluateIntent(p)
+ p.peerIntent = lan.IntentFriend
+ m.evaluateIntent(p)
+ if !errors.Is(m.mismatched[p.session], ErrIsFriendCode) {
+ t.Fatal("a later friend intent must invalidate device pairing")
+ }
+ if _, ok := m.pending[p.session]; ok {
+ t.Fatal("mismatched pairing remains pending")
+ }
+ if err := m.ConfirmPairing(context.Background(), p.session, true); !errors.Is(err, ErrIsFriendCode) {
+ t.Fatalf("confirmation must reject the friend code: %v", err)
+ }
+}