Skip to content

Commit 081d2c9

Browse files
author
o.drapeza
committed
feat(dispatcher/env-http-proxy-agent): strip leading dot and asterisk
1 parent 148e0ef commit 081d2c9

File tree

2 files changed

+45
-49
lines changed

2 files changed

+45
-49
lines changed

lib/dispatcher/env-http-proxy-agent.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,14 @@ class EnvHttpProxyAgent extends DispatcherBase {
9595
if (entry.port && entry.port !== port) {
9696
continue // Skip if ports don't match.
9797
}
98-
if (!/^[.*]/.test(entry.hostname)) {
99-
// No wildcards, so don't proxy only if there is not an exact match.
100-
if (hostname === entry.hostname) {
101-
return false
102-
}
103-
} else {
104-
// Don't proxy if the hostname ends with the no_proxy host.
105-
if (hostname.endsWith(entry.hostname.replace(/^\*/, ''))) {
106-
return false
107-
}
98+
// Don't proxy if the hostname is equal with the no_proxy host.
99+
if (hostname === entry.hostname) {
100+
return false
101+
}
102+
// Don't proxy if the hostname is the subdomain of the no_proxy host.
103+
// Reference - https://github.com/denoland/deno/blob/6fbce91e40cc07fc6da74068e5cc56fdd40f7b4c/ext/fetch/proxy.rs#L485
104+
if (hostname.slice(-(entry.hostname.length + 1)) === `.${entry.hostname}`) {
105+
return false
108106
}
109107
}
110108

@@ -123,7 +121,8 @@ class EnvHttpProxyAgent extends DispatcherBase {
123121
}
124122
const parsed = entry.match(/^(.+):(\d+)$/)
125123
noProxyEntries.push({
126-
hostname: (parsed ? parsed[1] : entry).toLowerCase(),
124+
// strip leading dot or asterisk with dot
125+
hostname: (parsed ? parsed[1] : entry).replace(/^\*?\./, '').toLowerCase(),
127126
port: parsed ? Number.parseInt(parsed[2], 10) : 0
128127
})
129128
}

test/env-http-proxy-agent.js

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ describe('no_proxy', () => {
258258
t.ok(await doesNotProxy('http://example:80'))
259259
t.ok(await doesNotProxy('http://example:0'))
260260
t.ok(await doesNotProxy('http://example:1337'))
261-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
261+
t.ok(await doesNotProxy('http://sub.example'))
262262
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://prefexample'))
263263
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.no'))
264-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://a.b.example'))
264+
t.ok(await doesNotProxy('http://a.b.example'))
265265
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://host/example'))
266266
return dispatcher.close()
267267
})
@@ -273,10 +273,10 @@ describe('no_proxy', () => {
273273
t.ok(await doesNotProxy('http://example:80'))
274274
t.ok(await doesNotProxy('http://example:0'))
275275
t.ok(await doesNotProxy('http://example:1337'))
276-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
276+
t.ok(await doesNotProxy('http://sub.example'))
277277
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://prefexample'))
278278
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.no'))
279-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://a.b.example'))
279+
t.ok(await doesNotProxy('http://a.b.example'))
280280
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://host/example'))
281281
return dispatcher.close()
282282
})
@@ -290,38 +290,39 @@ describe('no_proxy', () => {
290290
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:0'))
291291
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:1337'))
292292
t.ok(await doesNotProxy('http://sub.example'))
293-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://no.sub.example'))
293+
t.ok(await doesNotProxy('http://no.sub.example'))
294294
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub-example'))
295295
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.sub'))
296296
return dispatcher.close()
297297
})
298298

299299
test('host + port', async (t) => {
300-
t = tspl(t, { plan: 12 })
300+
t = tspl(t, { plan: 13 })
301301
process.env.no_proxy = 'example:80, localhost:3000'
302-
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(12)
302+
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(13)
303303
t.ok(await doesNotProxy('http://example'))
304304
t.ok(await doesNotProxy('http://example:80'))
305+
t.ok(await doesNotProxy('http://sub.example:80'))
305306
t.ok(await doesNotProxy('http://example:0'))
306307
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:1337'))
307-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
308+
t.ok(await doesNotProxy('http://sub.example'))
308309
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://prefexample'))
309310
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.no'))
310-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://a.b.example'))
311+
t.ok(await doesNotProxy('http://a.b.example'))
311312
t.ok(await doesNotProxy('http://localhost:3000/'))
312313
t.ok(await doesNotProxy('https://localhost:3000/'))
313314
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://localhost:3001/'))
314315
t.ok(await usesProxyAgent(kHttpsProxyAgent, 'https://localhost:3001/'))
315316
return dispatcher.close()
316317
})
317318

