mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 11:25:20 +00:00
* Harden REQUEST_SYNC and stop gossip-sync re-send loops Two fixes from an end-to-end review of the sync path: Efficiency: the GCS filter (400B, p=7) covers ~355 packet IDs, but stores hold up to 1000 messages + 600 fragments + 200 files. Once a mesh accumulates more than the filter can cover, responders re-sent the entire older tail to every requester every round — ~120KB per pair per 30s during file transfers, dropped by dedup after the airtime was already burned. Requesters now stamp the dormant sinceTimestamp TLV with the oldest timestamp their filter covers, and responders skip older packets (announces exempt: they carry the signing keys needed to verify everything else). Periodic sync also sends one request per type schedule instead of a union filter, so fragment floods can't crowd messages out of the filter budget. Security: a ~40-byte unsigned REQUEST_SYNC with an empty filter could elicit a full store replay (~900KB) — an unauthenticated >10,000x amplification vector, repeatable in a tight loop and relayable with crafted TTL to fan the drain out of every reachable node. Requests now require ttl == 0, a valid signature from the claimed sender's announced signing key, and a matching link binding; REQUEST_SYNC is never relayed regardless of TTL; and responses are rate-limited per peer (8 per 30s sliding window, ~3x the legitimate cadence). Cross-platform: verified against bitchat-android — it signs REQUEST_SYNC and sends SYNC_TTL_HOPS = 0, so both gates hold; it neither sends nor honors sinceTimestamp yet, so mixed pairs keep today's behavior with no regression. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Address Codex review: enforce no-relay on route path, exact since-cursor Two P2 findings from Codex on the REQUEST_SYNC hardening: - Route-forwarding bypass: handleRequestSync's early return for a rejected (nonzero-TTL / unsigned) request still fell through to forwardAlongRouteIfNeeded, which relays any routed packet with ttl > 1 regardless of type. The no-relay invariant was only enforced on the flood path. BLERouteForwardingPolicy now suppresses REQUEST_SYNC outright, so a crafted request with a route and TTL headroom can't be forwarded to the next hop either. - Inexact since-cursor: GCSFilter.buildFilter trimmed by hash order when the encoding overflowed the byte budget, so the cursor (computed from the untrimmed prefix) could claim coverage of timestamps whose packets were dropped from the filter — re-sending exactly those every round. buildFilter now trims from the input tail (oldest, since candidates are newest-first) and reports includedCount; the cursor is derived from that, so the covered set is always a contiguous newest-prefix and the cursor is exact. Adds GCSFilter includedCount coverage (full vs trimmed), a route-forwarding test for REQUEST_SYNC, and makes the truncated-cursor test robust to trim variance. Full suite: 1029 tests pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
92 lines
3.9 KiB
Swift
92 lines
3.9 KiB
Swift
import Foundation
|
|
|
|
// RelayDecision encapsulates a single relay scheduling choice.
|
|
struct RelayDecision {
|
|
let shouldRelay: Bool
|
|
let newTTL: UInt8
|
|
let delayMs: Int
|
|
}
|
|
|
|
// RelayController centralizes flood control policy for relays.
|
|
struct RelayController {
|
|
static func decide(ttl: UInt8,
|
|
senderIsSelf: Bool,
|
|
recipientIsSelf: Bool = false,
|
|
isEncrypted: Bool,
|
|
isDirectedEncrypted: Bool,
|
|
isFragment: Bool,
|
|
isDirectedFragment: Bool,
|
|
isHandshake: Bool,
|
|
isAnnounce: Bool,
|
|
isRequestSync: Bool = false,
|
|
degree: Int,
|
|
highDegreeThreshold: Int) -> RelayDecision {
|
|
let ttlCap = min(ttl, TransportConfig.messageTTLDefault)
|
|
|
|
// REQUEST_SYNC is link-local: never relay it, even when a peer crafts
|
|
// one with TTL headroom to turn every reachable node into a responder.
|
|
if isRequestSync {
|
|
return RelayDecision(shouldRelay: false, newTTL: ttlCap, delayMs: 0)
|
|
}
|
|
|
|
// Suppress obvious non-relays
|
|
if ttlCap <= 1 || senderIsSelf || recipientIsSelf {
|
|
return RelayDecision(shouldRelay: false, newTTL: ttlCap, delayMs: 0)
|
|
}
|
|
|
|
// For session-critical or directed traffic, be deterministic and reliable
|
|
if isHandshake || isDirectedFragment || isDirectedEncrypted {
|
|
// Always relay with no TTL cap for these types
|
|
let newTTL = ttlCap &- 1
|
|
// Slight jitter to desynchronize without adding too much latency
|
|
// Tighter for faster multi-hop handshakes and directed DMs
|
|
let delayRange: ClosedRange<Int> = isHandshake ? 10...35 : 20...60
|
|
let delayMs = Int.random(in: delayRange)
|
|
return RelayDecision(shouldRelay: true, newTTL: newTTL, delayMs: delayMs)
|
|
}
|
|
|
|
if isFragment {
|
|
// Dense graphs clamp harder to contain full-fanout fragment floods;
|
|
// sparse graphs get full depth so media reaches as far as text.
|
|
let fragmentCap = degree >= highDegreeThreshold
|
|
? TransportConfig.bleFragmentRelayTtlCapDense
|
|
: TransportConfig.bleFragmentRelayTtlCap
|
|
let ttlLimit = min(ttlCap, fragmentCap)
|
|
guard ttlLimit > 1 else {
|
|
return RelayDecision(shouldRelay: false, newTTL: ttlLimit, delayMs: 0)
|
|
}
|
|
let newTTL = ttlLimit &- 1
|
|
let delayMs = Int.random(in: TransportConfig.bleFragmentRelayMinDelayMs...TransportConfig.bleFragmentRelayMaxDelayMs)
|
|
return RelayDecision(shouldRelay: true, newTTL: newTTL, delayMs: delayMs)
|
|
}
|
|
|
|
// TTL clamping for broadcast
|
|
// - Dense graphs: keep lower but still allow multi-hop bridging
|
|
// - Thin chains (degree <= 2): every hop counts and flood cost is
|
|
// minimal, so relay at full incoming depth
|
|
// - Announces get a bit more headroom
|
|
let ttlLimit: UInt8 = {
|
|
if degree >= highDegreeThreshold {
|
|
return max(UInt8(2), min(ttlCap, UInt8(5)))
|
|
}
|
|
if degree <= 2 {
|
|
return ttlCap
|
|
}
|
|
let preferred = UInt8(isAnnounce ? 7 : 6)
|
|
return max(UInt8(2), min(ttlCap, preferred))
|
|
}()
|
|
let newTTL = ttlLimit &- 1
|
|
|
|
// Wider jitter window to allow duplicate suppression to win more often
|
|
// For sparse graphs (<=2), relay quickly to avoid cancellation races
|
|
let delayMs: Int
|
|
switch degree {
|
|
case 0...2: delayMs = Int.random(in: 10...40)
|
|
case 3...5: delayMs = Int.random(in: 60...150)
|
|
case 6...9: delayMs = Int.random(in: 80...180)
|
|
default: delayMs = Int.random(in: 100...220)
|
|
}
|
|
return RelayDecision(shouldRelay: true, newTTL: newTTL, delayMs: delayMs)
|
|
}
|
|
}
|