node-fetch-native icon indicating copy to clipboard operation
node-fetch-native copied to clipboard

node-fetch-native/proxy - noProxy does not properly parse hosts with port

Open CS-JJ opened this issue 1 year ago • 1 comments

Environment

Node 21.6.1 node-fetch-native 1.6.4

Reproduction

Make any request specifying port in the url to a host that is on the noProxy list.

http_proxy = 10.0.0.1:3128
no_proxy = 10.10.10.10
await fetch('http://10.10.10.10:8001/healthz')

You should observe the host applying the proxy settings to the request instead of observing the specified no_proxy configuration

Describe the bug

The bypassProxy implementation does not check/parse the hostname from any port specification in the request. This causes any requests to hosts in the no_proxy configuration using a specified port to have proxy configurations applied.

Suggested fix

host = host.split(':').at(0);

Additional context

There is an existing workaround to this issue by specifying the host with the port in the no_proxy variable This would correctly bypass the proxy settings in the current implementation.

no_proxy=10.10.10.10:8001
fetch('10.10.10.10:8001/healthz') 

Logs

No response

CS-JJ avatar Apr 16 '24 12:04 CS-JJ

ended cloning and running a local version for my project.

Realized that the implementation also doesn't support ip cidr ranges.

I'm running Node 21 and this implementation using the BlockList worked for me.

function bypassProxy(host, noProxy) {
  host = host.split(":").at(0);
  const blockList = new BlockList;
  for (const _host of noProxy) {
    if (/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.test(_host)) {
      if (_host.search('/') >= 0) {
        blockList.addSubnet(_host.split("/").at(0), parseInt(_host.split("/").at(-1)))
      } else {
        blockList.addAddress(_host)
      }
    }
  }
  if (!noProxy) {
    return false;
  }
  for (const _host of noProxy) {
    if (_host === host || (_host[0] === "." && host.endsWith(_host.slice(1))) || blockList.check(host)) {
      return true;
    }
  }
  return false;
}

CS-JJ avatar Apr 29 '24 20:04 CS-JJ