Skip to content

Commit 24246a2

Browse files
Lxxyxdanielleadams
authored andcommitted
net: throw ERR_OUT_OF_RANGE if blockList.addSubnet prefix is NaN
Fixes: #36731 PR-URL: #36732 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2d8423d commit 24246a2

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

lib/internal/blocklist.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ const { owner_symbol } = internalBinding('symbols');
2222
const {
2323
ERR_INVALID_ARG_TYPE,
2424
ERR_INVALID_ARG_VALUE,
25-
ERR_OUT_OF_RANGE,
2625
} = require('internal/errors').codes;
2726

27+
const { validateInt32 } = require('internal/validators');
28+
2829
class BlockList {
2930
constructor(handle = new BlockListHandle()) {
3031
// The handle argument is an intentionally undocumented
@@ -81,22 +82,18 @@ class BlockList {
8182
addSubnet(network, prefix, family = 'ipv4') {
8283
if (typeof network !== 'string')
8384
throw new ERR_INVALID_ARG_TYPE('network', 'string', network);
84-
if (typeof prefix !== 'number')
85-
throw new ERR_INVALID_ARG_TYPE('prefix', 'number', prefix);
8685
if (typeof family !== 'string')
8786
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
8887
family = family.toLowerCase();
8988
let type;
9089
switch (family) {
9190
case 'ipv4':
9291
type = AF_INET;
93-
if (prefix < 0 || prefix > 32)
94-
throw new ERR_OUT_OF_RANGE(prefix, '>= 0 and <= 32', prefix);
92+
validateInt32(prefix, 'prefix', 0, 32);
9593
break;
9694
case 'ipv6':
9795
type = AF_INET6;
98-
if (prefix < 0 || prefix > 128)
99-
throw new ERR_OUT_OF_RANGE(prefix, '>= 0 and <= 128', prefix);
96+
validateInt32(prefix, 'prefix', 0, 128);
10097
break;
10198
default:
10299
throw new ERR_INVALID_ARG_VALUE('family', family);

test/parallel/test-blocklist.js

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ const util = require('util');
150150
const blockList = new BlockList();
151151
assert.throws(() => blockList.addSubnet(1), /ERR_INVALID_ARG_TYPE/);
152152
assert.throws(() => blockList.addSubnet('', ''), /ERR_INVALID_ARG_TYPE/);
153+
assert.throws(() => blockList.addSubnet('', NaN), /ERR_OUT_OF_RANGE/);
153154
assert.throws(() => blockList.addSubnet('', 1, 1), /ERR_INVALID_ARG_TYPE/);
154155
assert.throws(() => blockList.addSubnet('', 1, ''), /ERR_INVALID_ARG_VALUE/);
155156

0 commit comments

Comments
 (0)