-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest.js
More file actions
432 lines (363 loc) · 15.5 KB
/
test.js
File metadata and controls
432 lines (363 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* eslint max-statements:0 */
'use strict';
import {describe, it} from 'node:test';
import assert from 'node:assert';
import {parse as parseUrl} from 'node:url';
import {getProxyForUrl} from 'proxy-from-env';
// Runs the callback with process.env temporarily set to env.
function runWithEnv(env, callback) {
var originalEnv = process.env;
process.env = env;
try {
callback();
} finally {
process.env = originalEnv;
}
}
// Defines a test case that checks whether getProxyForUrl(input) === expected.
function testProxyUrl(env, expected, input) {
assert(typeof env === 'object' && env !== null);
// Copy object to make sure that the in param does not get modified between
// the call of this function and the use of it below.
env = JSON.parse(JSON.stringify(env));
var title = 'getProxyForUrl(' + JSON.stringify(input) + ')' +
' === ' + JSON.stringify(expected);
it(title, function() {
var actual;
runWithEnv(env, function() {
actual = getProxyForUrl(input);
});
assert.strictEqual(actual, expected);
});
}
describe('getProxyForUrl', function() {
describe('No proxy variables', function() {
var env = {};
testProxyUrl(env, '', 'http://example.com');
testProxyUrl(env, '', 'https://example.com');
testProxyUrl(env, '', 'ftp://example.com');
});
describe('Invalid URLs', function() {
var env = {};
env.ALL_PROXY = 'http://unexpected.proxy';
testProxyUrl(env, '', 'bogus');
testProxyUrl(env, '', '//example.com');
testProxyUrl(env, '', '://example.com');
testProxyUrl(env, '', '://');
testProxyUrl(env, '', '/path');
testProxyUrl(env, '', '');
testProxyUrl(env, '', 'http:');
testProxyUrl(env, '', 'http:/');
testProxyUrl(env, '', 'http://');
testProxyUrl(env, '', 'prototype://');
testProxyUrl(env, '', 'hasOwnProperty://');
testProxyUrl(env, '', '__proto__://');
testProxyUrl(env, '', 'http://abc\x00/');
testProxyUrl(env, '', undefined);
testProxyUrl(env, '', null);
testProxyUrl(env, '', {});
testProxyUrl(env, '', {host: 'x', protocol: 1});
testProxyUrl(env, '', {host: 1, protocol: 'x'});
describe('difference between url.parse and WHATWG URL', function() {
// Node 24 and later raise the following warning when url.parse is used:
//
// (node:11623) [DEP0169] DeprecationWarning: `url.parse()` behavior is
// not standardized and prone to errors that have security implications.
// Use the WHATWG URL API instead. CVEs are not issued for `url.parse()`
// vulnerabilities.
//
// The above refers to https://hackerone.com/reports/678487 which shows
// that a bare percentage sign is parsed inconsistently:
// - `url.parse` splits hosts.
// - WHATWG `URL` constructor raised an error.
//
// This test case shows the difference.
//
// For comparison:
// - curl (8.17.0) refuses to connect:
// $ http_proxy=http://localhost:1337 curl http://bad%
// curl:(3) URL rejected: Bad hostname
// - wget (GNU wget 1.25.0) passes "bad%" as Host header:
// $ http_proxy=http://localhost:1337 wget http://bad%
// (nc -l 1337 receives request with "bad% as Host header)
// - Python (3.13.11) passes "bad%" as Host header:
// $ http_proxy=http://localhost:1337 python3 -c \
// 'import urllib.request;urllib.request.urlopen("http://bad%")'
// (nc -l 1337 receives request with "bad% as Host header)
// A canonical URL does not have a single "%".
var badUrl = 'http://bad%';
// proxy-from-env@1.1.0 and earlier accepted bad URLs:
testProxyUrl(env, 'http://unexpected.proxy', parseUrl(badUrl));
// Sanity check: WHATWG URL constructor rejects badUrl.
assert(!URL.canParse(badUrl));
// Verify current proxy-from-env behavior. Should reject without throwing.
testProxyUrl(env, '', badUrl);
});
});
describe('http_proxy and HTTP_PROXY', function() {
var env = {};
env.HTTP_PROXY = 'http://http-proxy';
testProxyUrl(env, '', 'https://example');
testProxyUrl(env, 'http://http-proxy', 'http://example');
testProxyUrl(env, 'http://http-proxy', parseUrl('http://example'));
// eslint-disable-next-line camelcase
env.http_proxy = 'http://priority';
testProxyUrl(env, 'http://priority', 'http://example');
});
describe('http_proxy with non-sensical value', function() {
var env = {};
// Crazy values should be passed as-is. It is the responsibility of the
// one who launches the application that the value makes sense.
// TODO: Should we be stricter and perform validation?
env.HTTP_PROXY = 'Crazy \n!() { ::// }';
testProxyUrl(env, 'Crazy \n!() { ::// }', 'http://wow');
// The implementation assumes that the HTTP_PROXY environment variable is
// somewhat reasonable, and if the scheme is missing, it is added.
// Garbage in, garbage out some would say...
env.HTTP_PROXY = 'crazy without colon slash slash';
testProxyUrl(env, 'http://crazy without colon slash slash', 'http://wow');
});
describe('https_proxy and HTTPS_PROXY', function() {
var env = {};
// Assert that there is no fall back to http_proxy
env.HTTP_PROXY = 'http://unexpected.proxy';
testProxyUrl(env, '', 'https://example');
env.HTTPS_PROXY = 'http://https-proxy';
testProxyUrl(env, 'http://https-proxy', 'https://example');
// eslint-disable-next-line camelcase
env.https_proxy = 'http://priority';
testProxyUrl(env, 'http://priority', 'https://example');
});
describe('ftp_proxy', function() {
var env = {};
// Something else than http_proxy / https, as a sanity check.
env.FTP_PROXY = 'http://ftp-proxy';
testProxyUrl(env, 'http://ftp-proxy', 'ftp://example');
testProxyUrl(env, '', 'ftps://example');
});
describe('all_proxy', function() {
var env = {};
env.ALL_PROXY = 'http://catch-all';
testProxyUrl(env, 'http://catch-all', 'https://example');
// eslint-disable-next-line camelcase
env.all_proxy = 'http://priority';
testProxyUrl(env, 'http://priority', 'https://example');
});
describe('all_proxy without scheme', function() {
var env = {};
env.ALL_PROXY = 'noscheme';
testProxyUrl(env, 'http://noscheme', 'http://example');
testProxyUrl(env, 'https://noscheme', 'https://example');
// The module does not impose restrictions on the scheme.
testProxyUrl(env, 'bogus-scheme://noscheme', 'bogus-scheme://example');
// But the URL should still be valid.
testProxyUrl(env, '', 'bogus');
});
describe('no_proxy empty', function() {
var env = {};
env.HTTPS_PROXY = 'http://proxy';
// NO_PROXY set but empty.
env.NO_PROXY = '';
testProxyUrl(env, 'http://proxy', 'https://example');
// No entries in NO_PROXY (comma).
env.NO_PROXY = ',';
testProxyUrl(env, 'http://proxy', 'https://example');
// No entries in NO_PROXY (whitespace).
env.NO_PROXY = ' ';
testProxyUrl(env, 'http://proxy', 'https://example');
// No entries in NO_PROXY (multiple whitespace / commas).
env.NO_PROXY = ',\t,,,\n, ,\r';
testProxyUrl(env, 'http://proxy', 'https://example');
});
describe('no_proxy=example (single host)', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = 'example';
testProxyUrl(env, '', 'http://example');
testProxyUrl(env, '', 'http://example:80');
testProxyUrl(env, '', 'http://example:0');
testProxyUrl(env, '', 'http://example:1337');
testProxyUrl(env, 'http://proxy', 'http://sub.example');
testProxyUrl(env, 'http://proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://example.no');
testProxyUrl(env, 'http://proxy', 'http://a.b.example');
testProxyUrl(env, 'http://proxy', 'http://host/example');
});
describe('no_proxy=sub.example (subdomain)', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = 'sub.example';
testProxyUrl(env, 'http://proxy', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://example:80');
testProxyUrl(env, 'http://proxy', 'http://example:0');
testProxyUrl(env, 'http://proxy', 'http://example:1337');
testProxyUrl(env, '', 'http://sub.example');
testProxyUrl(env, 'http://proxy', 'http://no.sub.example');
testProxyUrl(env, 'http://proxy', 'http://sub-example');
testProxyUrl(env, 'http://proxy', 'http://example.sub');
});
describe('no_proxy=example:80 (host + port)', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = 'example:80';
testProxyUrl(env, '', 'http://example');
testProxyUrl(env, '', 'http://example:80');
testProxyUrl(env, '', 'http://example:0');
testProxyUrl(env, 'http://proxy', 'http://example:1337');
testProxyUrl(env, 'http://proxy', 'http://sub.example');
testProxyUrl(env, 'http://proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://example.no');
testProxyUrl(env, 'http://proxy', 'http://a.b.example');
});
describe('no_proxy=.example (host suffix)', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = '.example';
testProxyUrl(env, 'http://proxy', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://example:80');
testProxyUrl(env, 'http://proxy', 'http://example:1337');
testProxyUrl(env, '', 'http://sub.example');
testProxyUrl(env, '', 'http://sub.example:80');
testProxyUrl(env, '', 'http://sub.example:1337');
testProxyUrl(env, 'http://proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://example.no');
testProxyUrl(env, '', 'http://a.b.example');
});
describe('no_proxy=*', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = '*';
testProxyUrl(env, '', 'http://example.com');
});
describe('no_proxy=*.example (host suffix with *.)', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = '*.example';
testProxyUrl(env, 'http://proxy', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://example:80');
testProxyUrl(env, 'http://proxy', 'http://example:1337');
testProxyUrl(env, '', 'http://sub.example');
testProxyUrl(env, '', 'http://sub.example:80');
testProxyUrl(env, '', 'http://sub.example:1337');
testProxyUrl(env, 'http://proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://example.no');
testProxyUrl(env, '', 'http://a.b.example');
});
describe('no_proxy=*example (substring suffix)', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = '*example';
testProxyUrl(env, '', 'http://example');
testProxyUrl(env, '', 'http://example:80');
testProxyUrl(env, '', 'http://example:1337');
testProxyUrl(env, '', 'http://sub.example');
testProxyUrl(env, '', 'http://sub.example:80');
testProxyUrl(env, '', 'http://sub.example:1337');
testProxyUrl(env, '', 'http://prefexample');
testProxyUrl(env, '', 'http://a.b.example');
testProxyUrl(env, 'http://proxy', 'http://example.no');
testProxyUrl(env, 'http://proxy', 'http://host/example');
});
describe('no_proxy=.*example (arbitrary wildcards are NOT supported)',
function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = '.*example';
testProxyUrl(env, 'http://proxy', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://sub.example');
testProxyUrl(env, 'http://proxy', 'http://sub.example');
testProxyUrl(env, 'http://proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://x.prefexample');
testProxyUrl(env, 'http://proxy', 'http://a.b.example');
});
describe('no_proxy=[::1],[::2]:80,10.0.0.1,10.0.0.2:80 (IP addresses)',
function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = '[::1],[::2]:80,10.0.0.1,10.0.0.2:80';
testProxyUrl(env, '', 'http://[::1]/');
testProxyUrl(env, '', 'http://[::1]:80/');
testProxyUrl(env, '', 'http://[::1]:1337/');
testProxyUrl(env, '', 'http://[::2]/');
testProxyUrl(env, '', 'http://[::2]:80/');
testProxyUrl(env, 'http://proxy', 'http://[::2]:1337/');
testProxyUrl(env, '', 'http://10.0.0.1/');
testProxyUrl(env, '', 'http://10.0.0.1:80/');
testProxyUrl(env, '', 'http://10.0.0.1:1337/');
testProxyUrl(env, '', 'http://10.0.0.2/');
testProxyUrl(env, '', 'http://10.0.0.2:80/');
testProxyUrl(env, 'http://proxy', 'http://10.0.0.2:1337/');
});
describe('no_proxy=127.0.0.1/32 (CIDR is NOT supported)', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = '127.0.0.1/32';
testProxyUrl(env, 'http://proxy', 'http://127.0.0.1');
testProxyUrl(env, 'http://proxy', 'http://127.0.0.1/32');
});
describe('no_proxy=127.0.0.1 does NOT match localhost', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = '127.0.0.1';
testProxyUrl(env, '', 'http://127.0.0.1');
// We're not performing DNS queries, so this shouldn't match.
testProxyUrl(env, 'http://proxy', 'http://localhost');
});
describe('no_proxy with protocols that have a default port', function() {
var env = {};
env.WS_PROXY = 'http://ws';
env.WSS_PROXY = 'http://wss';
env.HTTP_PROXY = 'http://http';
env.HTTPS_PROXY = 'http://https';
env.GOPHER_PROXY = 'http://gopher';
env.FTP_PROXY = 'http://ftp';
env.ALL_PROXY = 'http://all';
env.NO_PROXY = 'xxx:21,xxx:70,xxx:80,xxx:443';
testProxyUrl(env, '', 'http://xxx');
testProxyUrl(env, '', 'http://xxx:80');
testProxyUrl(env, 'http://http', 'http://xxx:1337');
testProxyUrl(env, '', 'ws://xxx');
testProxyUrl(env, '', 'ws://xxx:80');
testProxyUrl(env, 'http://ws', 'ws://xxx:1337');
testProxyUrl(env, '', 'https://xxx');
testProxyUrl(env, '', 'https://xxx:443');
testProxyUrl(env, 'http://https', 'https://xxx:1337');
testProxyUrl(env, '', 'wss://xxx');
testProxyUrl(env, '', 'wss://xxx:443');
testProxyUrl(env, 'http://wss', 'wss://xxx:1337');
testProxyUrl(env, '', 'gopher://xxx');
testProxyUrl(env, '', 'gopher://xxx:70');
testProxyUrl(env, 'http://gopher', 'gopher://xxx:1337');
testProxyUrl(env, '', 'ftp://xxx');
testProxyUrl(env, '', 'ftp://xxx:21');
testProxyUrl(env, 'http://ftp', 'ftp://xxx:1337');
});
describe('no_proxy should not be case-sensitive', function() {
var env = {};
env.HTTP_PROXY = 'http://proxy';
env.NO_PROXY = 'XXX,YYY,ZzZ';
testProxyUrl(env, '', 'http://xxx');
testProxyUrl(env, '', 'http://XXX');
testProxyUrl(env, '', 'http://yyy');
testProxyUrl(env, '', 'http://YYY');
testProxyUrl(env, '', 'http://ZzZ');
testProxyUrl(env, '', 'http://zZz');
});
// Up until proxy-from-env@1.1.0, proxy-from-env had undocumented support for
// specifying proxies through npm_config_ prefixes. The historical reasons
// for them are no longer relevant:
// https://github.com/Rob--W/proxy-from-env/issues/13#issuecomment-3150256653
describe('NPM proxy configuration', function() {
describe('npm_config_*_proxy variables are unsupported', function() {
var env = {};
// eslint-disable-next-line camelcase
env.npm_config_http_proxy = 'http://http-proxy';
// eslint-disable-next-line camelcase
env.npm_config_https_proxy = 'http://https-proxy';
// eslint-disable-next-line camelcase
env.npm_config_proxy = 'http://unexpected-proxy';
testProxyUrl(env, '', 'http://example');
testProxyUrl(env, '', 'https://example');
});
});
});