Skip to content
Closed
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
8 changes: 6 additions & 2 deletions lib/dispatcher/env-http-proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const DEFAULT_PORTS = {
'https:': 443
}

function normalizeProxyURI (proxy, defaultScheme = 'http://') {
return /^[a-zA-Z][a-zA-Z\d+.-]*:\/\//.test(proxy) ? proxy : `${defaultScheme}${proxy}`
}

class EnvHttpProxyAgent extends DispatcherBase {
#noProxyValue = null
#noProxyEntries = null
Expand All @@ -25,14 +29,14 @@ class EnvHttpProxyAgent extends DispatcherBase {

const HTTP_PROXY = httpProxy ?? process.env.http_proxy ?? process.env.HTTP_PROXY
if (HTTP_PROXY) {
this[kHttpProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTP_PROXY })
this[kHttpProxyAgent] = new ProxyAgent({ ...agentOpts, uri: normalizeProxyURI(HTTP_PROXY, 'http://') })
} else {
this[kHttpProxyAgent] = this[kNoProxyAgent]
}

const HTTPS_PROXY = httpsProxy ?? process.env.https_proxy ?? process.env.HTTPS_PROXY
if (HTTPS_PROXY) {
this[kHttpsProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTPS_PROXY })
this[kHttpsProxyAgent] = new ProxyAgent({ ...agentOpts, uri: normalizeProxyURI(HTTPS_PROXY, 'https://') })
} else {
this[kHttpsProxyAgent] = this[kHttpProxyAgent]
}
Expand Down
10 changes: 10 additions & 0 deletions test/env-http-proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ test('creates separate proxy agent for http and https when http_proxy and https_
return dispatcher.close()
})

test('assumes protocol-specific scheme for schema-less proxy env vars', async (t) => {
t = tspl(t, { plan: 2 })
process.env.http_proxy = 'localhost:8080'
process.env.https_proxy = 'localhost:8443'
const dispatcher = new EnvHttpProxyAgent()
t.equal(dispatcher[kHttpProxyAgent][kProxy].uri, 'http://localhost:8080/')
t.equal(dispatcher[kHttpsProxyAgent][kProxy].uri, 'https://localhost:8443/')
return dispatcher.close()
})

test('handles uppercase HTTP_PROXY and HTTPS_PROXY', async (t) => {
t = tspl(t, { plan: 6 })
process.env.HTTP_PROXY = 'http://example.com:8080'
Expand Down
Loading