From 348e0b394f75d4ab2ba4759b5d4f8cdbb9eac0b6 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Fri, 6 Nov 2026 03:00:46 +0800 Subject: [PATCH] fix(sync): ignore older announces from peers (#1834) --- bitchat/Services/BLE/BLEAnnounceHandler.swift | 20 +++++++-- bitchat/Services/BLE/BLEPeerRegistry.swift | 21 ++++++++- bitchat/Services/BLE/BLEService.swift | 13 ++++-- bitchat/Services/MeshTopologyTracker.swift | 24 +++++++++- bitchat/Sync/GossipSyncManager.swift | 9 +++- bitchatTests/GossipSyncManagerTests.swift | 45 +++++++++++++++++++ .../Services/BLEAnnounceHandlerTests.swift | 18 ++++---- .../Services/BLEPeerRegistryTests.swift | 31 +++++++++++++ .../Services/MeshTopologyTrackerTests.swift | 19 ++++++++ .../Simulation/AnnounceOrderReproTests.swift | 43 ++++++++++++++++++ 10 files changed, 221 insertions(+), 22 deletions(-) create mode 100644 bitchatTests/Simulation/AnnounceOrderReproTests.swift diff --git a/bitchat/Services/BLE/BLEAnnounceHandler.swift b/bitchat/Services/BLE/BLEAnnounceHandler.swift index d09bbb4b..59723602 100644 --- a/bitchat/Services/BLE/BLEAnnounceHandler.swift +++ b/bitchat/Services/BLE/BLEAnnounceHandler.swift @@ -45,13 +45,14 @@ struct BLEAnnounceHandlerEnvironment { _ peerID: PeerID, _ announcement: AnnouncementPacket, _ isConnected: Bool, - _ now: Date + _ now: Date, + _ packetTimestamp: UInt64 ) -> BLEPeerAnnounceUpdate? /// Debounced reconnect-log decision. /// Must only be called from inside `withRegistryBarrier`. let shouldEmitReconnectLog: (_ peerID: PeerID, _ now: Date) -> Bool /// Records verified direct-neighbor claims in the mesh topology. - let updateTopology: (_ peerID: PeerID, _ neighbors: [Data]) -> Void + let updateTopology: (_ peerID: PeerID, _ neighbors: [Data], _ packetTimestamp: UInt64) -> Void /// Persists the announced cryptographic identity for offline verification. let persistIdentity: (AnnouncementPacket) -> Void /// Announce-back dedup check. @@ -173,6 +174,7 @@ final class BLEAnnounceHandler { var isNewPeer = false var isReconnectedPeer = false + var wasOlderAnnouncement = false let directLinkState = env.linkState(peerID) let isDirectAnnounce = packet.ttl == env.messageTTL // A "direct" announce arriving on a link that another peer already @@ -214,7 +216,8 @@ final class BLEAnnounceHandler { peerID, announcement, hasPeripheralConnection || hasCentralSubscription || (isDirectAnnounce && !linkBoundToOtherPeer), - now + now, + packet.timestamp ) else { SecureLogger.warning("🚨 Registry refused announce for \(peerID.id.prefix(8))… — signing key differs from pinned key", category: .security) verifiedAnnounce = false @@ -222,6 +225,11 @@ final class BLEAnnounceHandler { isReconnectedPeer = false return } + if update.wasOlderAnnouncement { + wasOlderAnnouncement = true + verifiedAnnounce = false + return + } isNewPeer = update.isNewPeer isReconnectedPeer = update.wasDisconnected @@ -240,9 +248,13 @@ final class BLEAnnounceHandler { } } + if wasOlderAnnouncement { + return nil + } + // Update topology with verified neighbor claims (only for authenticated announces) if verifiedAnnounce, let neighbors = announcement.directNeighbors { - env.updateTopology(peerID, neighbors) + env.updateTopology(peerID, neighbors, packet.timestamp) } // Persist cryptographic identity and signing key for robust offline diff --git a/bitchat/Services/BLE/BLEPeerRegistry.swift b/bitchat/Services/BLE/BLEPeerRegistry.swift index 62113a59..51d6179c 100644 --- a/bitchat/Services/BLE/BLEPeerRegistry.swift +++ b/bitchat/Services/BLE/BLEPeerRegistry.swift @@ -15,12 +15,16 @@ struct BLEPeerInfo: Equatable { var capabilitiesWereExplicitlyAdvertised: Bool = false /// Rendezvous cell from the peer's announce when it advertises `.bridge`. var bridgeGeohash: String? + /// Sender timestamp of the newest accepted announce. Arrival time is not + /// sufficient here: a delayed sync reply can arrive after newer state. + var announceTimestamp: UInt64? = nil } struct BLEPeerAnnounceUpdate: Equatable { let isNewPeer: Bool let wasDisconnected: Bool let previousNickname: String? + let wasOlderAnnouncement: Bool = false } struct BLEPeerLinkPresence: Equatable { @@ -205,10 +209,22 @@ struct BLEPeerRegistry { isConnected: Bool, now: Date, capabilities: PeerCapabilities? = nil, - bridgeGeohash: String? = nil + bridgeGeohash: String? = nil, + announceTimestamp: UInt64? = nil ) -> BLEPeerAnnounceUpdate? { let existing = peers[peerID] + if let announceTimestamp, + let previousTimestamp = existing?.announceTimestamp, + announceTimestamp < previousTimestamp { + return BLEPeerAnnounceUpdate( + isNewPeer: false, + wasDisconnected: false, + previousNickname: existing?.nickname, + wasOlderAnnouncement: true + ) + } + if let pinnedSigningKey = existing?.signingPublicKey, let announcedSigningKey = signingPublicKey, pinnedSigningKey != announcedSigningKey { @@ -232,7 +248,8 @@ struct BLEPeerRegistry { lastSeen: now, capabilities: capabilities ?? [], capabilitiesWereExplicitlyAdvertised: capabilities != nil, - bridgeGeohash: bridgeGeohash + bridgeGeohash: bridgeGeohash, + announceTimestamp: announceTimestamp ?? existing?.announceTimestamp ) return update diff --git a/bitchat/Services/BLE/BLEService.swift b/bitchat/Services/BLE/BLEService.swift index a07e5d31..05e2014f 100644 --- a/bitchat/Services/BLE/BLEService.swift +++ b/bitchat/Services/BLE/BLEService.swift @@ -6483,7 +6483,7 @@ extension BLEService { withRegistryBarrier: { [weak self] body in self?.onEngine { body() } }, - upsertVerifiedAnnounce: { [weak self] peerID, announcement, isConnected, now in + upsertVerifiedAnnounce: { [weak self] peerID, announcement, isConnected, now, packetTimestamp in // Called from inside withRegistryBarrier; access registry directly. guard let self = self else { return BLEPeerAnnounceUpdate(isNewPeer: false, wasDisconnected: false, previousNickname: nil) @@ -6502,7 +6502,8 @@ extension BLEService { // preserved. now: now, capabilities: announcement.capabilities, - bridgeGeohash: announcement.bridgeGeohash + bridgeGeohash: announcement.bridgeGeohash, + announceTimestamp: packetTimestamp ) } }, @@ -6514,8 +6515,12 @@ extension BLEService { minimumInterval: TransportConfig.bleReconnectLogDebounceSeconds ) ?? false }, - updateTopology: { [weak self] peerID, neighbors in - self?.meshTopology.updateNeighbors(for: peerID.routingData, neighbors: neighbors) + updateTopology: { [weak self] peerID, neighbors, packetTimestamp in + _ = self?.meshTopology.updateNeighbors( + for: peerID.routingData, + neighbors: neighbors, + announceTimestamp: packetTimestamp + ) }, persistIdentity: { [weak self] announcement in self?.identityManager.upsertCryptographicIdentity( diff --git a/bitchat/Services/MeshTopologyTracker.swift b/bitchat/Services/MeshTopologyTracker.swift index 5596b5b1..f8c06fe6 100644 --- a/bitchat/Services/MeshTopologyTracker.swift +++ b/bitchat/Services/MeshTopologyTracker.swift @@ -10,6 +10,8 @@ final class MeshTopologyTracker { private var claims: [RoutingID: Set] = [:] // Last time we received an update from a node private var lastSeen: [RoutingID: Date] = [:] + // Sender timestamps prevent a delayed announce from restoring old routes. + private var lastAnnounceTimestamp: [RoutingID: UInt64] = [:] // Highest protocol version observed from each node's decoded packets. // Nodes absent from this map are assumed v1-only and are never used as // hops (or targets) for version-gated routes. @@ -23,19 +25,35 @@ final class MeshTopologyTracker { queue.sync(flags: .barrier) { self.claims.removeAll() self.lastSeen.removeAll() + self.lastAnnounceTimestamp.removeAll() self.observedVersions.removeAll() } } /// Update the topology with a node's self-reported neighbor list - func updateNeighbors(for sourceData: Data?, neighbors: [Data], at now: Date = Date()) { - guard let source = sanitize(sourceData) else { return } + @discardableResult + func updateNeighbors( + for sourceData: Data?, + neighbors: [Data], + at now: Date = Date(), + announceTimestamp: UInt64? = nil + ) -> Bool { + guard let source = sanitize(sourceData) else { return false } // Sanitize neighbors and exclude self-loops let validNeighbors = Set(neighbors.compactMap { sanitize($0) }).subtracting([source]) queue.sync(flags: .barrier) { + if let announceTimestamp, + let previousTimestamp = self.lastAnnounceTimestamp[source], + announceTimestamp < previousTimestamp { + return false + } self.claims[source] = validNeighbors self.lastSeen[source] = now + if let announceTimestamp { + self.lastAnnounceTimestamp[source] = announceTimestamp + } + return true } } @@ -61,6 +79,7 @@ final class MeshTopologyTracker { queue.sync(flags: .barrier) { self.claims.removeValue(forKey: peer) self.lastSeen.removeValue(forKey: peer) + self.lastAnnounceTimestamp.removeValue(forKey: peer) self.observedVersions.removeValue(forKey: peer) } } @@ -73,6 +92,7 @@ final class MeshTopologyTracker { for (peer, _) in stale { self.claims.removeValue(forKey: peer) self.lastSeen.removeValue(forKey: peer) + self.lastAnnounceTimestamp.removeValue(forKey: peer) } self.observedVersions = self.observedVersions.filter { $0.value.seenAt >= deadline } } diff --git a/bitchat/Sync/GossipSyncManager.swift b/bitchat/Sync/GossipSyncManager.swift index c54140f0..323eb426 100644 --- a/bitchat/Sync/GossipSyncManager.swift +++ b/bitchat/Sync/GossipSyncManager.swift @@ -279,13 +279,18 @@ final class GossipSyncManager { switch messageType { case .announce: + let sender = PeerID(hexData: packet.senderID) + // Arrival order is not sender order: sync replies can deliver an + // older announce after a newer one. Keep the newest known record. + if let existing = latestAnnouncementByPeer[sender], + existing.timestamp > packet.timestamp { + return + } guard isPacketFresh(packet) else { return } guard isAnnouncementFresh(packet) else { - let sender = PeerID(hexData: packet.senderID) removeState(for: sender) return } - let sender = PeerID(hexData: packet.senderID) latestAnnouncementByPeer[sender] = packet case .message: guard isBroadcastRecipient else { return } diff --git a/bitchatTests/GossipSyncManagerTests.swift b/bitchatTests/GossipSyncManagerTests.swift index 41f9f801..52880d18 100644 --- a/bitchatTests/GossipSyncManagerTests.swift +++ b/bitchatTests/GossipSyncManagerTests.swift @@ -445,6 +445,51 @@ struct GossipSyncManagerTests { #expect(delegate.packets.isEmpty) } + @Test func olderAnnounceDoesNotReplaceNewerGossipRecord() throws { + var config = GossipSyncManager.Config() + config.messageSyncIntervalSeconds = 0 + config.fragmentSyncIntervalSeconds = 0 + config.fileTransferSyncIntervalSeconds = 0 + config.prekeyBundleSyncIntervalSeconds = 0 + + let manager = GossipSyncManager(myPeerID: myPeerID, config: config, requestSyncManager: RequestSyncManager()) + let delegate = RecordingDelegate() + manager.delegate = delegate + + let sender = try #require(Data(hexString: "aabbccddeeff0011")) + let newerTimestamp = UInt64(Date().timeIntervalSince1970 * 1000) + let newer = BitchatPacket( + type: MessageType.announce.rawValue, + senderID: sender, + recipientID: nil, + timestamp: newerTimestamp, + payload: Data([0x02]), + signature: nil, + ttl: 1 + ) + let older = BitchatPacket( + type: MessageType.announce.rawValue, + senderID: sender, + recipientID: nil, + timestamp: newerTimestamp - 1, + payload: Data([0x01]), + signature: nil, + ttl: 1 + ) + + manager.onPublicPacketSeen(newer) + manager.onPublicPacketSeen(older) + manager._performMaintenanceSynchronously() + + let emptyFilter = GCSFilter.buildFilter(ids: [], maxBytes: 256, targetFpr: 0.01) + let request = RequestSyncPacket(p: emptyFilter.p, m: emptyFilter.m, data: emptyFilter.data, types: .announce) + manager.handleRequestSync(from: PeerID(str: "FFFFFFFFFFFFFFFF"), request: request) + manager._performMaintenanceSynchronously() + + let response = try #require(delegate.packets.first) + #expect(response.timestamp == newerTimestamp) + } + @Test func handleRequestSyncIsRateLimitedPerPeer() async throws { var config = GossipSyncManager.Config() config.seenCapacity = 5 diff --git a/bitchatTests/Services/BLEAnnounceHandlerTests.swift b/bitchatTests/Services/BLEAnnounceHandlerTests.swift index fdfdfa40..6261bb6c 100644 --- a/bitchatTests/Services/BLEAnnounceHandlerTests.swift +++ b/bitchatTests/Services/BLEAnnounceHandlerTests.swift @@ -56,7 +56,7 @@ struct BLEAnnounceHandlerTests { recorder.barrierCount += 1 body() }, - upsertVerifiedAnnounce: { peerID, announcement, isConnected, now in + upsertVerifiedAnnounce: { peerID, announcement, isConnected, now, _ in recorder.upsertCalls.append((peerID, announcement, isConnected, now)) return recorder.upsertResult }, @@ -64,7 +64,7 @@ struct BLEAnnounceHandlerTests { recorder.reconnectLogQueries.append(peerID) return recorder.shouldEmitReconnectLogResult }, - updateTopology: { peerID, neighbors in + updateTopology: { peerID, neighbors, _ in recorder.topologyUpdates.append((peerID, neighbors)) }, persistIdentity: { announcement in @@ -717,18 +717,19 @@ struct BLEAnnounceHandlerTests { linkState: { _ in (hasPeripheral: true, hasCentral: false) }, linkBoundToOtherPeer: { _, _ in false }, withRegistryBarrier: { body in body() }, - upsertVerifiedAnnounce: { peerID, announcement, isConnected, now in + upsertVerifiedAnnounce: { peerID, announcement, isConnected, now, packetTimestamp in box.registry.upsertVerifiedAnnounce( peerID: peerID, nickname: announcement.nickname, noisePublicKey: announcement.noisePublicKey, signingPublicKey: announcement.signingPublicKey, isConnected: isConnected, - now: now + now: now, + announceTimestamp: packetTimestamp ) }, shouldEmitReconnectLog: { _, _ in false }, - updateTopology: { _, _ in }, + updateTopology: { _, _, _ in }, persistIdentity: { announcement in box.persistedIdentities.append(announcement) }, @@ -832,18 +833,19 @@ struct BLEAnnounceHandlerTests { linkState: { _ in (hasPeripheral: true, hasCentral: false) }, linkBoundToOtherPeer: { _, _ in false }, withRegistryBarrier: { body in body() }, - upsertVerifiedAnnounce: { peerID, announcement, isConnected, now in + upsertVerifiedAnnounce: { peerID, announcement, isConnected, now, packetTimestamp in box.registry.upsertVerifiedAnnounce( peerID: peerID, nickname: announcement.nickname, noisePublicKey: announcement.noisePublicKey, signingPublicKey: announcement.signingPublicKey, isConnected: isConnected, - now: now + now: now, + announceTimestamp: packetTimestamp ) }, shouldEmitReconnectLog: { _, _ in false }, - updateTopology: { _, _ in }, + updateTopology: { _, _, _ in }, persistIdentity: { announcement in identityManager.upsertCryptographicIdentity( fingerprint: announcement.noisePublicKey.sha256Fingerprint(), diff --git a/bitchatTests/Services/BLEPeerRegistryTests.swift b/bitchatTests/Services/BLEPeerRegistryTests.swift index 204c8a3a..0f846db5 100644 --- a/bitchatTests/Services/BLEPeerRegistryTests.swift +++ b/bitchatTests/Services/BLEPeerRegistryTests.swift @@ -44,6 +44,37 @@ struct BLEPeerRegistryTests { #expect(registry.info(for: peerID)?.nickname == "alice-renamed") } + @Test("older announces cannot replace newer peer state") + func upsertVerifiedAnnounceRejectsOlderTimestamp() throws { + var registry = BLEPeerRegistry() + let peerID = PeerID(str: "1122334455667788") + let noiseKey = Data(repeating: 0x11, count: 32) + let firstSeen = Date(timeIntervalSince1970: 100) + + _ = registry.upsertVerifiedAnnounce( + peerID: peerID, + nickname: "bobby", + noisePublicKey: noiseKey, + signingPublicKey: nil, + isConnected: true, + now: firstSeen, + announceTimestamp: 200 + ) + let stale = try #require(registry.upsertVerifiedAnnounce( + peerID: peerID, + nickname: "bob", + noisePublicKey: noiseKey, + signingPublicKey: nil, + isConnected: true, + now: firstSeen.addingTimeInterval(1), + announceTimestamp: 100 + )) + + #expect(stale.wasOlderAnnouncement) + #expect(registry.info(for: peerID)?.nickname == "bobby") + #expect(registry.info(for: peerID)?.announceTimestamp == 200) + } + @Test("pinned signing key cannot be silently replaced by a later announce") func upsertVerifiedAnnounceRefusesToReplacePinnedSigningKey() throws { var registry = BLEPeerRegistry() diff --git a/bitchatTests/Services/MeshTopologyTrackerTests.swift b/bitchatTests/Services/MeshTopologyTrackerTests.swift index fa5d2a4d..c11d7ba5 100644 --- a/bitchatTests/Services/MeshTopologyTrackerTests.swift +++ b/bitchatTests/Services/MeshTopologyTrackerTests.swift @@ -29,6 +29,25 @@ struct MeshTopologyTrackerTests { #expect(route == []) } + @Test func olderAnnounceDoesNotReplaceNeighborClaims() throws { + let tracker = MeshTopologyTracker() + let source = try hex("0102030405060708") + let currentNeighbor = try hex("1112131415161718") + let staleNeighbor = try hex("2122232425262728") + + #expect(tracker.updateNeighbors( + for: source, + neighbors: [currentNeighbor], + announceTimestamp: 200 + )) + #expect(!tracker.updateNeighbors( + for: source, + neighbors: [staleNeighbor], + announceTimestamp: 100 + )) + #expect(tracker.adjacencySnapshot()[source] == [currentNeighbor]) + } + @Test func multiHopRouteComputation() throws { let tracker = MeshTopologyTracker() let a = try hex("0001020304050607") diff --git a/bitchatTests/Simulation/AnnounceOrderReproTests.swift b/bitchatTests/Simulation/AnnounceOrderReproTests.swift new file mode 100644 index 00000000..f61f2c2b --- /dev/null +++ b/bitchatTests/Simulation/AnnounceOrderReproTests.swift @@ -0,0 +1,43 @@ +import BitFoundation +import Foundation +import Testing +@testable import bitchat + +/// Repro for an older sync reply rolling back a peer's newer announcement. +@Suite(.serialized) +struct AnnounceOrderReproTests { + private static func nickname(of peer: PeerID, at node: SimulatedMesh.Node) -> String? { + node.service.currentPeerSnapshots().first { $0.peerID == peer }?.nickname + } + + private static func lastAnnounce(from index: Int, in mesh: SimulatedMesh, nickname: String) -> BitchatPacket? { + mesh.emittedPackets(from: index).last { + $0.type == MessageType.announce.rawValue + && AnnouncementPacket.decode(from: $0.payload)?.nickname == nickname + } + } + + @Test func olderAnnounceDoesNotOverwriteNewerOne() throws { + let mesh = SimulatedMesh() + let bob = mesh.addNode(nickname: "bob") + let xavier = mesh.addNode(nickname: "xavier") + // No links yet: bob's announces are captured, not delivered. + bob.service._test_resetAnnounceThrottle() + mesh.forceAnnounce(from: 0) + Thread.sleep(forTimeInterval: 0.01) // distinct millisecond timestamps + bob.service._test_resetAnnounceThrottle() + bob.service.setNickname("bobby") + mesh.pump() + let older = try #require(Self.lastAnnounce(from: 0, in: mesh, nickname: "bob")) + let newer = try #require(Self.lastAnnounce(from: 0, in: mesh, nickname: "bobby")) + #expect(older.timestamp < newer.timestamp) + + let link = BLEIngressLinkID.central(mesh.linkUUID(from: 0, at: 1)) + xavier.service._test_ingestFrame(newer, link: link) + xavier.service._test_fenceEngine() + xavier.service._test_ingestFrame(older, link: link) + xavier.service._test_fenceEngine() + + #expect(Self.nickname(of: bob.service.myPeerID, at: xavier) == "bobby") + } +}