Skip to content

Commit

Permalink
SNOW-1825719 - setting proxy port if default was skipped during parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pmotacki committed Nov 25, 2024
1 parent c4e6edb commit 8cf07b0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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),
Expand Down
40 changes: 38 additions & 2 deletions test/unit/util_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ describe('Util', function () {
{
name: 'HTTP PROXY with authentication',
isHttps: false,
httpProxy: 'http://hello:[email protected]:8080',
httpProxy: 'http://hello:[email protected]:8080', //# pragma: allowlist secret
httpsProxy: undefined,
noProxy: '*.amazonaws.com,*.my_company.com',
result: {
Expand All @@ -1372,7 +1372,7 @@ describe('Util', function () {
{
name: 'HTTPS PROXY with authentication without NO proxy',
isHttps: true,
httpsProxy: 'https://user:[email protected]:1234',
httpsProxy: 'https://user:[email protected]:1234', //# pragma: allowlist secret
result: {
host: 'myproxy.server.com',
user: 'user',
Expand All @@ -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 }) => {
Expand Down

0 comments on commit 8cf07b0

Please sign in to comment.