From 8cf07b08d2fc01acec775ca58459a5a822023084 Mon Sep 17 00:00:00 2001 From: Przemyslaw Motacki Date: Mon, 25 Nov 2024 17:50:06 +0100 Subject: [PATCH] SNOW-1825719 - setting proxy port if default was skipped during parsing --- 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..a837e7040 100644 --- a/lib/util.js +++ b/lib/util.js @@ -862,6 +862,13 @@ exports.validateEmptyString = function (value) { }; exports.getProxyFromEnv = function (isHttps = true) { + const getDeafaultPortIfNotSet = (proxyFromEnv, isHttps) => { + if (!proxyFromEnv.port) { + return isHttps ? 443 : 80; + } else { + return proxyFromEnv.port; + } + }; const protocol = isHttps ? 'https' : 'http'; let proxyFromEnv = this.getEnvVar(`${protocol}_proxy`); if (!proxyFromEnv){ @@ -872,11 +879,13 @@ exports.getProxyFromEnv = function (isHttps = true) { if (proxyFromEnv.indexOf('://') === -1) { Logger.getInstance().info('Util.getProxyEnv: the protocol was missing from the environment proxy. Use the HTTP protocol.'); proxyFromEnv = 'http' + '://' + proxyFromEnv; + isHttps = false; } proxyFromEnv = new URL(proxyFromEnv); + const port = getDeafaultPortIfNotSet(proxyFromEnv, isHttps); const proxy = { host: this.validateEmptyString(proxyFromEnv.hostname), - port: Number(this.validateEmptyString(proxyFromEnv.port)), + port: Number(this.validateEmptyString(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 }) => {