diff --git a/bitchat/Services/BLE/BLENoiseSessionQueues.swift b/bitchat/Services/BLE/BLENoiseSessionQueues.swift index 301e6bce..fc0f8923 100644 --- a/bitchat/Services/BLE/BLENoiseSessionQueues.swift +++ b/bitchat/Services/BLE/BLENoiseSessionQueues.swift @@ -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) { diff --git a/bitchat/Services/BLE/BLEService.swift b/bitchat/Services/BLE/BLEService.swift index a07e5d31..639041c7 100644 --- a/bitchat/Services/BLE/BLEService.swift +++ b/bitchat/Services/BLE/BLEService.swift @@ -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 } diff --git a/bitchatTests/EndToEnd/PrivateMediaEndToEndTests.swift b/bitchatTests/EndToEnd/PrivateMediaEndToEndTests.swift index 30f071f5..54de4a23 100644 --- a/bitchatTests/EndToEnd/PrivateMediaEndToEndTests.swift +++ b/bitchatTests/EndToEnd/PrivateMediaEndToEndTests.swift @@ -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