[DO-NOT-MERGE] proposed fix (thx to claude)

This commit is contained in:
2026-02-12 14:12:28 +01:00
parent 789897b51a
commit e745f849dc

View File

@@ -503,7 +503,14 @@ pub const DhtCoordinator = struct {
pub fn init(allocator: Allocator, routing_table: *KademliaRoutingTable, local_id: NodeId) DhtCoordinator { pub fn init(allocator: Allocator, routing_table: *KademliaRoutingTable, local_id: NodeId) DhtCoordinator {
// Generate Ed25519 identity keypair for signing // Generate Ed25519 identity keypair for signing
const identity_keypair = Ed25519.KeyPair.generate(); 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, .allocator = allocator,
.routing_table = routing_table, .routing_table = routing_table,
.local_id = local_id, .local_id = local_id,
@@ -515,6 +522,8 @@ pub const DhtCoordinator = struct {
.peer_rate_limits = std.AutoHashMap(NodeId, PeerRateLimit).init(allocator), .peer_rate_limits = std.AutoHashMap(NodeId, PeerRateLimit).init(allocator),
.discovered_peers = std.AutoHashMap(NodeId, DiscoveredPeer).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) /// Initialize with custom TOFU callback for peer verification (recommended for production)