Gate connected-link trust on a secure session; deny forged direct announces the connected shortcut

Residual gap after the rotation-heal containment (#1401): "verified
direct" announces prove the signature but not directness — TTL is
unsigned — so a malicious connected peer can replay a victim's fresh
announce with its TTL restored. When the victim has no live link, the
replayer's link rebinds to the victim's ID and reads as "connected",
and MessageRouter's connected fast-path then trusts it outright: every
DM stalls on a Noise handshake the replayer can never complete and is
silently lost while showing "sent".

Router-level trust gate (no wire change):
- Transport gains canDeliverSecurely(to:) — BLE answers with an
  established Noise session; Nostr keeps its prompt-delivery predicate;
  the protocol default forwards to canDeliverPromptly for transports
  without a forgeable link layer.
- MessageRouter.sendPrivate only trusts a connected link outright when
  it can deliver securely; otherwise it still sends (kicking the
  handshake on a genuine link) but retains a copy and hands a sealed
  copy to couriers, like the reachable path. flushOutbox gets the same
  gate so a flush over an insecure link resends instead of dropping the
  retained copy.

Presence hardening (defense in depth): a "direct" announce arriving on
a link already bound to a different peer no longer shortcuts the
claimed peer into "connected" — only real link state does. Genuine
first-contact and direct announces are unaffected; only the ambiguous
heal path loses the forgeable shortcut.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
jack
2026-07-09 11:55:32 +02:00
co-authored by Claude Opus 4.8
parent 021af3a22d
commit 18862102ea
10 changed files with 269 additions and 9 deletions
@@ -316,6 +316,76 @@ struct MessageRouterTests {
#expect(couriers.contains(favorite))
}
/// Residual gap after the rotation-heal containment: a replayed "direct"
/// announce (TTL is unsigned) can bind an absent victim's peer ID to the
/// replayer's link, leaving the victim "connected" on a link whose Noise
/// handshake can never complete. The connected fast-path must not trust
/// that outright: the send still goes out (a genuine link finishes the
/// handshake), but a copy is retained and a sealed copy goes to couriers
/// so nothing is silently lost.
@Test @MainActor
func sendPrivate_connectedWithoutSecureSessionRetainsAndDepositsWithCourier() async {
let victim = PeerID(str: "00000000000000aa")
let victimKey = Data(repeating: 0xBB, count: 32)
let courier = PeerID(str: "00000000000000cc")
let courierKey = Data(repeating: 0xCC, count: 32)
let transport = MockTransport()
transport.connectedPeers.insert(victim)
transport.connectedPeers.insert(courier)
transport.securePeers = [] // no established Noise session with anyone
transport.updatePeerSnapshots([Self.snapshot(courier, key: courierKey, verified: true)])
let router = MessageRouter(
transports: [transport],
courierDirectory: Self.directory(recipient: victim, recipientKey: victimKey)
)
router.sendPrivate("Hello", to: victim, recipientNickname: "Peer", messageID: "cs1")
// The send is still attempted (it kicks the handshake on a genuine
// link) but not trusted outright: a courier gets a sealed copy now
#expect(transport.sentPrivateMessages.map(\.messageID) == ["cs1"])
#expect(transport.sentCourierMessages.count == 1)
#expect(transport.sentCourierMessages.first?.messageID == "cs1")
#expect(transport.sentCourierMessages.first?.recipientNoiseKey == victimKey)
#expect(transport.sentCourierMessages.first?.couriers == [courier])
// and the retained copy keeps flushing until a delivery ack a
// flush over the insecure link must resend without dropping it.
router.flushOutbox(for: victim)
router.flushOutbox(for: victim)
#expect(transport.sentPrivateMessages.count == 3)
router.markDelivered("cs1")
router.flushOutbox(for: victim)
#expect(transport.sentPrivateMessages.count == 3)
}
/// With an established secure session the connected fast-path stays
/// exactly as before: trusted outright, no retained copy, no courier.
@Test @MainActor
func sendPrivate_connectedWithSecureSessionIsTrustedOutright() async {
let peerID = PeerID(str: "00000000000000ab")
let peerKey = Data(repeating: 0xAB, count: 32)
let courier = PeerID(str: "00000000000000cc")
let transport = MockTransport()
transport.connectedPeers.insert(peerID)
transport.connectedPeers.insert(courier)
transport.securePeers = [peerID]
transport.updatePeerSnapshots([Self.snapshot(courier, key: Data(repeating: 0xCC, count: 32), verified: true)])
let router = MessageRouter(
transports: [transport],
courierDirectory: Self.directory(recipient: peerID, recipientKey: peerKey)
)
router.sendPrivate("Hello", to: peerID, recipientNickname: "Peer", messageID: "cs2")
#expect(transport.sentPrivateMessages.map(\.messageID) == ["cs2"])
#expect(transport.sentCourierMessages.isEmpty)
router.flushOutbox(for: peerID)
#expect(transport.sentPrivateMessages.count == 1)
}
@Test @MainActor
func courierBecameAvailable_retriesDepositOnceWithoutDoubleBurn() async {
let recipient = PeerID(str: "00000000000000aa")