mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 17:05:19 +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>
102 lines
3.1 KiB
Swift
102 lines
3.1 KiB
Swift
import BitFoundation
|
|
import Foundation
|
|
|
|
struct BLERouteForwardingPlan {
|
|
let shouldSuppressFloodRelay: Bool
|
|
let forwardPacket: BitchatPacket?
|
|
let nextHop: PeerID?
|
|
|
|
static let allowFloodRelay = BLERouteForwardingPlan(
|
|
shouldSuppressFloodRelay: false,
|
|
forwardPacket: nil,
|
|
nextHop: nil
|
|
)
|
|
|
|
static let suppressFloodRelay = BLERouteForwardingPlan(
|
|
shouldSuppressFloodRelay: true,
|
|
forwardPacket: nil,
|
|
nextHop: nil
|
|
)
|
|
|
|
static func forward(_ packet: BitchatPacket, to nextHop: PeerID) -> BLERouteForwardingPlan {
|
|
BLERouteForwardingPlan(
|
|
shouldSuppressFloodRelay: true,
|
|
forwardPacket: packet,
|
|
nextHop: nextHop
|
|
)
|
|
}
|
|
}
|
|
|
|
struct BLERouteForwardingPolicy {
|
|
static func plan(
|
|
for packet: BitchatPacket,
|
|
localPeerID: PeerID,
|
|
localRoutingData: Data?,
|
|
routingPeer: (Data) -> PeerID?,
|
|
isPeerConnected: (PeerID) -> Bool
|
|
) -> BLERouteForwardingPlan {
|
|
// REQUEST_SYNC is link-local: never forward it, on the flood path or
|
|
// the source-routed path. A crafted request with a route and TTL
|
|
// headroom must not be able to fan a full-store replay out to the next
|
|
// hop. Suppressing here also short-circuits the flood relay.
|
|
if packet.type == MessageType.requestSync.rawValue {
|
|
return .suppressFloodRelay
|
|
}
|
|
|
|
if PeerID(hexData: packet.recipientID) == localPeerID {
|
|
return .suppressFloodRelay
|
|
}
|
|
|
|
guard let route = packet.route, !route.isEmpty else {
|
|
return .allowFloodRelay
|
|
}
|
|
|
|
guard packet.ttl > 1 else {
|
|
return .suppressFloodRelay
|
|
}
|
|
|
|
guard let localRoutingData else {
|
|
return .allowFloodRelay
|
|
}
|
|
|
|
guard let localIndex = route.firstIndex(of: localRoutingData) else {
|
|
return forward(packet, toRouteData: route[0], routingPeer: routingPeer, isPeerConnected: isPeerConnected)
|
|
}
|
|
|
|
if localIndex == route.count - 1 {
|
|
guard let destinationPeer = PeerID(hexData: packet.recipientID),
|
|
isPeerConnected(destinationPeer) else {
|
|
return .allowFloodRelay
|
|
}
|
|
return .forward(relayed(packet), to: destinationPeer)
|
|
}
|
|
|
|
return forward(
|
|
packet,
|
|
toRouteData: route[localIndex + 1],
|
|
routingPeer: routingPeer,
|
|
isPeerConnected: isPeerConnected
|
|
)
|
|
}
|
|
|
|
private static func forward(
|
|
_ packet: BitchatPacket,
|
|
toRouteData routeData: Data,
|
|
routingPeer: (Data) -> PeerID?,
|
|
isPeerConnected: (PeerID) -> Bool
|
|
) -> BLERouteForwardingPlan {
|
|
guard let nextPeer = routingPeer(routeData),
|
|
isPeerConnected(nextPeer) else {
|
|
return .allowFloodRelay
|
|
}
|
|
|
|
return .forward(relayed(packet), to: nextPeer)
|
|
}
|
|
|
|
private static func relayed(_ packet: BitchatPacket) -> BitchatPacket {
|
|
var relayPacket = packet
|
|
relayPacket.ttl = packet.ttl - 1
|
|
return relayPacket
|
|
}
|
|
}
|