diff --git a/src/mwp/types.zig b/src/mwp/types.zig index 2c462ef..190b260 100644 --- a/src/mwp/types.zig +++ b/src/mwp/types.zig @@ -11,6 +11,9 @@ pub const PROTOCOL_VERSION: u8 = 0x01; /// Magic bytes for hole punch packets pub const HOLE_PUNCH_MAGIC = [4]u8{ 'M', 'W', 'P', '1' }; +/// Maximum addresses per meeting publication (prevents malicious inflation) +const MAX_ADDRESSES: u8 = 32; + /// Default parameters pub const Defaults = struct { /// Time to live for publications (seconds) @@ -187,44 +190,46 @@ pub const MeetingPublication = struct { /// Compute hash of publication (for signing) pub fn computeHash(self: MeetingPublication) [32]u8 { - var buf: [1024]u8 = undefined; - var idx: usize = 0; + const Blake2b256 = std.crypto.hash.blake2.Blake2b256; + var hasher = Blake2b256.init(.{}); // Version - buf[idx] = PROTOCOL_VERSION; - idx += 1; + hasher.update(&[_]u8{PROTOCOL_VERSION}); // Peer ID - @memcpy(buf[idx..][0..32], &self.peer_id); - idx += 32; + hasher.update(&self.peer_id); // Address count - buf[idx] = @intCast(self.addresses.len); - idx += 1; + hasher.update(&[_]u8{@intCast(self.addresses.len)}); - // Addresses (simplified - just count for hash) + // Addresses for (self.addresses) |addr| { - const size = addr.serialize(buf[idx..]) catch 0; - idx += size; + var addr_buf: [19]u8 = undefined; // max: 1 type + 16 ipv6 + 2 port + const size = addr.serialize(&addr_buf) catch 0; + hasher.update(addr_buf[0..size]); } // Nonce - @memcpy(buf[idx..][0..32], &self.nonce); - idx += 32; + hasher.update(&self.nonce); // Timestamp - std.mem.writeInt(i64, buf[idx..][0..8], self.timestamp, .big); - idx += 8; + var ts_buf: [8]u8 = undefined; + std.mem.writeInt(i64, &ts_buf, self.timestamp, .big); + hasher.update(&ts_buf); // TTL - std.mem.writeInt(u32, buf[idx..][0..4], self.ttl, .big); - idx += 4; + var ttl_buf: [4]u8 = undefined; + std.mem.writeInt(u32, &ttl_buf, self.ttl, .big); + hasher.update(&ttl_buf); // Sequence - std.mem.writeInt(u64, buf[idx..][0..8], self.sequence, .big); - idx += 8; + var seq_buf: [8]u8 = undefined; + std.mem.writeInt(u64, &seq_buf, self.sequence, .big); + hasher.update(&seq_buf); - return blake2b256(buf[0..idx]); + var out: [32]u8 = undefined; + hasher.final(&out); + return out; } /// Verify signature @@ -314,6 +319,7 @@ pub const MeetingPublication = struct { // Addresses const addr_count = buf[idx]; idx += 1; + if (addr_count > MAX_ADDRESSES) return error.InvalidData; const addresses = try allocator.alloc(Address, addr_count); errdefer allocator.free(addresses); diff --git a/src/mwp_integration.zig b/src/mwp_integration.zig index 3f306dd..d4f3e18 100644 --- a/src/mwp_integration.zig +++ b/src/mwp_integration.zig @@ -57,7 +57,7 @@ pub const KeyPair = mwp_lib.KeyPair; /// Maximum allowed clock skew for peer publications (milliseconds). /// Publications with timestamps more than this far in the future are rejected /// to prevent a malicious peer from delaying rendezvous arbitrarily. -const MAX_TIMESTAMP_SKEW_MS: i64 = 60_000; // 60 seconds +const MAX_TIMESTAMP_SKEW_MS: i64 = 30_000; // 30 seconds /// Main MWP integration with zig-p2p pub const MWP = struct { diff --git a/tests/integration/test_mwp_connection.zig b/tests/integration/test_mwp_connection.zig index 7593215..54955ac 100644 --- a/tests/integration/test_mwp_connection.zig +++ b/tests/integration/test_mwp_connection.zig @@ -57,21 +57,21 @@ test "MWP: two P2P nodes complete hole punch on localhost" { std.debug.print("Server has no connected peer\n", .{}); return error.SkipZigTest; }; - pair.server.registerDhtNodeId(bob_dht_id, server_view_of_client) catch {}; + try pair.server.registerDhtNodeId(bob_dht_id, server_view_of_client); // Register Alice's DHT ID on Bob's side - pair.client.registerDhtNodeId(alice_dht_id, client_view_of_server) catch {}; + try pair.client.registerDhtNodeId(alice_dht_id, client_view_of_server); // Add to DHT routing tables var alice_addr = Address.init(); alice_addr.port = pair.server_port; alice_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } }; - pair.client.dhtAddPeer(alice_dht_id, alice_addr) catch {}; + try pair.client.dhtAddPeer(alice_dht_id, alice_addr); var bob_addr = Address.init(); bob_addr.port = pair.client_port; bob_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } }; - pair.server.dhtAddPeer(bob_dht_id, bob_addr) catch {}; + try pair.server.dhtAddPeer(bob_dht_id, bob_addr); // Let DHT stabilize pair.updateBothN(10);