11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst {
61cb0ef41Sopenharmony_ci  BlockList,
71cb0ef41Sopenharmony_ci  SocketAddress,
81cb0ef41Sopenharmony_ci} = require('net');
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst util = require('util');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci{
131cb0ef41Sopenharmony_ci  const blockList = new BlockList();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  [1, [], {}, null, 1n, undefined, null].forEach((i) => {
161cb0ef41Sopenharmony_ci    assert.throws(() => blockList.addAddress(i), {
171cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE'
181cb0ef41Sopenharmony_ci    });
191cb0ef41Sopenharmony_ci  });
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  [1, [], {}, null, 1n, null].forEach((i) => {
221cb0ef41Sopenharmony_ci    assert.throws(() => blockList.addAddress('1.1.1.1', i), {
231cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE'
241cb0ef41Sopenharmony_ci    });
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addAddress('1.1.1.1', 'foo'), {
281cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE'
291cb0ef41Sopenharmony_ci  });
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  [1, [], {}, null, 1n, undefined, null].forEach((i) => {
321cb0ef41Sopenharmony_ci    assert.throws(() => blockList.addRange(i), {
331cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE'
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci    assert.throws(() => blockList.addRange('1.1.1.1', i), {
361cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE'
371cb0ef41Sopenharmony_ci    });
381cb0ef41Sopenharmony_ci  });
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  [1, [], {}, null, 1n, null].forEach((i) => {
411cb0ef41Sopenharmony_ci    assert.throws(() => blockList.addRange('1.1.1.1', '1.1.1.2', i), {
421cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE'
431cb0ef41Sopenharmony_ci    });
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addRange('1.1.1.1', '1.1.1.2', 'foo'), {
471cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE'
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci{
521cb0ef41Sopenharmony_ci  const blockList = new BlockList();
531cb0ef41Sopenharmony_ci  blockList.addAddress('1.1.1.1');
541cb0ef41Sopenharmony_ci  blockList.addAddress('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17', 'ipv6');
551cb0ef41Sopenharmony_ci  blockList.addAddress('::ffff:1.1.1.2', 'ipv6');
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  assert(blockList.check('1.1.1.1'));
581cb0ef41Sopenharmony_ci  assert(!blockList.check('1.1.1.1', 'ipv6'));
591cb0ef41Sopenharmony_ci  assert(!blockList.check('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17'));
601cb0ef41Sopenharmony_ci  assert(blockList.check('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17', 'ipv6'));
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  assert(blockList.check('::ffff:1.1.1.1', 'ipv6'));
631cb0ef41Sopenharmony_ci  assert(blockList.check('::ffff:1.1.1.1', 'IPV6'));
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  assert(blockList.check('1.1.1.2'));
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  assert(!blockList.check('1.2.3.4'));
681cb0ef41Sopenharmony_ci  assert(!blockList.check('::1', 'ipv6'));
691cb0ef41Sopenharmony_ci}
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci{
721cb0ef41Sopenharmony_ci  const blockList = new BlockList();
731cb0ef41Sopenharmony_ci  const sa1 = new SocketAddress({ address: '1.1.1.1' });
741cb0ef41Sopenharmony_ci  const sa2 = new SocketAddress({
751cb0ef41Sopenharmony_ci    address: '8592:757c:efae:4e45:fb5d:d62a:0d00:8e17',
761cb0ef41Sopenharmony_ci    family: 'ipv6'
771cb0ef41Sopenharmony_ci  });
781cb0ef41Sopenharmony_ci  const sa3 = new SocketAddress({ address: '1.1.1.2' });
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  blockList.addAddress(sa1);
811cb0ef41Sopenharmony_ci  blockList.addAddress(sa2);
821cb0ef41Sopenharmony_ci  blockList.addAddress('::ffff:1.1.1.2', 'ipv6');
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  assert(blockList.check('1.1.1.1'));
851cb0ef41Sopenharmony_ci  assert(blockList.check(sa1));
861cb0ef41Sopenharmony_ci  assert(!blockList.check('1.1.1.1', 'ipv6'));
871cb0ef41Sopenharmony_ci  assert(!blockList.check('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17'));
881cb0ef41Sopenharmony_ci  assert(blockList.check('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17', 'ipv6'));
891cb0ef41Sopenharmony_ci  assert(blockList.check(sa2));
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  assert(blockList.check('::ffff:1.1.1.1', 'ipv6'));
921cb0ef41Sopenharmony_ci  assert(blockList.check('::ffff:1.1.1.1', 'IPV6'));
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  assert(blockList.check('1.1.1.2'));
951cb0ef41Sopenharmony_ci  assert(blockList.check(sa3));
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  assert(!blockList.check('1.2.3.4'));
981cb0ef41Sopenharmony_ci  assert(!blockList.check('::1', 'ipv6'));
991cb0ef41Sopenharmony_ci}
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci{
1021cb0ef41Sopenharmony_ci  const blockList = new BlockList();
1031cb0ef41Sopenharmony_ci  blockList.addRange('1.1.1.1', '1.1.1.10');
1041cb0ef41Sopenharmony_ci  blockList.addRange('::1', '::f', 'ipv6');
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  assert(!blockList.check('1.1.1.0'));
1071cb0ef41Sopenharmony_ci  for (let n = 1; n <= 10; n++)
1081cb0ef41Sopenharmony_ci    assert(blockList.check(`1.1.1.${n}`));
1091cb0ef41Sopenharmony_ci  assert(!blockList.check('1.1.1.11'));
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  assert(!blockList.check('::0', 'ipv6'));
1121cb0ef41Sopenharmony_ci  for (let n = 0x1; n <= 0xf; n++) {
1131cb0ef41Sopenharmony_ci    assert(blockList.check(`::${n.toString(16)}`, 'ipv6'),
1141cb0ef41Sopenharmony_ci           `::${n.toString(16)} check failed`);
1151cb0ef41Sopenharmony_ci  }
1161cb0ef41Sopenharmony_ci  assert(!blockList.check('::10', 'ipv6'));
1171cb0ef41Sopenharmony_ci}
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci{
1201cb0ef41Sopenharmony_ci  const blockList = new BlockList();
1211cb0ef41Sopenharmony_ci  const sa1 = new SocketAddress({ address: '1.1.1.1' });
1221cb0ef41Sopenharmony_ci  const sa2 = new SocketAddress({ address: '1.1.1.10' });
1231cb0ef41Sopenharmony_ci  const sa3 = new SocketAddress({ address: '::1', family: 'ipv6' });
1241cb0ef41Sopenharmony_ci  const sa4 = new SocketAddress({ address: '::f', family: 'ipv6' });
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  blockList.addRange(sa1, sa2);
1271cb0ef41Sopenharmony_ci  blockList.addRange(sa3, sa4);
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci  assert(!blockList.check('1.1.1.0'));
1301cb0ef41Sopenharmony_ci  for (let n = 1; n <= 10; n++)
1311cb0ef41Sopenharmony_ci    assert(blockList.check(`1.1.1.${n}`));
1321cb0ef41Sopenharmony_ci  assert(!blockList.check('1.1.1.11'));
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  assert(!blockList.check('::0', 'ipv6'));
1351cb0ef41Sopenharmony_ci  for (let n = 0x1; n <= 0xf; n++) {
1361cb0ef41Sopenharmony_ci    assert(blockList.check(`::${n.toString(16)}`, 'ipv6'),
1371cb0ef41Sopenharmony_ci           `::${n.toString(16)} check failed`);
1381cb0ef41Sopenharmony_ci  }
1391cb0ef41Sopenharmony_ci  assert(!blockList.check('::10', 'ipv6'));
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci{
1431cb0ef41Sopenharmony_ci  const blockList = new BlockList();
1441cb0ef41Sopenharmony_ci  blockList.addSubnet('1.1.1.0', 16);
1451cb0ef41Sopenharmony_ci  blockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6');
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci  assert(blockList.check('1.1.0.1'));
1481cb0ef41Sopenharmony_ci  assert(blockList.check('1.1.1.1'));
1491cb0ef41Sopenharmony_ci  assert(!blockList.check('1.2.0.1'));
1501cb0ef41Sopenharmony_ci  assert(blockList.check('::ffff:1.1.0.1', 'ipv6'));
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  assert(blockList.check('8592:757c:efae:4e45:f::', 'ipv6'));
1531cb0ef41Sopenharmony_ci  assert(blockList.check('8592:757c:efae:4e45::f', 'ipv6'));
1541cb0ef41Sopenharmony_ci  assert(!blockList.check('8592:757c:efae:4f45::f', 'ipv6'));
1551cb0ef41Sopenharmony_ci}
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci{
1581cb0ef41Sopenharmony_ci  const blockList = new BlockList();
1591cb0ef41Sopenharmony_ci  const sa1 = new SocketAddress({ address: '1.1.1.0' });
1601cb0ef41Sopenharmony_ci  const sa2 = new SocketAddress({ address: '1.1.1.1' });
1611cb0ef41Sopenharmony_ci  blockList.addSubnet(sa1, 16);
1621cb0ef41Sopenharmony_ci  blockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6');
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  assert(blockList.check('1.1.0.1'));
1651cb0ef41Sopenharmony_ci  assert(blockList.check(sa2));
1661cb0ef41Sopenharmony_ci  assert(!blockList.check('1.2.0.1'));
1671cb0ef41Sopenharmony_ci  assert(blockList.check('::ffff:1.1.0.1', 'ipv6'));
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci  assert(blockList.check('8592:757c:efae:4e45:f::', 'ipv6'));
1701cb0ef41Sopenharmony_ci  assert(blockList.check('8592:757c:efae:4e45::f', 'ipv6'));
1711cb0ef41Sopenharmony_ci  assert(!blockList.check('8592:757c:efae:4f45::f', 'ipv6'));
1721cb0ef41Sopenharmony_ci}
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci{
1751cb0ef41Sopenharmony_ci  const blockList = new BlockList();
1761cb0ef41Sopenharmony_ci  blockList.addAddress('1.1.1.1');
1771cb0ef41Sopenharmony_ci  blockList.addRange('10.0.0.1', '10.0.0.10');
1781cb0ef41Sopenharmony_ci  blockList.addSubnet('8592:757c:efae:4e45::', 64, 'IpV6'); // Case insensitive
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci  const rulesCheck = [
1811cb0ef41Sopenharmony_ci    'Subnet: IPv6 8592:757c:efae:4e45::/64',
1821cb0ef41Sopenharmony_ci    'Range: IPv4 10.0.0.1-10.0.0.10',
1831cb0ef41Sopenharmony_ci    'Address: IPv4 1.1.1.1',
1841cb0ef41Sopenharmony_ci  ];
1851cb0ef41Sopenharmony_ci  assert.deepStrictEqual(blockList.rules, rulesCheck);
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci  assert(blockList.check('1.1.1.1'));
1881cb0ef41Sopenharmony_ci  assert(blockList.check('10.0.0.5'));
1891cb0ef41Sopenharmony_ci  assert(blockList.check('::ffff:10.0.0.5', 'ipv6'));
1901cb0ef41Sopenharmony_ci  assert(blockList.check('8592:757c:efae:4e45::f', 'ipv6'));
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  assert(!blockList.check('123.123.123.123'));
1931cb0ef41Sopenharmony_ci  assert(!blockList.check('8592:757c:efaf:4e45:fb5d:d62a:0d00:8e17', 'ipv6'));
1941cb0ef41Sopenharmony_ci  assert(!blockList.check('::ffff:123.123.123.123', 'ipv6'));
1951cb0ef41Sopenharmony_ci}
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci{
1981cb0ef41Sopenharmony_ci  // This test validates boundaries of non-aligned CIDR bit prefixes
1991cb0ef41Sopenharmony_ci  const blockList = new BlockList();
2001cb0ef41Sopenharmony_ci  blockList.addSubnet('10.0.0.0', 27);
2011cb0ef41Sopenharmony_ci  blockList.addSubnet('8592:757c:efaf::', 51, 'ipv6');
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci  for (let n = 0; n <= 31; n++)
2041cb0ef41Sopenharmony_ci    assert(blockList.check(`10.0.0.${n}`));
2051cb0ef41Sopenharmony_ci  assert(!blockList.check('10.0.0.32'));
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci  assert(blockList.check('8592:757c:efaf:0:0:0:0:0', 'ipv6'));
2081cb0ef41Sopenharmony_ci  assert(blockList.check('8592:757c:efaf:1fff:ffff:ffff:ffff:ffff', 'ipv6'));
2091cb0ef41Sopenharmony_ci  assert(!blockList.check('8592:757c:efaf:2fff:ffff:ffff:ffff:ffff', 'ipv6'));
2101cb0ef41Sopenharmony_ci}
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci{
2131cb0ef41Sopenharmony_ci  // Regression test for https://github.com/nodejs/node/issues/39074
2141cb0ef41Sopenharmony_ci  const blockList = new BlockList();
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci  blockList.addRange('10.0.0.2', '10.0.0.10');
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci  // IPv4 checks against IPv4 range.
2191cb0ef41Sopenharmony_ci  assert(blockList.check('10.0.0.2'));
2201cb0ef41Sopenharmony_ci  assert(blockList.check('10.0.0.10'));
2211cb0ef41Sopenharmony_ci  assert(!blockList.check('192.168.0.3'));
2221cb0ef41Sopenharmony_ci  assert(!blockList.check('2.2.2.2'));
2231cb0ef41Sopenharmony_ci  assert(!blockList.check('255.255.255.255'));
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci  // IPv6 checks against IPv4 range.
2261cb0ef41Sopenharmony_ci  assert(blockList.check('::ffff:0a00:0002', 'ipv6'));
2271cb0ef41Sopenharmony_ci  assert(blockList.check('::ffff:0a00:000a', 'ipv6'));
2281cb0ef41Sopenharmony_ci  assert(!blockList.check('::ffff:c0a8:0003', 'ipv6'));
2291cb0ef41Sopenharmony_ci  assert(!blockList.check('::ffff:0202:0202', 'ipv6'));
2301cb0ef41Sopenharmony_ci  assert(!blockList.check('::ffff:ffff:ffff', 'ipv6'));
2311cb0ef41Sopenharmony_ci}
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci{
2341cb0ef41Sopenharmony_ci  const blockList = new BlockList();
2351cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addRange('1.1.1.2', '1.1.1.1'), /ERR_INVALID_ARG_VALUE/);
2361cb0ef41Sopenharmony_ci}
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci{
2391cb0ef41Sopenharmony_ci  const blockList = new BlockList();
2401cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addSubnet(1), /ERR_INVALID_ARG_TYPE/);
2411cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addSubnet('1.1.1.1', ''),
2421cb0ef41Sopenharmony_ci                /ERR_INVALID_ARG_TYPE/);
2431cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addSubnet('1.1.1.1', NaN), /ERR_OUT_OF_RANGE/);
2441cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addSubnet('', 1, 1), /ERR_INVALID_ARG_TYPE/);
2451cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addSubnet('', 1, ''), /ERR_INVALID_ARG_VALUE/);
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addSubnet('1.1.1.1', -1, 'ipv4'),
2481cb0ef41Sopenharmony_ci                /ERR_OUT_OF_RANGE/);
2491cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addSubnet('1.1.1.1', 33, 'ipv4'),
2501cb0ef41Sopenharmony_ci                /ERR_OUT_OF_RANGE/);
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addSubnet('::', -1, 'ipv6'),
2531cb0ef41Sopenharmony_ci                /ERR_OUT_OF_RANGE/);
2541cb0ef41Sopenharmony_ci  assert.throws(() => blockList.addSubnet('::', 129, 'ipv6'),
2551cb0ef41Sopenharmony_ci                /ERR_OUT_OF_RANGE/);
2561cb0ef41Sopenharmony_ci}
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci{
2591cb0ef41Sopenharmony_ci  const blockList = new BlockList();
2601cb0ef41Sopenharmony_ci  assert.throws(() => blockList.check(1), /ERR_INVALID_ARG_TYPE/);
2611cb0ef41Sopenharmony_ci  assert.throws(() => blockList.check('', 1), /ERR_INVALID_ARG_TYPE/);
2621cb0ef41Sopenharmony_ci}
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ci{
2651cb0ef41Sopenharmony_ci  const blockList = new BlockList();
2661cb0ef41Sopenharmony_ci  const ret = util.inspect(blockList, { depth: -1 });
2671cb0ef41Sopenharmony_ci  assert.strictEqual(ret, '[BlockList]');
2681cb0ef41Sopenharmony_ci}
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci{
2711cb0ef41Sopenharmony_ci  const blockList = new BlockList();
2721cb0ef41Sopenharmony_ci  const ret = util.inspect(blockList, { depth: null });
2731cb0ef41Sopenharmony_ci  assert(ret.includes('rules: []'));
2741cb0ef41Sopenharmony_ci}
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci{
2771cb0ef41Sopenharmony_ci  // Test for https://github.com/nodejs/node/issues/43360
2781cb0ef41Sopenharmony_ci  const blocklist = new BlockList();
2791cb0ef41Sopenharmony_ci  blocklist.addSubnet('1.1.1.1', 32, 'ipv4');
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci  assert(blocklist.check('1.1.1.1'));
2821cb0ef41Sopenharmony_ci  assert(!blocklist.check('1.1.1.2'));
2831cb0ef41Sopenharmony_ci  assert(!blocklist.check('2.3.4.5'));
2841cb0ef41Sopenharmony_ci}
285