Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1825719 - setting proxy port if default was skipped during parsing #972

Merged
merged 6 commits into from
Nov 27, 2024
11 changes: 10 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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),
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