mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-24 23:45:18 +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>
70 lines
3.1 KiB
Swift
70 lines
3.1 KiB
Swift
import Testing
|
|
import struct Foundation.Data
|
|
@testable import bitchat
|
|
|
|
struct GCSFilterTests {
|
|
@Test func buildFilterWithDuplicateIdsProducesStableEncoding() {
|
|
let id = Data(repeating: 0xAB, count: 16)
|
|
let ids = Array(repeating: id, count: 64)
|
|
|
|
let params = GCSFilter.buildFilter(ids: ids, maxBytes: 128, targetFpr: 0.01)
|
|
#expect(params.m >= 1)
|
|
|
|
let decoded = GCSFilter.decodeToSortedSet(p: params.p, m: params.m, data: params.data)
|
|
#expect(decoded.count <= 1)
|
|
}
|
|
|
|
@Test func bucketAvoidsZeroCandidate() {
|
|
let id = Data(repeating: 0x01, count: 16)
|
|
let bucket = GCSFilter.bucket(for: id, modulus: 2)
|
|
#expect(bucket != 0)
|
|
#expect(bucket < 2)
|
|
}
|
|
|
|
@Test func decodeRejectsOutOfRangeParameters() {
|
|
let junk = Data(repeating: 0xFF, count: 64)
|
|
#expect(GCSFilter.decodeToSortedSet(p: 0, m: 1000, data: junk).isEmpty)
|
|
#expect(GCSFilter.decodeToSortedSet(p: -1, m: 1000, data: junk).isEmpty)
|
|
#expect(GCSFilter.decodeToSortedSet(p: GCSFilter.maxP + 1, m: 1000, data: junk).isEmpty)
|
|
#expect(GCSFilter.decodeToSortedSet(p: 255, m: UInt32.max, data: junk).isEmpty)
|
|
#expect(GCSFilter.decodeToSortedSet(p: 8, m: 0, data: junk).isEmpty)
|
|
#expect(GCSFilter.decodeToSortedSet(p: 8, m: 1, data: junk).isEmpty)
|
|
}
|
|
|
|
@Test func decodeOfTruncatedDataReturnsOnlyCompleteValues() {
|
|
let ids = (0..<32).map { i in Data(repeating: UInt8(i), count: 16) }
|
|
let params = GCSFilter.buildFilter(ids: ids, maxBytes: 128, targetFpr: 0.01)
|
|
let full = GCSFilter.decodeToSortedSet(p: params.p, m: params.m, data: params.data)
|
|
let truncated = GCSFilter.decodeToSortedSet(p: params.p, m: params.m, data: params.data.prefix(params.data.count / 2))
|
|
#expect(truncated.count <= full.count)
|
|
// Truncation must not invent values that were not in the full set.
|
|
#expect(truncated.allSatisfy { full.contains($0) })
|
|
}
|
|
|
|
@Test func buildFilterReportsFullCoverageWhenBudgetFits() {
|
|
let ids = (0..<8).map { i in Data(repeating: UInt8(i), count: 16) }
|
|
let params = GCSFilter.buildFilter(ids: ids, maxBytes: 1024, targetFpr: 0.01)
|
|
#expect(params.includedCount == ids.count)
|
|
}
|
|
|
|
@Test func buildFilterTrimsTailWhenBudgetExceeded() {
|
|
// A tight byte budget can't hold every ID, so the encoder trims from
|
|
// the input tail and reports how many it actually covered.
|
|
let ids = (0..<200).map { i in
|
|
Data((0..<16).map { UInt8((i &* 31 &+ $0) & 0xFF) })
|
|
}
|
|
let params = GCSFilter.buildFilter(ids: ids, maxBytes: 32, targetFpr: 0.01)
|
|
#expect(params.includedCount > 0)
|
|
#expect(params.includedCount < ids.count)
|
|
#expect(params.data.count <= 32)
|
|
}
|
|
|
|
@Test func requestSyncPacketDecodeRejectsOversizedP() {
|
|
let valid = RequestSyncPacket(p: 8, m: 4096, data: Data([0x01, 0x02]))
|
|
#expect(RequestSyncPacket.decode(from: valid.encode()) != nil)
|
|
|
|
let oversized = RequestSyncPacket(p: 200, m: 4096, data: Data([0x01, 0x02]))
|
|
#expect(RequestSyncPacket.decode(from: oversized.encode()) == nil)
|
|
}
|
|
}
|