diff --git a/lib/proxy_util.js b/lib/proxy_util.js index 01f02d34f..e4097b6dc 100644 --- a/lib/proxy_util.js +++ b/lib/proxy_util.js @@ -129,6 +129,14 @@ exports.validateEmptyString = function (value) { }; exports.getProxyFromEnv = function (isHttps = true) { + const getDefaultPortIfNotSet = (proxyFromEnv) => { + const isProxyProtocolHttps = proxyFromEnv.protocol === 'https:'; + if (!proxyFromEnv.port) { + return isProxyProtocolHttps ? 443 : 80; + } else { + return proxyFromEnv.port; + } + }; const protocol = isHttps ? 'https' : 'http'; let proxyFromEnv = Util.getEnvVar(`${protocol}_proxy`); if (!proxyFromEnv){ @@ -141,9 +149,10 @@ exports.getProxyFromEnv = function (isHttps = true) { proxyFromEnv = 'http' + '://' + proxyFromEnv; } proxyFromEnv = new URL(proxyFromEnv); + const port = getDefaultPortIfNotSet(proxyFromEnv); const proxy = { host: Util.validateEmptyString(proxyFromEnv.hostname), - port: Number(Util.validateEmptyString(proxyFromEnv.port)), + port: Number(port), user: Util.validateEmptyString(proxyFromEnv.username), password: Util.validateEmptyString(proxyFromEnv.password), protocol: Util.validateEmptyString(proxyFromEnv.protocol), diff --git a/test/unit/proxy_util_test.js b/test/unit/proxy_util_test.js index efc72371b..55c9d57d0 100644 --- a/test/unit/proxy_util_test.js +++ b/test/unit/proxy_util_test.js @@ -150,7 +150,7 @@ describe('getProxyEnv function test ', function () { { name: 'HTTP PROXY with authentication', isHttps: false, - httpProxy: 'http://hello:world@proxy.example.com:8080', + httpProxy: 'http://hello:world@proxy.example.com:8080', //# pragma: allowlist secret httpsProxy: undefined, noProxy: '*.amazonaws.com,*.my_company.com', result: { @@ -165,7 +165,7 @@ describe('getProxyEnv function test ', function () { { name: 'HTTPS PROXY with authentication without NO proxy', isHttps: true, - httpsProxy: 'https://user:pass@myproxy.server.com:1234', + httpsProxy: 'https://user:pass@myproxy.server.com:1234', //# pragma: allowlist secret result: { host: 'myproxy.server.com', user: 'user', @@ -187,6 +187,42 @@ describe('getProxyEnv function test ', function () { noProxy: '*.amazonaws.com|*.my_company.com|*.test.com', }, }, + { + name: 'HTTPS PROXY with authentication without port and protocol', + isHttps: true, + noProxy: '*.amazonaws.com,*.my_company.com,*.test.com', + httpsProxy: 'myproxy.server.com', + result: { + host: 'myproxy.server.com', + port: 80, + protocol: 'http:', + noProxy: '*.amazonaws.com|*.my_company.com|*.test.com', + }, + }, + { + name: 'HTTP PROXY with authentication without port and protocol', + isHttps: false, + noProxy: '*.amazonaws.com,*.my_company.com,*.test.com', + httpProxy: 'myproxy.server.com', + result: { + host: 'myproxy.server.com', + port: 80, + protocol: 'http:', + noProxy: '*.amazonaws.com|*.my_company.com|*.test.com', + }, + }, + { + name: 'HTTPS PROXY with authentication without port', + isHttps: true, + noProxy: '*.amazonaws.com,*.my_company.com,*.test.com', + httpsProxy: 'https://myproxy.server.com', + result: { + host: 'myproxy.server.com', + port: 443, + protocol: 'https:', + noProxy: '*.amazonaws.com|*.my_company.com|*.test.com', + } + } ]; testCases.forEach(({ name, isHttps, httpsProxy, httpProxy, noProxy, result }) => { @@ -265,11 +301,11 @@ describe('Proxy Util for Azure', function () { it('test hide and restore environment proxy', function () { const testCases = { - httpProxy: 'https://user:pass@myproxy.server.com:1234', - httpsProxy: 'https://user:pass@myproxy.server.com:1234', + httpProxy: 'https://user:pass@myproxy.server.com:1234', //# pragma: allowlist secret + httpsProxy: 'https://user:pass@myproxy.server.com:1234', //# pragma: allowlist secret noProxy: '*.amazonaws.com,*.my_company.com', - HttpProxy: 'https://user:pass@myproxy2.server.com:1234', - HttpsProxy: 'https://user:pass@myproxy2.server.com:1234', + HttpProxy: 'https://user:pass@myproxy2.server.com:1234', //# pragma: allowlist secret + HttpsProxy: 'https://user:pass@myproxy2.server.com:1234', //# pragma: allowlist secret NoProxy: '*.amazonaws2.com,*.my_company2.com', };