From 586ad49adf4a681aebcd25676edba424fee39991 Mon Sep 17 00:00:00 2001 From: Felix Luthman Date: Sun, 30 Oct 2022 16:39:56 +0100 Subject: [PATCH] strip leading dot + '*' match all + testcases --- packages/http-client/__tests__/proxy.test.ts | 24 ++++++++++++++++++++ packages/http-client/src/proxy.ts | 12 +++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/http-client/__tests__/proxy.test.ts b/packages/http-client/__tests__/proxy.test.ts index 62e8e962..48516226 100644 --- a/packages/http-client/__tests__/proxy.test.ts +++ b/packages/http-client/__tests__/proxy.test.ts @@ -145,6 +145,30 @@ describe('proxy', () => { expect(bypass).toBeFalsy() }) + it('checkBypass returns true if host with subdomain in no_proxy', () => { + process.env['no_proxy'] = 'myserver.com' + const bypass = pm.checkBypass(new URL('https://sub.myserver.com')) + expect(bypass).toBeTruthy() + }) + + it('checkBypass returns true if host with leading dot in no_proxy', () => { + process.env['no_proxy'] = '.myserver.com' + const bypass = pm.checkBypass(new URL('https://myserver.com')) + expect(bypass).toBeTruthy() + }) + + it('checkBypass returns false if no_proxy is subdomain', () => { + process.env['no_proxy'] = 'myserver.com' + const bypass = pm.checkBypass(new URL('https://myserver.com.evil.org')) + expect(bypass).toBeFalsy() + }) + + it('checkBypass returns true if no_proxy is "*"', () => { + process.env['no_proxy'] = '*' + const bypass = pm.checkBypass(new URL('https://anything.whatsoever.com')) + expect(bypass).toBeTruthy() + }) + it('HttpClient does basic http get request through proxy', async () => { process.env['http_proxy'] = _proxyUrl const httpClient = new httpm.HttpClient() diff --git a/packages/http-client/src/proxy.ts b/packages/http-client/src/proxy.ts index bbea03bb..f42e7c62 100644 --- a/packages/http-client/src/proxy.ts +++ b/packages/http-client/src/proxy.ts @@ -30,6 +30,11 @@ export function checkBypass(reqUrl: URL): boolean { return false } + // '*' match all hosts (https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/#standardizing-no_proxy) + if (noProxy === '*') { + return true + } + // Determine the request port let reqPort: number | undefined if (reqUrl.port) { @@ -50,8 +55,13 @@ export function checkBypass(reqUrl: URL): boolean { for (const upperNoProxyItem of noProxy .split(',') .map(x => x.trim().toUpperCase()) + .map(x => (x.startsWith('.') ? x.substring(1) : x)) // Strip leading dot (https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/#standardizing-no_proxy) .filter(x => x)) { - if (upperReqHosts.some(x => x.includes(upperNoProxyItem))) { + if ( + upperReqHosts.some( + x => x === upperNoProxyItem || x.endsWith(`.${upperNoProxyItem}`) + ) + ) { return true } }