fix MWP: incremental Blake2b hashing, address count limit in

deserialization
This commit is contained in:
2026-02-11 14:21:23 -03:00
parent 137469c3e1
commit 30bad26e67
3 changed files with 31 additions and 25 deletions

View File

@@ -11,6 +11,9 @@ pub const PROTOCOL_VERSION: u8 = 0x01;
/// Magic bytes for hole punch packets /// Magic bytes for hole punch packets
pub const HOLE_PUNCH_MAGIC = [4]u8{ 'M', 'W', 'P', '1' }; 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 /// Default parameters
pub const Defaults = struct { pub const Defaults = struct {
/// Time to live for publications (seconds) /// Time to live for publications (seconds)
@@ -187,44 +190,46 @@ pub const MeetingPublication = struct {
/// Compute hash of publication (for signing) /// Compute hash of publication (for signing)
pub fn computeHash(self: MeetingPublication) [32]u8 { pub fn computeHash(self: MeetingPublication) [32]u8 {
var buf: [1024]u8 = undefined; const Blake2b256 = std.crypto.hash.blake2.Blake2b256;
var idx: usize = 0; var hasher = Blake2b256.init(.{});
// Version // Version
buf[idx] = PROTOCOL_VERSION; hasher.update(&[_]u8{PROTOCOL_VERSION});
idx += 1;
// Peer ID // Peer ID
@memcpy(buf[idx..][0..32], &self.peer_id); hasher.update(&self.peer_id);
idx += 32;
// Address count // Address count
buf[idx] = @intCast(self.addresses.len); hasher.update(&[_]u8{@intCast(self.addresses.len)});
idx += 1;
// Addresses (simplified - just count for hash) // Addresses
for (self.addresses) |addr| { for (self.addresses) |addr| {
const size = addr.serialize(buf[idx..]) catch 0; var addr_buf: [19]u8 = undefined; // max: 1 type + 16 ipv6 + 2 port
idx += size; const size = addr.serialize(&addr_buf) catch 0;
hasher.update(addr_buf[0..size]);
} }
// Nonce // Nonce
@memcpy(buf[idx..][0..32], &self.nonce); hasher.update(&self.nonce);
idx += 32;
// Timestamp // Timestamp
std.mem.writeInt(i64, buf[idx..][0..8], self.timestamp, .big); var ts_buf: [8]u8 = undefined;
idx += 8; std.mem.writeInt(i64, &ts_buf, self.timestamp, .big);
hasher.update(&ts_buf);
// TTL // TTL
std.mem.writeInt(u32, buf[idx..][0..4], self.ttl, .big); var ttl_buf: [4]u8 = undefined;
idx += 4; std.mem.writeInt(u32, &ttl_buf, self.ttl, .big);
hasher.update(&ttl_buf);
// Sequence // Sequence
std.mem.writeInt(u64, buf[idx..][0..8], self.sequence, .big); var seq_buf: [8]u8 = undefined;
idx += 8; 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 /// Verify signature
@@ -314,6 +319,7 @@ pub const MeetingPublication = struct {
// Addresses // Addresses
const addr_count = buf[idx]; const addr_count = buf[idx];
idx += 1; idx += 1;
if (addr_count > MAX_ADDRESSES) return error.InvalidData;
const addresses = try allocator.alloc(Address, addr_count); const addresses = try allocator.alloc(Address, addr_count);
errdefer allocator.free(addresses); errdefer allocator.free(addresses);

View File

@@ -57,7 +57,7 @@ pub const KeyPair = mwp_lib.KeyPair;
/// Maximum allowed clock skew for peer publications (milliseconds). /// Maximum allowed clock skew for peer publications (milliseconds).
/// Publications with timestamps more than this far in the future are rejected /// Publications with timestamps more than this far in the future are rejected
/// to prevent a malicious peer from delaying rendezvous arbitrarily. /// 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 /// Main MWP integration with zig-p2p
pub const MWP = struct { pub const MWP = struct {

View File

@@ -57,21 +57,21 @@ test "MWP: two P2P nodes complete hole punch on localhost" {
std.debug.print("Server has no connected peer\n", .{}); std.debug.print("Server has no connected peer\n", .{});
return error.SkipZigTest; 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 // 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 // Add to DHT routing tables
var alice_addr = Address.init(); var alice_addr = Address.init();
alice_addr.port = pair.server_port; alice_addr.port = pair.server_port;
alice_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } }; 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(); var bob_addr = Address.init();
bob_addr.port = pair.client_port; bob_addr.port = pair.client_port;
bob_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } }; 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 // Let DHT stabilize
pair.updateBothN(10); pair.updateBothN(10);