Bypass proxy on loopback IPs (localhost, 127.*, ::1 etc) (#1361)

* Bypass proxy on loopback IPs

* Expect empty array instead of undefined

* Restore accidentally deleted test

* Fix formatting

* Fix linting

* Update proxy.ts

* Better ipv6 definitions

* Fix linting

* Update proxy.test.ts
This commit is contained in:
Ferenc Hammerl
2023-03-06 11:07:04 +01:00
committed by GitHub
parent d47e0bac60
commit 94ab8de5f3
2 changed files with 40 additions and 0 deletions

View File

@@ -25,6 +25,11 @@ export function checkBypass(reqUrl: URL): boolean {
return false
}
const reqHost = reqUrl.hostname
if (isLoopbackAddress(reqHost)) {
return true
}
const noProxy = process.env['no_proxy'] || process.env['NO_PROXY'] || ''
if (!noProxy) {
return false
@@ -67,3 +72,13 @@ export function checkBypass(reqUrl: URL): boolean {
return false
}
function isLoopbackAddress(host: string): boolean {
const hostLower = host.toLowerCase()
return (
hostLower === 'localhost' ||
hostLower.startsWith('127.') ||
hostLower.startsWith('[::1]') ||
hostLower.startsWith('[0:0:0:0:0:0:0:1]')
)
}