From 202e13cddf2b11194fbd8d2987685dad62075c9d Mon Sep 17 00:00:00 2001 From: Taksh Kothari Date: Sun, 29 Nov 2026 06:05:51 +0530 Subject: [PATCH] fix: let a resubscribing central recover from the announce rate limit (#1669) * Document the subscription backoff ceiling Split the existing PR change into a smaller reviewable piece. Original change: Pin subscription recovery boundary and steady-state rate * Let subscription announcements recover after sustained retries Split the existing PR change into a smaller reviewable piece. Original change: Pin subscription recovery boundary and steady-state rate * Test subscription limiter recovery and admission bounds Split the existing PR change into a smaller reviewable piece. Original change: Pin subscription recovery boundary and steady-state rate --- .../BLE/BLESubscriptionAnnounceLimiter.swift | 12 +++- bitchat/Services/TransportConfig.swift | 2 + .../BLESubscriptionAnnounceLimiterTests.swift | 60 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/bitchat/Services/BLE/BLESubscriptionAnnounceLimiter.swift b/bitchat/Services/BLE/BLESubscriptionAnnounceLimiter.swift index 9846e22b..a0129224 100644 --- a/bitchat/Services/BLE/BLESubscriptionAnnounceLimiter.swift +++ b/bitchat/Services/BLE/BLESubscriptionAnnounceLimiter.swift @@ -41,8 +41,18 @@ struct BLESubscriptionAnnounceLimiter { existing.currentBackoffSeconds * TransportConfig.bleSubscriptionRateLimitBackoffFactor, TransportConfig.bleSubscriptionRateLimitMaxBackoffSeconds ) + // `lastAnnounceTime` deliberately keeps the time of the last *allowed* + // announce. It is the reference for both the backoff and + // `pruneStaleEntries`, so refreshing it on a rejection restarts the + // tracking window on every attempt — a central resubscribing faster + // than the maximum backoff could then never reach the window and + // stayed suppressed for as long as it kept trying. + // 9cd955a intentionally reset blocked attempts after the remediation + // in 99896bc; 193cfdc extracted the limiter without that rationale. + // Keeping the allowed timestamp preserves backoff while permitting + // recovery during a continuous retry stream. states[centralID] = State( - lastAnnounceTime: now, + lastAnnounceTime: existing.lastAnnounceTime, attemptCount: newAttemptCount, currentBackoffSeconds: newBackoff ) diff --git a/bitchat/Services/TransportConfig.swift b/bitchat/Services/TransportConfig.swift index 99c42f90..0ee35694 100644 --- a/bitchat/Services/TransportConfig.swift +++ b/bitchat/Services/TransportConfig.swift @@ -301,6 +301,8 @@ enum TransportConfig { // Prevents rapid enumeration attacks by rate-limiting announce responses static let bleSubscriptionRateLimitMinSeconds: TimeInterval = 2.0 // Minimum interval between announces per central static let bleSubscriptionRateLimitBackoffFactor: Double = 2.0 // Exponential backoff multiplier + // Keep the maximum backoff below the tracking window: otherwise pruning + // resets the budget before the capped backoff can elapse. static let bleSubscriptionRateLimitMaxBackoffSeconds: TimeInterval = 30.0 // Maximum backoff period static let bleSubscriptionRateLimitWindowSeconds: TimeInterval = 60.0 // Window for tracking subscription attempts static let bleSubscriptionRateLimitMaxAttempts: Int = 5 // Max attempts before extended cooldown diff --git a/bitchatTests/Services/BLESubscriptionAnnounceLimiterTests.swift b/bitchatTests/Services/BLESubscriptionAnnounceLimiterTests.swift index 4f6966c3..6b01e4da 100644 --- a/bitchatTests/Services/BLESubscriptionAnnounceLimiterTests.swift +++ b/bitchatTests/Services/BLESubscriptionAnnounceLimiterTests.swift @@ -58,4 +58,64 @@ struct BLESubscriptionAnnounceLimiterTests { #expect(limiter.decision(for: freshCentralID, now: afterWindow) == .allowed) #expect(limiter.trackedCentralCount == 1) } + + @Test("a central that keeps resubscribing recovers once the backoff elapses") + func flappingCentralRecoversAfterBackoff() { + // A legitimate central whose BLE link flaps resubscribes faster than the + // maximum backoff. Every rejection used to refresh `lastAnnounceTime`, + // which is the reference for both the backoff and `pruneStaleEntries`, + // so the elapsed time never grew: the central stayed suppressed for as + // long as it kept trying. It must instead be re-admitted once the + // capped backoff has passed since its last *allowed* announce. + var limiter = BLESubscriptionAnnounceLimiter() + let centralID = "central-flapping" + let start = Date(timeIntervalSince1970: 1_000) + + #expect(limiter.decision(for: centralID, now: start) == .allowed) + + let maxBackoff = TransportConfig.bleSubscriptionRateLimitMaxBackoffSeconds + var admittedAt: Double? + for step in stride(from: 1.0, through: maxBackoff * 2, by: 1.0) { + if limiter.decision(for: centralID, now: start.addingTimeInterval(step)) == .allowed { + admittedAt = step + break + } + } + + #expect(admittedAt == maxBackoff, "re-admission must occur exactly at the capped backoff") + } + + @Test("a hammering central is still held off for the whole backoff") + func flappingCentralIsNotAdmittedEarly() { + // The counterpart: relaxing the reference timestamp must not let a + // hammering central back in before the capped backoff has elapsed. + var limiter = BLESubscriptionAnnounceLimiter() + let centralID = "central-flapping" + let start = Date(timeIntervalSince1970: 2_000) + let maxBackoff = TransportConfig.bleSubscriptionRateLimitMaxBackoffSeconds + + #expect(limiter.decision(for: centralID, now: start) == .allowed) + + for step in stride(from: 1.0, to: maxBackoff, by: 1.0) { + let decision = limiter.decision(for: centralID, now: start.addingTimeInterval(step)) + #expect(decision != .allowed, "admitted early at +\(step)s") + } + } + + @Test("continuous retries admit exactly once per capped backoff") + func continuousRetriesPreserveSteadyStateRate() { + var limiter = BLESubscriptionAnnounceLimiter() + let start = Date(timeIntervalSince1970: 3_000) + var admissions: [Int] = [] + + #expect(TransportConfig.bleSubscriptionRateLimitMaxBackoffSeconds + < TransportConfig.bleSubscriptionRateLimitWindowSeconds) + for second in 0..<90 { + if limiter.decision(for: "central-flapping", now: start.addingTimeInterval(Double(second))) == .allowed { + admissions.append(second) + } + } + #expect(admissions == [0, 30, 60]) + } + }