Skip to content

Commit 935b546

Browse files
authored
Merge pull request #286 from microsoft/users/EzzhevNikita/fix-processing-of-proxybypass-variables
Fix processing of proxybypass variables from environment
2 parents 9397d2e + fd36d95 commit 935b546

File tree

6 files changed

+43
-20
lines changed

6 files changed

+43
-20
lines changed

lib/Constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Used by buildProxyBypassRegexFromEnv for escaping dot symbols in NO_PROXY hosts' strings
2+
export const searchRegExpToReplaceSpecialChars: RegExp = new RegExp('(?<!\\\\)([.])(?!\\*)', 'g');

lib/Util.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as url from 'url';
66
import * as path from 'path';
77
import zlib = require('zlib');
88
import { IRequestQueryParams, IHttpClientResponse } from './Interfaces';
9+
import { searchRegExpToReplaceSpecialChars } from './Constants';
910

1011
/**
1112
* creates an url from a request url and optional base url (http://server:8080)
@@ -107,17 +108,15 @@ export async function decompressGzippedContent(buffer: Buffer, charset?: string)
107108
* @return {RegExp}
108109
*/
109110
export function buildProxyBypassRegexFromEnv(bypass : string) : RegExp {
110-
try {
111-
// We need to keep this around for back-compat purposes
112-
return new RegExp(bypass, 'i')
113-
}
114-
catch(err) {
115-
if (err instanceof SyntaxError && (bypass || "").startsWith("*")) {
116-
let wildcardEscaped = bypass.replace('*', '(.*)');
117-
return new RegExp(wildcardEscaped, 'i');
118-
}
119-
throw err;
111+
// check if expression starts with asterisk and replace it with .*
112+
if (bypass && bypass.startsWith("*")) {
113+
bypass = bypass.replace("*", ".*");
120114
}
115+
116+
// replace all . symbols in string by \. because point is a special character
117+
const safeRegex = (bypass || "").replace(searchRegExpToReplaceSpecialChars, '\\$1');
118+
119+
return new RegExp(safeRegex, 'i');
121120
}
122121

123122
/**

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typed-rest-client",
3-
"version": "1.8.4",
3+
"version": "1.8.5",
44
"description": "Node Rest and Http Clients for use with TypeScript",
55
"main": "./RestClient.js",
66
"scripts": {

test/package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/units/utiltests.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ describe('Util Tests', function () {
4141
assert.equal(bypassed, true);
4242
});
4343

44-
});
44+
it('not bypasses if domain pattern starts with .', () => {
45+
let regExp = util.buildProxyBypassRegexFromEnv('.intranet');
46+
assert(regExp, 'regExp should not be null');
47+
let parsedUrl = url.parse("https://keyvault.vault.azure.net/secrets/test-secret1-intranet");
48+
let bypassed = regExp.test(parsedUrl.href);
49+
assert.equal(bypassed, false);
50+
});
51+
52+
it('bypasses if domain pattern starts with .', () => {
53+
let regExp = util.buildProxyBypassRegexFromEnv('.net');
54+
assert(regExp, 'regExp should not be null');
55+
let parsedUrl = url.parse("https://keyvault.vault.azure.net/secrets/test-secret1-intranet");
56+
let bypassed = regExp.test(parsedUrl.href);
57+
assert.equal(bypassed, true);
58+
});
4559

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

0 commit comments

Comments
 (0)