• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4
5const {
6  BlockList,
7  SocketAddress,
8} = require('net');
9const assert = require('assert');
10const util = require('util');
11
12{
13  const blockList = new BlockList();
14
15  [1, [], {}, null, 1n, undefined, null].forEach((i) => {
16    assert.throws(() => blockList.addAddress(i), {
17      code: 'ERR_INVALID_ARG_TYPE'
18    });
19  });
20
21  [1, [], {}, null, 1n, null].forEach((i) => {
22    assert.throws(() => blockList.addAddress('1.1.1.1', i), {
23      code: 'ERR_INVALID_ARG_TYPE'
24    });
25  });
26
27  assert.throws(() => blockList.addAddress('1.1.1.1', 'foo'), {
28    code: 'ERR_INVALID_ARG_VALUE'
29  });
30
31  [1, [], {}, null, 1n, undefined, null].forEach((i) => {
32    assert.throws(() => blockList.addRange(i), {
33      code: 'ERR_INVALID_ARG_TYPE'
34    });
35    assert.throws(() => blockList.addRange('1.1.1.1', i), {
36      code: 'ERR_INVALID_ARG_TYPE'
37    });
38  });
39
40  [1, [], {}, null, 1n, null].forEach((i) => {
41    assert.throws(() => blockList.addRange('1.1.1.1', '1.1.1.2', i), {
42      code: 'ERR_INVALID_ARG_TYPE'
43    });
44  });
45
46  assert.throws(() => blockList.addRange('1.1.1.1', '1.1.1.2', 'foo'), {
47    code: 'ERR_INVALID_ARG_VALUE'
48  });
49}
50
51{
52  const blockList = new BlockList();
53  blockList.addAddress('1.1.1.1');
54  blockList.addAddress('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17', 'ipv6');
55  blockList.addAddress('::ffff:1.1.1.2', 'ipv6');
56
57  assert(blockList.check('1.1.1.1'));
58  assert(!blockList.check('1.1.1.1', 'ipv6'));
59  assert(!blockList.check('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17'));
60  assert(blockList.check('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17', 'ipv6'));
61
62  assert(blockList.check('::ffff:1.1.1.1', 'ipv6'));
63  assert(blockList.check('::ffff:1.1.1.1', 'IPV6'));
64
65  assert(blockList.check('1.1.1.2'));
66
67  assert(!blockList.check('1.2.3.4'));
68  assert(!blockList.check('::1', 'ipv6'));
69}
70
71{
72  const blockList = new BlockList();
73  const sa1 = new SocketAddress({ address: '1.1.1.1' });
74  const sa2 = new SocketAddress({
75    address: '8592:757c:efae:4e45:fb5d:d62a:0d00:8e17',
76    family: 'ipv6'
77  });
78  const sa3 = new SocketAddress({ address: '1.1.1.2' });
79
80  blockList.addAddress(sa1);
81  blockList.addAddress(sa2);
82  blockList.addAddress('::ffff:1.1.1.2', 'ipv6');
83
84  assert(blockList.check('1.1.1.1'));
85  assert(blockList.check(sa1));
86  assert(!blockList.check('1.1.1.1', 'ipv6'));
87  assert(!blockList.check('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17'));
88  assert(blockList.check('8592:757c:efae:4e45:fb5d:d62a:0d00:8e17', 'ipv6'));
89  assert(blockList.check(sa2));
90
91  assert(blockList.check('::ffff:1.1.1.1', 'ipv6'));
92  assert(blockList.check('::ffff:1.1.1.1', 'IPV6'));
93
94  assert(blockList.check('1.1.1.2'));
95  assert(blockList.check(sa3));
96
97  assert(!blockList.check('1.2.3.4'));
98  assert(!blockList.check('::1', 'ipv6'));
99}
100
101{
102  const blockList = new BlockList();
103  blockList.addRange('1.1.1.1', '1.1.1.10');
104  blockList.addRange('::1', '::f', 'ipv6');
105
106  assert(!blockList.check('1.1.1.0'));
107  for (let n = 1; n <= 10; n++)
108    assert(blockList.check(`1.1.1.${n}`));
109  assert(!blockList.check('1.1.1.11'));
110
111  assert(!blockList.check('::0', 'ipv6'));
112  for (let n = 0x1; n <= 0xf; n++) {
113    assert(blockList.check(`::${n.toString(16)}`, 'ipv6'),
114           `::${n.toString(16)} check failed`);
115  }
116  assert(!blockList.check('::10', 'ipv6'));
117}
118
119{
120  const blockList = new BlockList();
121  const sa1 = new SocketAddress({ address: '1.1.1.1' });
122  const sa2 = new SocketAddress({ address: '1.1.1.10' });
123  const sa3 = new SocketAddress({ address: '::1', family: 'ipv6' });
124  const sa4 = new SocketAddress({ address: '::f', family: 'ipv6' });
125
126  blockList.addRange(sa1, sa2);
127  blockList.addRange(sa3, sa4);
128
129  assert(!blockList.check('1.1.1.0'));
130  for (let n = 1; n <= 10; n++)
131    assert(blockList.check(`1.1.1.${n}`));
132  assert(!blockList.check('1.1.1.11'));
133
134  assert(!blockList.check('::0', 'ipv6'));
135  for (let n = 0x1; n <= 0xf; n++) {
136    assert(blockList.check(`::${n.toString(16)}`, 'ipv6'),
137           `::${n.toString(16)} check failed`);
138  }
139  assert(!blockList.check('::10', 'ipv6'));
140}
141
142{
143  const blockList = new BlockList();
144  blockList.addSubnet('1.1.1.0', 16);
145  blockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6');
146
147  assert(blockList.check('1.1.0.1'));
148  assert(blockList.check('1.1.1.1'));
149  assert(!blockList.check('1.2.0.1'));
150  assert(blockList.check('::ffff:1.1.0.1', 'ipv6'));
151
152  assert(blockList.check('8592:757c:efae:4e45:f::', 'ipv6'));
153  assert(blockList.check('8592:757c:efae:4e45::f', 'ipv6'));
154  assert(!blockList.check('8592:757c:efae:4f45::f', 'ipv6'));
155}
156
157{
158  const blockList = new BlockList();
159  const sa1 = new SocketAddress({ address: '1.1.1.0' });
160  const sa2 = new SocketAddress({ address: '1.1.1.1' });
161  blockList.addSubnet(sa1, 16);
162  blockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6');
163
164  assert(blockList.check('1.1.0.1'));
165  assert(blockList.check(sa2));
166  assert(!blockList.check('1.2.0.1'));
167  assert(blockList.check('::ffff:1.1.0.1', 'ipv6'));
168
169  assert(blockList.check('8592:757c:efae:4e45:f::', 'ipv6'));
170  assert(blockList.check('8592:757c:efae:4e45::f', 'ipv6'));
171  assert(!blockList.check('8592:757c:efae:4f45::f', 'ipv6'));
172}
173
174{
175  const blockList = new BlockList();
176  blockList.addAddress('1.1.1.1');
177  blockList.addRange('10.0.0.1', '10.0.0.10');
178  blockList.addSubnet('8592:757c:efae:4e45::', 64, 'IpV6'); // Case insensitive
179
180  const rulesCheck = [
181    'Subnet: IPv6 8592:757c:efae:4e45::/64',
182    'Range: IPv4 10.0.0.1-10.0.0.10',
183    'Address: IPv4 1.1.1.1',
184  ];
185  assert.deepStrictEqual(blockList.rules, rulesCheck);
186
187  assert(blockList.check('1.1.1.1'));
188  assert(blockList.check('10.0.0.5'));
189  assert(blockList.check('::ffff:10.0.0.5', 'ipv6'));
190  assert(blockList.check('8592:757c:efae:4e45::f', 'ipv6'));
191
192  assert(!blockList.check('123.123.123.123'));
193  assert(!blockList.check('8592:757c:efaf:4e45:fb5d:d62a:0d00:8e17', 'ipv6'));
194  assert(!blockList.check('::ffff:123.123.123.123', 'ipv6'));
195}
196
197{
198  // This test validates boundaries of non-aligned CIDR bit prefixes
199  const blockList = new BlockList();
200  blockList.addSubnet('10.0.0.0', 27);
201  blockList.addSubnet('8592:757c:efaf::', 51, 'ipv6');
202
203  for (let n = 0; n <= 31; n++)
204    assert(blockList.check(`10.0.0.${n}`));
205  assert(!blockList.check('10.0.0.32'));
206
207  assert(blockList.check('8592:757c:efaf:0:0:0:0:0', 'ipv6'));
208  assert(blockList.check('8592:757c:efaf:1fff:ffff:ffff:ffff:ffff', 'ipv6'));
209  assert(!blockList.check('8592:757c:efaf:2fff:ffff:ffff:ffff:ffff', 'ipv6'));
210}
211
212{
213  // Regression test for https://github.com/nodejs/node/issues/39074
214  const blockList = new BlockList();
215
216  blockList.addRange('10.0.0.2', '10.0.0.10');
217
218  // IPv4 checks against IPv4 range.
219  assert(blockList.check('10.0.0.2'));
220  assert(blockList.check('10.0.0.10'));
221  assert(!blockList.check('192.168.0.3'));
222  assert(!blockList.check('2.2.2.2'));
223  assert(!blockList.check('255.255.255.255'));
224
225  // IPv6 checks against IPv4 range.
226  assert(blockList.check('::ffff:0a00:0002', 'ipv6'));
227  assert(blockList.check('::ffff:0a00:000a', 'ipv6'));
228  assert(!blockList.check('::ffff:c0a8:0003', 'ipv6'));
229  assert(!blockList.check('::ffff:0202:0202', 'ipv6'));
230  assert(!blockList.check('::ffff:ffff:ffff', 'ipv6'));
231}
232
233{
234  const blockList = new BlockList();
235  assert.throws(() => blockList.addRange('1.1.1.2', '1.1.1.1'), /ERR_INVALID_ARG_VALUE/);
236}
237
238{
239  const blockList = new BlockList();
240  assert.throws(() => blockList.addSubnet(1), /ERR_INVALID_ARG_TYPE/);
241  assert.throws(() => blockList.addSubnet('1.1.1.1', ''),
242                /ERR_INVALID_ARG_TYPE/);
243  assert.throws(() => blockList.addSubnet('1.1.1.1', NaN), /ERR_OUT_OF_RANGE/);
244  assert.throws(() => blockList.addSubnet('', 1, 1), /ERR_INVALID_ARG_TYPE/);
245  assert.throws(() => blockList.addSubnet('', 1, ''), /ERR_INVALID_ARG_VALUE/);
246
247  assert.throws(() => blockList.addSubnet('1.1.1.1', -1, 'ipv4'),
248                /ERR_OUT_OF_RANGE/);
249  assert.throws(() => blockList.addSubnet('1.1.1.1', 33, 'ipv4'),
250                /ERR_OUT_OF_RANGE/);
251
252  assert.throws(() => blockList.addSubnet('::', -1, 'ipv6'),
253                /ERR_OUT_OF_RANGE/);
254  assert.throws(() => blockList.addSubnet('::', 129, 'ipv6'),
255                /ERR_OUT_OF_RANGE/);
256}
257
258{
259  const blockList = new BlockList();
260  assert.throws(() => blockList.check(1), /ERR_INVALID_ARG_TYPE/);
261  assert.throws(() => blockList.check('', 1), /ERR_INVALID_ARG_TYPE/);
262}
263
264{
265  const blockList = new BlockList();
266  const ret = util.inspect(blockList, { depth: -1 });
267  assert.strictEqual(ret, '[BlockList]');
268}
269
270{
271  const blockList = new BlockList();
272  const ret = util.inspect(blockList, { depth: null });
273  assert(ret.includes('rules: []'));
274}
275