Fix constructor on NO_PROXY with wildcard domain (#216)

* fix contrstructor on NO_PROXY with wildcard domain

* escape * in NO_PROXY regex only if needed
keep the original behavior for back-compat purposes

* patch for no proxy with wildcards
This commit is contained in:
tuwrraphael 2021-03-09 16:21:46 +01:00 коммит произвёл GitHub
Родитель 305ef3e65b
Коммит f21ab8a105
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 111 добавлений и 3 удалений

Просмотреть файл

@ -128,7 +128,7 @@ export class HttpClient implements ifm.IHttpClient {
if (no_proxy) {
this._httpProxyBypassHosts = [];
no_proxy.split(',').forEach(bypass => {
this._httpProxyBypassHosts.push(new RegExp(bypass, 'i'));
this._httpProxyBypassHosts.push(util.buildProxyBypassRegexFromEnv(bypass));
});
}

Просмотреть файл

@ -98,6 +98,28 @@ export async function decompressGzippedContent(buffer: Buffer, charset?: string)
})
}
/**
* Builds a RegExp to test urls against for deciding
* wether to bypass proxy from an entry of the
* environment variable setting NO_PROXY
*
* @param {string} bypass
* @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;
}
}
/**
* Obtain Response's Content Charset.
* Through inspecting `content-type` response header.

2
package-lock.json сгенерированный
Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "typed-rest-client",
"version": "1.8.2",
"version": "1.8.3",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "typed-rest-client",
"version": "1.8.2",
"version": "1.8.3",
"description": "Node Rest and Http Clients for use with TypeScript",
"main": "./RestClient.js",
"scripts": {

Просмотреть файл

@ -462,3 +462,43 @@ describe('Http Tests with keepAlive', function () {
assert(res.message.statusCode == 501, "status code should be 501");
});
});
describe('Http Tests with NO_PROXY environment variable', function () {
let noProxyBeforeTest = undefined;
before(() => {
});
after(() => {
});
beforeEach(() => {
noProxyBeforeTest = process.env["NO_PROXY"];
});
afterEach(() => {
if (undefined !== noProxyBeforeTest) {
process.env["NO_PROXY"] = noProxyBeforeTest;
} else {
delete process.env["NO_PROXY"];
}
})
it('constructs with NO_PROXY', () => {
this.timeout(1000);
process.env["NO_PROXY"] = "microsoft.com";
let http: httpm.HttpClient = new httpm.HttpClient('typed-test-client-tests');
assert(http, 'http client should not be null');
assert(http.userAgent, 'user-agent should not be null')
});
it('constructs with wildcard in NO_PROXY', () => {
this.timeout(1000);
process.env["NO_PROXY"] = "*.microsoft.com";
let http: httpm.HttpClient = new httpm.HttpClient('typed-test-client-tests');
assert(http, 'http client should not be null');
assert(http.userAgent, 'user-agent should not be null')
});
});

46
test/units/utiltests.ts Normal file
Просмотреть файл

@ -0,0 +1,46 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import assert = require('assert');
import url = require("url");
import * as util from '../../lib/Util';
describe('Util Tests', function () {
before(() => {
});
after(() => {
});
describe('buildProxyBypassRegexFromEnv', () => {
it('constructs RegExp for domain', () => {
let regExp = util.buildProxyBypassRegexFromEnv('microsoft.com');
assert(regExp, 'regExp should not be null');
});
it('constructs RegExp for wildcard domain', () => {
let regExp = util.buildProxyBypassRegexFromEnv('*.microsoft.com');
assert(regExp, 'regExp should not be null');
});
it('bypasses same domain', () => {
let regExp = util.buildProxyBypassRegexFromEnv('microsoft.com');
assert(regExp, 'regExp should not be null');
let parsedUrl = url.parse("https://microsoft.com/api/resource");
let bypassed = regExp.test(parsedUrl.href);
assert.equal(bypassed, true);
});
it('bypasses subdomain using wildcard', () => {
let regExp = util.buildProxyBypassRegexFromEnv('*.microsoft.com');
assert(regExp, 'regExp should not be null');
let parsedUrl = url.parse("https://subdomain.microsoft.com/api/resource");
let bypassed = regExp.test(parsedUrl.href);
assert.equal(bypassed, true);
});
});
});