diff --git a/src/network/dht.zig b/src/network/dht.zig index 0f99ecb..dd58843 100644 --- a/src/network/dht.zig +++ b/src/network/dht.zig @@ -503,7 +503,14 @@ pub const DhtCoordinator = struct { pub fn init(allocator: Allocator, routing_table: *KademliaRoutingTable, local_id: NodeId) DhtCoordinator { // Generate Ed25519 identity keypair for signing const identity_keypair = Ed25519.KeyPair.generate(); - return .{ + + // SECURITY FIX: Initialize request_id with random value to prevent nonce collisions + // When multiple nodes start simultaneously, they would all start with request_id=1 + // causing the replay protection system to detect false positives (DuplicateNonce errors) + var random_start: u64 = undefined; + std.crypto.random.bytes(std.mem.asBytes(&random_start)); + + var dht = DhtCoordinator{ .allocator = allocator, .routing_table = routing_table, .local_id = local_id, @@ -515,6 +522,8 @@ pub const DhtCoordinator = struct { .peer_rate_limits = std.AutoHashMap(NodeId, PeerRateLimit).init(allocator), .discovered_peers = std.AutoHashMap(NodeId, DiscoveredPeer).init(allocator), }; + dht.next_request_id.store(random_start, .monotonic); + return dht; } /// Initialize with custom TOFU callback for peer verification (recommended for production)