TooTallNate/proxy-agents

PAC proxy with `*_PROXY` env vars is broken

Drarig29 opened this issue · 2 comments

With proxy-agent@5.0.0, the following code worked (example taken from here):

// index.mjs

import * as http from 'http'
import ProxyAgent from 'proxy-agent'

// The correct proxy `Agent` implementation to use will be determined
// via the `http_proxy` / `https_proxy` / `no_proxy` / etc. env vars
const agent = new ProxyAgent()

// The rest works just like any other normal HTTP request
http.get('http://jsonip.com', {agent}, (res) => {
  console.log(res.statusCode, res.headers)
  res.pipe(process.stdout)
})

Test command line (this uses a PAC file which only returns "DIRECT"):

DEBUG=proxy-agent,pac-proxy-agent HTTP_PROXY=pac+https://gist.githubusercontent.com/Drarig29/d6dcf7f2995921e063006afb01b776db/raw/84fed7334300698e6e7390a4928de592d5666e99/noop-proxy.pac node index.mjs

After upgrading to proxy-agent@6.2.1 (and doing this little change:)

- import ProxyAgent from 'proxy-agent'
+ import {ProxyAgent} from 'proxy-agent'

Then it fails with this:

  proxy-agent Creating new ProxyAgent instance: undefined +0ms
  proxy-agent Request URL: 'http://jsonip.com/' +3ms
  proxy-agent Proxy URL: 'pac+https://gist.githubusercontent.com/Drarig29/d6dcf7f2995921e063006afb01b776db/raw/84fed7334300698e6e7390a4928de592d5666e99/noop-proxy.pac' +0ms

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: Unsupported protocol for proxy URL: pac+https://gist.githubusercontent.com/Drarig29/d6dcf7f2995921e063006afb01b776db/raw/84fed7334300698e6e7390a4928de592d5666e99/noop-proxy.pac
    at ProxyAgent.connect (/Users/corentin.girard/playground/mcve-bug-pac-proxy-agent/node_modules/proxy-agent/dist/index.js:113:23)
    at /Users/corentin.girard/playground/mcve-bug-pac-proxy-agent/node_modules/agent-base/dist/index.js:73:30
Emitted 'error' event on ClientRequest instance at:
    at _destroy (node:_http_client:874:13)
    at onSocketNT (node:_http_client:891:5)
    at processTicksAndRejections (node:internal/process/task_queues:84:21)

I think that's because isValidProtocol()

if (!isValidProtocol(proxyProto)) {
throw new Error(`Unsupported protocol for proxy URL: ${proxy}`);
}

uses those protocols:

static readonly protocols: `pac-${Protocols}`[] = [
'pac-data',
'pac-file',
'pac-ftp',
'pac-http',
'pac-https',
];

Whoops, ya, good catch.