Skip to content

Commit

Permalink
net: throw ERR_OUT_OF_RANGE if blockList.addSubnet prefix is NaN
Browse files Browse the repository at this point in the history
Fixes: #36731

PR-URL: #36732
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Lxxyx authored and danielleadams committed Jan 12, 2021
1 parent 2d8423d commit 24246a2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/internal/blocklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ const { owner_symbol } = internalBinding('symbols');
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE,
} = require('internal/errors').codes;

const { validateInt32 } = require('internal/validators');

class BlockList {
constructor(handle = new BlockListHandle()) {
// The handle argument is an intentionally undocumented
Expand Down Expand Up @@ -81,22 +82,18 @@ class BlockList {
addSubnet(network, prefix, family = 'ipv4') {
if (typeof network !== 'string')
throw new ERR_INVALID_ARG_TYPE('network', 'string', network);
if (typeof prefix !== 'number')
throw new ERR_INVALID_ARG_TYPE('prefix', 'number', prefix);
if (typeof family !== 'string')
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
family = family.toLowerCase();
let type;
switch (family) {
case 'ipv4':
type = AF_INET;
if (prefix < 0 || prefix > 32)
throw new ERR_OUT_OF_RANGE(prefix, '>= 0 and <= 32', prefix);
validateInt32(prefix, 'prefix', 0, 32);
break;
case 'ipv6':
type = AF_INET6;
if (prefix < 0 || prefix > 128)
throw new ERR_OUT_OF_RANGE(prefix, '>= 0 and <= 128', prefix);
validateInt32(prefix, 'prefix', 0, 128);
break;
default:
throw new ERR_INVALID_ARG_VALUE('family', family);
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-blocklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const util = require('util');
const blockList = new BlockList();
assert.throws(() => blockList.addSubnet(1), /ERR_INVALID_ARG_TYPE/);
assert.throws(() => blockList.addSubnet('', ''), /ERR_INVALID_ARG_TYPE/);
assert.throws(() => blockList.addSubnet('', NaN), /ERR_OUT_OF_RANGE/);
assert.throws(() => blockList.addSubnet('', 1, 1), /ERR_INVALID_ARG_TYPE/);
assert.throws(() => blockList.addSubnet('', 1, ''), /ERR_INVALID_ARG_VALUE/);

Expand Down

0 comments on commit 24246a2

Please sign in to comment.