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
26 changes: 24 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,22 @@ const DEFAULT_PORTS = {
'https:': 443
}

/**
* Normalizes a proxy URL by prepending a scheme if one is missing.
* This matches the behavior of curl and Go's httpproxy package, which
* assume http:// for scheme-less proxy values.
*
* @param {string} proxyUrl - The proxy URL to normalize
* @param {string} defaultScheme - The scheme to prepend if missing ('http' or 'https')
* @returns {string} The normalized proxy URL
*/
function normalizeProxyUrl (proxyUrl, defaultScheme) {
if (!proxyUrl) return proxyUrl
// If the value already contains a scheme (e.g. http://, https://, socks5://), return as-is
if (/^[a-z][a-z0-9+\-.]*:\/\//i.test(proxyUrl)) return proxyUrl
return `${defaultScheme}://${proxyUrl}`
}

class EnvHttpProxyAgent extends DispatcherBase {
#noProxyValue = null
#noProxyEntries = null
Expand All @@ -23,14 +39,20 @@ class EnvHttpProxyAgent extends DispatcherBase {

this[kNoProxyAgent] = new Agent(agentOpts)

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

const HTTPS_PROXY = httpsProxy ?? process.env.https_proxy ?? process.env.HTTPS_PROXY
const HTTPS_PROXY = normalizeProxyUrl(
httpsProxy ?? process.env.https_proxy ?? process.env.HTTPS_PROXY,
'https'
)
if (HTTPS_PROXY) {
this[kHttpsProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTPS_PROXY })
} else {
Expand Down
55 changes: 55 additions & 0 deletions test/env-http-proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,61 @@ test('prefers lowercase over uppercase env vars even when empty', async (t) => {
return dispatcher.close()
})

test('handles scheme-less HTTP_PROXY by assuming http://', async (t) => {
t = tspl(t, { plan: 2 })
process.env.HTTP_PROXY = 'example.com:8080'
const dispatcher = new EnvHttpProxyAgent()
t.ok(dispatcher[kHttpProxyAgent] instanceof ProxyAgent)
t.equal(dispatcher[kHttpProxyAgent][kProxy].uri, 'http://example.com:8080/')
return dispatcher.close()
})

test('handles scheme-less HTTPS_PROXY by assuming https://', async (t) => {
t = tspl(t, { plan: 2 })
process.env.HTTPS_PROXY = 'example.com:8443'
const dispatcher = new EnvHttpProxyAgent()
t.ok(dispatcher[kHttpsProxyAgent] instanceof ProxyAgent)
t.equal(dispatcher[kHttpsProxyAgent][kProxy].uri, 'https://example.com:8443/')
return dispatcher.close()
})

test('handles scheme-less httpProxy option by assuming http://', async (t) => {
t = tspl(t, { plan: 2 })
const dispatcher = new EnvHttpProxyAgent({ httpProxy: 'myproxy:9000' })
t.ok(dispatcher[kHttpProxyAgent] instanceof ProxyAgent)
t.equal(dispatcher[kHttpProxyAgent][kProxy].uri, 'http://myproxy:9000/')
return dispatcher.close()
})

test('handles scheme-less httpsProxy option by assuming https://', async (t) => {
t = tspl(t, { plan: 2 })
const dispatcher = new EnvHttpProxyAgent({ httpsProxy: 'myproxy:9443' })
t.ok(dispatcher[kHttpsProxyAgent] instanceof ProxyAgent)
t.equal(dispatcher[kHttpsProxyAgent][kProxy].uri, 'https://myproxy:9443/')
return dispatcher.close()
})

test('does not modify proxy URLs that already have a scheme', async (t) => {
t = tspl(t, { plan: 4 })
process.env.HTTP_PROXY = 'http://example.com:8080'
process.env.HTTPS_PROXY = 'http://example.com:8443'
const dispatcher = new EnvHttpProxyAgent()
t.ok(dispatcher[kHttpProxyAgent] instanceof ProxyAgent)
t.equal(dispatcher[kHttpProxyAgent][kProxy].uri, 'http://example.com:8080/')
t.ok(dispatcher[kHttpsProxyAgent] instanceof ProxyAgent)
t.equal(dispatcher[kHttpsProxyAgent][kProxy].uri, 'http://example.com:8443/')
return dispatcher.close()
})

test('handles scheme-less hostname-only proxy values', async (t) => {
t = tspl(t, { plan: 2 })
process.env.HTTP_PROXY = 'myproxy'
const dispatcher = new EnvHttpProxyAgent()
t.ok(dispatcher[kHttpProxyAgent] instanceof ProxyAgent)
t.equal(dispatcher[kHttpProxyAgent][kProxy].uri, 'http://myproxy/')
return dispatcher.close()
})

test('creates a proxy agent only for https when only https_proxy is set', async (t) => {
t = tspl(t, { plan: 5 })
process.env.https_proxy = 'http://example.com:8443'
Expand Down
Loading