Skip to content

Commit f8bdc53

Browse files
supriyo-biswastargos
authored andcommitted
net: prevent /32 ipv4 mask from matching all ips
Fixes: #43360 PR-URL: #43381 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent b27856d commit f8bdc53

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/node_sockaddr.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ bool in_network_ipv4(
215215
const SocketAddress& ip,
216216
const SocketAddress& net,
217217
int prefix) {
218-
uint32_t mask = ((1 << prefix) - 1) << (32 - prefix);
218+
uint32_t mask = ((1ull << prefix) - 1) << (32 - prefix);
219219

220220
const sockaddr_in* ip_in =
221221
reinterpret_cast<const sockaddr_in*>(ip.data());
@@ -293,7 +293,7 @@ bool in_network_ipv6_ipv4(
293293
if (prefix == 32)
294294
return compare_ipv4_ipv6(net, ip) == SocketAddress::CompareResult::SAME;
295295

296-
uint32_t m = ((1 << prefix) - 1) << (32 - prefix);
296+
uint32_t m = ((1ull << prefix) - 1) << (32 - prefix);
297297

298298
const sockaddr_in6* ip_in =
299299
reinterpret_cast<const sockaddr_in6*>(ip.data());

test/parallel/test-blocklist.js

+10
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,13 @@ const util = require('util');
272272
const ret = util.inspect(blockList, { depth: null });
273273
assert(ret.includes('rules: []'));
274274
}
275+
276+
{
277+
// Test for https://github.com/nodejs/node/issues/43360
278+
const blocklist = new BlockList();
279+
blocklist.addSubnet('1.1.1.1', 32, 'ipv4');
280+
281+
assert(blocklist.check('1.1.1.1'));
282+
assert(!blocklist.check('1.1.1.2'));
283+
assert(!blocklist.check('2.3.4.5'));
284+
}

0 commit comments

Comments
 (0)