318-
test('host suffix', async (t) => {
319+
test('host suffix - leading dot stripped', async (t) => {
319320
t = tspl(t, { plan: 9 })
320321
process.env.no_proxy = '.example'
321322
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(9)
322-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example'))
323-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:80'))
324-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:1337'))
323+
t.ok(await doesNotProxy('http://example'))
324+
t.ok(await doesNotProxy('http://example:80'))
325+
t.ok(await doesNotProxy('http://example:1337'))
325326
t.ok(await doesNotProxy('http://sub.example'))
326327
t.ok(await doesNotProxy('http://sub.example:80'))
327328
t.ok(await doesNotProxy('http://sub.example:1337'))
@@ -331,13 +332,13 @@ describe('no_proxy', () => {
331332
return dispatcher.close()
332333
})
333334

334-
test('host suffix with *.', async (t) => {
335+
test('host suffix with *. - leading dot with asterisk stripped', async (t) => {
335336
t = tspl(t, { plan: 9 })
336337
process.env.no_proxy = '*.example'
337338
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(9)
338-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example'))
339-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:80'))
340-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:1337'))
339+
t.ok(await doesNotProxy('http://example'))
340+
t.ok(await doesNotProxy('http://example:80'))
341+
t.ok(await doesNotProxy('http://example:1337'))
341342
t.ok(await doesNotProxy('http://sub.example'))
342343
t.ok(await doesNotProxy('http://sub.example:80'))
343344
t.ok(await doesNotProxy('http://sub.example:1337'))
@@ -347,20 +348,16 @@ describe('no_proxy', () => {
347348
return dispatcher.close()
348349
})
349350

350-
test('substring suffix', async (t) => {
351-
t = tspl(t, { plan: 10 })
351+
test('substring suffix are NOT supported', async (t) => {
352+
t = tspl(t, { plan: 6 })
352353
process.env.no_proxy = '*example'
353-
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(10)
354-
t.ok(await doesNotProxy('http://example'))
355-
t.ok(await doesNotProxy('http://example:80'))
356-
t.ok(await doesNotProxy('http://example:1337'))
357-
t.ok(await doesNotProxy('http://sub.example'))
358-
t.ok(await doesNotProxy('http://sub.example:80'))
359-
t.ok(await doesNotProxy('http://sub.example:1337'))
360-
t.ok(await doesNotProxy('http://prefexample'))
361-
t.ok(await doesNotProxy('http://a.b.example'))
362-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.no'))
363-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://host/example'))
354+
const { dispatcher, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(6)
355+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example'))
356+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
357+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
358+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://prefexample'))
359+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://x.prefexample'))
360+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://a.b.example'))
364361
return dispatcher.close()
365362
})
366363

@@ -442,11 +439,11 @@ describe('no_proxy', () => {
442439

443440
test('prefers lowercase over uppercase', async (t) => {
444441
t = tspl(t, { plan: 2 })
445-
process.env.NO_PROXY = 'sub.example.com'
442+
process.env.NO_PROXY = 'another.com'
446443
process.env.no_proxy = 'example.com'
447444
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(6)
448445
t.ok(await doesNotProxy('http://example.com'))
449-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example.com'))
446+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://another.com'))
450447
return dispatcher.close()
451448
})
452449

@@ -464,21 +461,21 @@ describe('no_proxy', () => {
464461
process.env.no_proxy = 'example.com'
465462
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(4)
466463
t.ok(await doesNotProxy('http://example.com'))
467-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example.com'))
468-
process.env.no_proxy = 'sub.example.com'
464+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://another.com'))
465+
process.env.no_proxy = 'another.com'
469466
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.com'))
470-
t.ok(await doesNotProxy('http://sub.example.com'))
467+
t.ok(await doesNotProxy('http://another.com'))
471468
return dispatcher.close()
472469
})
473470

474471
test('ignores env var changes when set via config', async (t) => {
475472
t = tspl(t, { plan: 4 })
476473
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(4, { noProxy: 'example.com' })
477474
t.ok(await doesNotProxy('http://example.com'))
478-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example.com'))
479-
process.env.no_proxy = 'sub.example.com'
475+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://another.com'))
476+
process.env.no_proxy = 'another.com'
480477
t.ok(await doesNotProxy('http://example.com'))
481-
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example.com'))
478+
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://another.com'))
482479
return dispatcher.close()
483480
})
484481
})

0 commit comments

Comments
 (0)