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

Fix processing of proxybypass variables from environment #286

Merged
2 changes: 2 additions & 0 deletions lib/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Used by buildProxyBypassRegexFromEnv for escaping dot symbols in NO_PROXY hosts' strings
export const searchRegExpToReplaceSpecialChars: RegExp = new RegExp('(?<!\\\\)([.])(?!\\*)', 'g');
19 changes: 9 additions & 10 deletions lib/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as url from 'url';
import * as path from 'path';
import zlib = require('zlib');
import { IRequestQueryParams, IHttpClientResponse } from './Interfaces';
import { searchRegExpToReplaceSpecialChars } from './Constants';

/**
* creates an url from a request url and optional base url (http://server:8080)
Expand Down Expand Up @@ -107,17 +108,15 @@ export async function decompressGzippedContent(buffer: Buffer, charset?: string)
* @return {RegExp}
*/
export function buildProxyBypassRegexFromEnv(bypass : string) : RegExp {
try {
// We need to keep this around for back-compat purposes
return new RegExp(bypass, 'i')
}
catch(err) {
if (err instanceof SyntaxError && (bypass || "").startsWith("*")) {
let wildcardEscaped = bypass.replace('*', '(.*)');
return new RegExp(wildcardEscaped, 'i');
}
throw err;
// check if expression starts with asterisk and replace it with .*
if (bypass && bypass.startsWith("*")) {
bypass = bypass.replace("*", ".*");
}

// replace all . symbols in string by \. because point is a special character
const safeRegex = (bypass || "").replace(searchRegExpToReplaceSpecialChars, '\\$1');

return new RegExp(safeRegex, 'i');
EzzhevNikita marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typed-rest-client",
"version": "1.8.4",
"version": "1.8.5",
"description": "Node Rest and Http Clients for use with TypeScript",
"main": "./RestClient.js",
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions test/units/utiltests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ describe('Util Tests', function () {
assert.equal(bypassed, true);
});

});
it('not bypasses if domain pattern starts with .', () => {
let regExp = util.buildProxyBypassRegexFromEnv('.intranet');
assert(regExp, 'regExp should not be null');
let parsedUrl = url.parse("https://keyvault.vault.azure.net/secrets/test-secret1-intranet");
let bypassed = regExp.test(parsedUrl.href);
assert.equal(bypassed, false);
});

it('bypasses if domain pattern starts with .', () => {
let regExp = util.buildProxyBypassRegexFromEnv('.net');
assert(regExp, 'regExp should not be null');
let parsedUrl = url.parse("https://keyvault.vault.azure.net/secrets/test-secret1-intranet");
let bypassed = regExp.test(parsedUrl.href);
assert.equal(bypassed, true);
});

});
it('bypasses if domain pattern starts with . and contains complex domain', () => {
let regExp = util.buildProxyBypassRegexFromEnv('.azure.net');
assert(regExp, 'regExp should not be null');
let parsedUrl = url.parse("https://keyvault.vault.azure.net/secrets/test-secret1-intranet");
let bypassed = regExp.test(parsedUrl.href);
assert.equal(bypassed, true);
});
});
});