From 753aa3d41943c0ade92228802c5498b639cfecf4 Mon Sep 17 00:00:00 2001 From: Przemyslaw Motacki Date: Wed, 27 Nov 2024 08:52:15 +0100 Subject: [PATCH] SNOW-1825719 - setting proxy port if default was skipped during parsing (#972) --- lib/util.js | 11 ++++++++++- test/unit/util_test.js | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index dd1a3be07..90db5d079 100644 --- a/lib/util.js +++ b/lib/util.js @@ -862,6 +862,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 = this.getEnvVar(`${protocol}_proxy`); if (!proxyFromEnv){ @@ -874,9 +882,10 @@ exports.getProxyFromEnv = function (isHttps = true) { proxyFromEnv = 'http' + '://' + proxyFromEnv; } proxyFromEnv = new URL(proxyFromEnv); + const port = getDefaultPortIfNotSet(proxyFromEnv); const proxy = { host: this.validateEmptyString(proxyFromEnv.hostname), - port: Number(this.validateEmptyString(proxyFromEnv.port)), + port: Number(port), user: this.validateEmptyString(proxyFromEnv.username), password: this.validateEmptyString(proxyFromEnv.password), protocol: this.validateEmptyString(proxyFromEnv.protocol), diff --git a/test/unit/util_test.js b/test/unit/util_test.js index feaa48541..e48e8f400 100644 --- a/test/unit/util_test.js +++ b/test/unit/util_test.js @@ -1357,7 +1357,7 @@ describe('Util', 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: { @@ -1372,7 +1372,7 @@ describe('Util', 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', @@ -1394,6 +1394,42 @@ describe('Util', 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 }) => {