Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions lib/dispatcher/env-http-proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,14 @@ class EnvHttpProxyAgent extends DispatcherBase {
if (entry.port && entry.port !== port) {
continue // Skip if ports don't match.
}
if (!/^[.*]/.test(entry.hostname)) {
// No wildcards, so don't proxy only if there is not an exact match.
if (hostname === entry.hostname) {
return false
}
} else {
// Don't proxy if the hostname ends with the no_proxy host.
if (hostname.endsWith(entry.hostname.replace(/^\*/, ''))) {
return false
}
// Don't proxy if the hostname is equal with the no_proxy host.
if (hostname === entry.hostname) {
return false
}
// Don't proxy if the hostname is the subdomain of the no_proxy host.
// Reference - https://github.com/denoland/deno/blob/6fbce91e40cc07fc6da74068e5cc56fdd40f7b4c/ext/fetch/proxy.rs#L485
if (hostname.slice(-(entry.hostname.length + 1)) === `.${entry.hostname}`) {
return false
}
}

Expand All @@ -123,7 +121,8 @@ class EnvHttpProxyAgent extends DispatcherBase {
}
const parsed = entry.match(/^(.+):(\d+)$/)
noProxyEntries.push({
hostname: (parsed ? parsed[1] : entry).toLowerCase(),
// strip leading dot or asterisk with dot
hostname: (parsed ? parsed[1] : entry).replace(/^\*?\./, '').toLowerCase(),
port: parsed ? Number.parseInt(parsed[2], 10) : 0
})
}
Expand Down
73 changes: 35 additions & 38 deletions test/env-http-proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ describe('no_proxy', () => {
t.ok(await doesNotProxy('http://example:80'))
t.ok(await doesNotProxy('http://example:0'))
t.ok(await doesNotProxy('http://example:1337'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
t.ok(await doesNotProxy('http://sub.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://prefexample'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.no'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://a.b.example'))
t.ok(await doesNotProxy('http://a.b.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://host/example'))
return dispatcher.close()
})
Expand All @@ -273,10 +273,10 @@ describe('no_proxy', () => {
t.ok(await doesNotProxy('http://example:80'))
t.ok(await doesNotProxy('http://example:0'))
t.ok(await doesNotProxy('http://example:1337'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
t.ok(await doesNotProxy('http://sub.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://prefexample'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.no'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://a.b.example'))
t.ok(await doesNotProxy('http://a.b.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://host/example'))
return dispatcher.close()
})
Expand All @@ -290,38 +290,39 @@ describe('no_proxy', () => {
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:0'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:1337'))
t.ok(await doesNotProxy('http://sub.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://no.sub.example'))
t.ok(await doesNotProxy('http://no.sub.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub-example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.sub'))
return dispatcher.close()
})

test('host + port', async (t) => {
t = tspl(t, { plan: 12 })
t = tspl(t, { plan: 13 })
process.env.no_proxy = 'example:80, localhost:3000'
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(12)
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(13)
t.ok(await doesNotProxy('http://example'))
t.ok(await doesNotProxy('http://example:80'))
t.ok(await doesNotProxy('http://sub.example:80'))
t.ok(await doesNotProxy('http://example:0'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:1337'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
t.ok(await doesNotProxy('http://sub.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://prefexample'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.no'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://a.b.example'))
t.ok(await doesNotProxy('http://a.b.example'))
t.ok(await doesNotProxy('http://localhost:3000/'))
t.ok(await doesNotProxy('https://localhost:3000/'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://localhost:3001/'))
t.ok(await usesProxyAgent(kHttpsProxyAgent, 'https://localhost:3001/'))
return dispatcher.close()
})

test('host suffix', async (t) => {
test('host suffix - leading dot stripped', async (t) => {
t = tspl(t, { plan: 9 })
process.env.no_proxy = '.example'
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(9)
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:80'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:1337'))
t.ok(await doesNotProxy('http://example'))
t.ok(await doesNotProxy('http://example:80'))
t.ok(await doesNotProxy('http://example:1337'))
t.ok(await doesNotProxy('http://sub.example'))
t.ok(await doesNotProxy('http://sub.example:80'))
t.ok(await doesNotProxy('http://sub.example:1337'))
Expand All @@ -331,13 +332,13 @@ describe('no_proxy', () => {
return dispatcher.close()
})

test('host suffix with *.', async (t) => {
test('host suffix with *. - leading dot with asterisk stripped', async (t) => {
t = tspl(t, { plan: 9 })
process.env.no_proxy = '*.example'
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(9)
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:80'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example:1337'))
t.ok(await doesNotProxy('http://example'))
t.ok(await doesNotProxy('http://example:80'))
t.ok(await doesNotProxy('http://example:1337'))
t.ok(await doesNotProxy('http://sub.example'))
t.ok(await doesNotProxy('http://sub.example:80'))
t.ok(await doesNotProxy('http://sub.example:1337'))
Expand All @@ -347,20 +348,16 @@ describe('no_proxy', () => {
return dispatcher.close()
})

test('substring suffix', async (t) => {
t = tspl(t, { plan: 10 })
test('substring suffix are NOT supported', async (t) => {
t = tspl(t, { plan: 6 })
process.env.no_proxy = '*example'
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(10)
t.ok(await doesNotProxy('http://example'))
t.ok(await doesNotProxy('http://example:80'))
t.ok(await doesNotProxy('http://example:1337'))
t.ok(await doesNotProxy('http://sub.example'))
t.ok(await doesNotProxy('http://sub.example:80'))
t.ok(await doesNotProxy('http://sub.example:1337'))
t.ok(await doesNotProxy('http://prefexample'))
t.ok(await doesNotProxy('http://a.b.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.no'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://host/example'))
const { dispatcher, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(6)
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://prefexample'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://x.prefexample'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://a.b.example'))
return dispatcher.close()
})

Expand Down Expand Up @@ -442,11 +439,11 @@ describe('no_proxy', () => {

test('prefers lowercase over uppercase', async (t) => {
t = tspl(t, { plan: 2 })
process.env.NO_PROXY = 'sub.example.com'
process.env.NO_PROXY = 'another.com'
process.env.no_proxy = 'example.com'
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(6)
t.ok(await doesNotProxy('http://example.com'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example.com'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://another.com'))
return dispatcher.close()
})

Expand All @@ -464,21 +461,21 @@ describe('no_proxy', () => {
process.env.no_proxy = 'example.com'
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(4)
t.ok(await doesNotProxy('http://example.com'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example.com'))
process.env.no_proxy = 'sub.example.com'
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://another.com'))
process.env.no_proxy = 'another.com'
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://example.com'))
t.ok(await doesNotProxy('http://sub.example.com'))
t.ok(await doesNotProxy('http://another.com'))
return dispatcher.close()
})

test('ignores env var changes when set via config', async (t) => {
t = tspl(t, { plan: 4 })
const { dispatcher, doesNotProxy, usesProxyAgent } = createEnvHttpProxyAgentWithMocks(4, { noProxy: 'example.com' })
t.ok(await doesNotProxy('http://example.com'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example.com'))
process.env.no_proxy = 'sub.example.com'
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://another.com'))
process.env.no_proxy = 'another.com'
t.ok(await doesNotProxy('http://example.com'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://sub.example.com'))
t.ok(await usesProxyAgent(kHttpProxyAgent, 'http://another.com'))
return dispatcher.close()
})
})
Loading