diff --git a/lib/internal/blocklist.js b/lib/internal/blocklist.js index 28a31caa165e09..ba8a9ec45081b9 100644 --- a/lib/internal/blocklist.js +++ b/lib/internal/blocklist.js @@ -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 @@ -81,8 +82,6 @@ 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(); @@ -90,13 +89,11 @@ class BlockList { 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); diff --git a/test/parallel/test-blocklist.js b/test/parallel/test-blocklist.js index 953b26eec46ade..0d537574c72e11 100644 --- a/test/parallel/test-blocklist.js +++ b/test/parallel/test-blocklist.js @@ -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/);