diff --git a/bitchatTests/Services/BLEFileTransferHandlerTests.swift b/bitchatTests/Services/BLEFileTransferHandlerTests.swift index ffe828c4..9cd287a1 100644 --- a/bitchatTests/Services/BLEFileTransferHandlerTests.swift +++ b/bitchatTests/Services/BLEFileTransferHandlerTests.swift @@ -24,6 +24,7 @@ struct BLEFileTransferHandlerTests { var finishedIncomingFileDeliveries: [URL] = [] var lastSeenUpdates: [PeerID] = [] var deliveryAcks: [(messageID: String, peerID: PeerID)] = [] + var decodeFailures: [(PrivateMediaDecodeFailureReason, PeerID)] = [] var deliveredMessages: [BitchatMessage] = [] var shouldAcceptDelivery = true var deliveryOutcome = TransportEventDeliveryOutcome.accepted @@ -188,6 +189,9 @@ struct BLEFileTransferHandlerTests { } outcome = .accepted completion() + }, + reportPrivateMediaDecodeFailure: { reason, peerID in + recorder.decodeFailures.append((reason, peerID)) } ) return BLEFileTransferHandler(environment: environment) @@ -929,6 +933,21 @@ struct BLEFileTransferHandlerTests { #expect(recorder.deliveryAcks.count == 1) } + @Test + func blockedPrivateMediaDecodeFailureDoesNotPostSystemLine() { + let recorder = Recorder() + recorder.blockedPeers = [remotePeerID] + let handler = makeHandler(recorder: recorder) + let payload = Data([0x00]) + + #expect(handler.handlePrivatePayload( + payload, + from: remotePeerID, + timestamp: Date(timeIntervalSince1970: 1_234) + )) + #expect(recorder.decodeFailures.isEmpty) + } + @Test func decryptedPrivateFileOverPayloadCapIsRejectedBeforeQuotaOrDiskWrite() { let recorder = Recorder() diff --git a/bitchatTests/Services/PrivateMediaDecodeFailureReasonTests.swift b/bitchatTests/Services/PrivateMediaDecodeFailureReasonTests.swift new file mode 100644 index 00000000..c39f1ee0 --- /dev/null +++ b/bitchatTests/Services/PrivateMediaDecodeFailureReasonTests.swift @@ -0,0 +1,26 @@ +// +// PrivateMediaDecodeFailureReasonTests.swift +// bitchatTests +// +// This is free and unencumbered software released into the public domain. +// For more information, see +// + +import Testing +@testable import bitchat + +struct PrivateMediaDecodeFailureReasonTests { + @Test func mapsMalformedRejection() { + let reason = PrivateMediaDecodeFailureReason.from(.malformedPayload) + #expect(reason.logLabel == "malformed_payload") + #expect(!reason.localizedSystemMessage.isEmpty) + } + + @Test func mapsMagicMismatchWithMimeRawValue() { + let reason = PrivateMediaDecodeFailureReason.from( + .magicMismatch(mime: .jpeg, bytes: 12, prefixHex: "ff d8") + ) + #expect(reason.logLabel.contains("magic_mismatch")) + #expect(reason.localizedSystemMessage.contains("jpeg") || reason.localizedSystemMessage.contains("image")) + } +} diff --git a/bitchatTests/Services/PrivateMediaDecodeFailureThrottleTests.swift b/bitchatTests/Services/PrivateMediaDecodeFailureThrottleTests.swift new file mode 100644 index 00000000..41cd01c4 --- /dev/null +++ b/bitchatTests/Services/PrivateMediaDecodeFailureThrottleTests.swift @@ -0,0 +1,100 @@ +// +// PrivateMediaDecodeFailureThrottleTests.swift +// bitchatTests +// +// This is free and unencumbered software released into the public domain. +// For more information, see +// + +import BitFoundation +import Foundation +import Testing +@testable import bitchat + +/// Media decode failures are attacker-triggerable: an authenticated peer can +/// send malformed payloads as fast as the link allows. Without a rate limit, +/// one system line per payload floods the DM thread (#1518). +struct PrivateMediaDecodeFailureThrottleTests { + private let alice = PeerID(str: "aaaaaaaaaaaaaaaa") + private let mallory = PeerID(str: "bbbbbbbbbbbbbbbb") + private let t0 = Date(timeIntervalSince1970: 1_000_000) + + private func reason() -> PrivateMediaDecodeFailureReason { .malformedPayload } + + @Test("The first failure from a peer posts immediately") + func firstFailurePosts() { + var throttle = PrivateMediaDecodeFailureThrottle(window: 300) + #expect(throttle.record(from: alice, reason: reason(), now: t0) == .post(.malformedPayload)) + } + + @Test("A flood inside one window posts exactly one line") + func floodPostsOnlyOnce() { + var throttle = PrivateMediaDecodeFailureThrottle(window: 300) + var posted = 0 + for i in 0..<500 { + let outcome = throttle.record( + from: mallory, + reason: reason(), + now: t0.addingTimeInterval(Double(i) * 0.1) // 500 payloads in 50s + ) + if outcome != .suppress { posted += 1 } + } + #expect(posted == 1) + } + + @Test("Suppressed failures are reported on the next line, not dropped") + func suppressedCountSurfacesOnNextLine() { + var throttle = PrivateMediaDecodeFailureThrottle(window: 300) + _ = throttle.record(from: mallory, reason: reason(), now: t0) + for i in 1...9 { + _ = throttle.record(from: mallory, reason: reason(), now: t0.addingTimeInterval(Double(i))) + } + let afterWindow = throttle.record( + from: mallory, + reason: reason(), + now: t0.addingTimeInterval(301) + ) + #expect(afterWindow == .postWithSuppressed(.malformedPayload, suppressed: 9)) + } + + @Test("A quiet peer posts plainly, with no suppressed count") + func quietPeerPostsWithoutCount() { + var throttle = PrivateMediaDecodeFailureThrottle(window: 300) + _ = throttle.record(from: alice, reason: reason(), now: t0) + let next = throttle.record(from: alice, reason: reason(), now: t0.addingTimeInterval(600)) + #expect(next == .post(.malformedPayload)) + } + + @Test("Peers are throttled independently") + func peersAreIndependent() { + var throttle = PrivateMediaDecodeFailureThrottle(window: 300) + #expect(throttle.record(from: alice, reason: reason(), now: t0) == .post(.malformedPayload)) + // Mallory flooding must not silence a genuine failure from Alice. + #expect(throttle.record(from: mallory, reason: reason(), now: t0) == .post(.malformedPayload)) + #expect(throttle.record(from: alice, reason: reason(), now: t0.addingTimeInterval(1)) == .suppress) + #expect(throttle.record(from: mallory, reason: reason(), now: t0.addingTimeInterval(1)) == .suppress) + } + + @Test("The reason carried through is the one that surfaced") + func outcomeCarriesTheReason() { + var throttle = PrivateMediaDecodeFailureThrottle(window: 300) + let tooLarge = PrivateMediaDecodeFailureReason.payloadTooLarge(bytes: 99) + #expect(throttle.record(from: alice, reason: tooLarge, now: t0) == .post(tooLarge)) + } + + /// A hostile sender rotating peer IDs must not grow the throttle's state + /// without bound. + @Test("State does not accumulate across long-idle peers") + func stalePeersArePruned() { + var throttle = PrivateMediaDecodeFailureThrottle(window: 300) + for i in 0..<1_000 { + let rotating = PeerID(str: String(format: "%016x", i)) + _ = throttle.record(from: rotating, reason: reason(), now: t0) + } + // Long after every window closed, an old peer starts a fresh window + // rather than reporting a stale suppressed count. + let old = PeerID(str: String(format: "%016x", 0)) + let outcome = throttle.record(from: old, reason: reason(), now: t0.addingTimeInterval(10_000)) + #expect(outcome == .post(.malformedPayload)) + } +}