Merge 338304902f3f82975df4a8569a03bfd5b9e85d1b into 04995f68e470a18d1fc99f86fa326c2a87bab4f2

This commit is contained in:
Jozef Koval 2026-11-05 13:05:16 +00:00 committed by GitHub
commit 05d17757a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 1 deletions

View File

@ -59,6 +59,12 @@ struct BLENoiseSessionQueues {
}
}
#if DEBUG
func typedPayloadCount(for peerID: PeerID) -> Int {
typedPayloadsByPeerID[peerID]?.count ?? 0
}
#endif
@discardableResult
mutating func removeTypedPayload(transferId: String) -> Bool {
for peerID in Array(typedPayloadsByPeerID.keys) {

View File

@ -1333,7 +1333,12 @@ final class BLEService: NSObject {
)
guard expiration.expired else { return }
let policy = privateMediaSendPolicy(to: peerID)
if !expiration.deferredOutbound {
// The drain encrypts under the current session and never re-queues.
// With the handshake still in flight past this deadline, every
// encryption would fail and each parked ack, receipt or payload would
// be dropped; leave them for the handshake-completion drain instead.
if !expiration.deferredOutbound,
noiseService.hasEstablishedSession(with: peerID) {
sendPendingNoisePayloadsAfterHandshake(for: peerID)
}
completePrivateMediaPolicyResolution(expiration.completions, with: policy)
@ -3318,6 +3323,14 @@ extension BLEService {
)
}
/// Typed payloads parked for `peerID` behind a handshake, with or
/// without a transfer ID (acks and receipts carry none).
func _test_pendingTypedPayloadCount(for peerID: PeerID) -> Int {
onEngine {
pendingNoiseSessionQueues.typedPayloadCount(for: peerID)
}
}
func _test_privateMediaAdmissionEntryCount() -> Int {
privateMediaTransferAdmissions.count
}

View File

@ -862,6 +862,50 @@ struct PrivateMediaEndToEndTests {
bob._test_onOutboundPacket = nil
}
@Test
func proofTimeoutBeforeHandshakeLeavesParkedAckForTheSession() async throws {
let root = FileManager.default.temporaryDirectory
.appendingPathComponent("private-media-proof-timeout-parked-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: root) }
let alice = makeService(baseDirectory: root.appendingPathComponent("alice", isDirectory: true))
let bob = makeService(baseDirectory: root.appendingPathComponent("bob", isDirectory: true))
alice._test_seedConnectedPeer(
bob.myPeerID,
nickname: "Bob",
capabilities: .privateMedia,
noisePublicKey: bob.noiseStaticPublicKeyData()
)
// No session yet: resolving the media policy arms the 5 s proof
// watchdog and starts a handshake, which on a multi-hop mesh can
// take longer than that.
let recorder = PrivateMediaPolicyRecorder()
alice.resolvePrivateMediaSendPolicy(to: bob.myPeerID) { recorder.record($0) }
let registered = await TestHelpers.waitUntil(
{ alice._test_hasPendingPrivateMediaPolicyResolution(for: bob.myPeerID) },
timeout: TestConstants.longTimeout
)
#expect(registered)
#expect(!alice.canDeliverSecurely(to: bob.myPeerID))
// An ack owed to Bob parks behind that same handshake.
alice.sendDeliveryAck(for: "earlier-message-from-bob", to: bob.myPeerID)
await alice._test_drainNoiseMessagePipeline()
#expect(alice._test_pendingTypedPayloadCount(for: bob.myPeerID) == 1)
alice._test_forcePrivateMediaProofTimeout(for: bob.myPeerID)
let resolved = await TestHelpers.waitUntil(
{ recorder.snapshot() == .legacyRequiresConsent },
timeout: TestConstants.longTimeout
)
#expect(resolved)
// The watchdog settles the media policy. It must not consume the ack:
// with no session the ack cannot be encrypted, and a drained payload
// is never re-queued, so it would be lost for good.
#expect(alice._test_pendingTypedPayloadCount(for: bob.myPeerID) == 1)
}
@Test
func authenticatedFingerprintMismatchCannotPoisonCapabilityPin() async throws {
let root = FileManager.default.temporaryDirectory