diff --git a/lib/dispatcher/env-http-proxy-agent.js b/lib/dispatcher/env-http-proxy-agent.js index f88437f1936..9a602f465d4 100644 --- a/lib/dispatcher/env-http-proxy-agent.js +++ b/lib/dispatcher/env-http-proxy-agent.js @@ -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 @@ -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] } diff --git a/test/env-http-proxy-agent.js b/test/env-http-proxy-agent.js index 97969cfaf20..483a5eec886 100644 --- a/test/env-http-proxy-agent.js +++ b/test/env-http-proxy-agent.js @@ -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'