Skip to content

Commit

Permalink
fix: support url-encoded proxy authorization
Browse files Browse the repository at this point in the history
FIX: added tests to ensure username and password are url-decoded

PR-URL: #47
Credit: @svennergr
Close: #47
Reviewed-by: @isaacs
  • Loading branch information
svennergr authored and isaacs committed Jun 1, 2021
1 parent 4be683d commit cabacdc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ function getProxyUri (uri, opts) {
}

const getAuth = u =>
u.username && u.password ? `${u.username}:${u.password}`
: u.username ? u.username
u.username && u.password ? decodeURIComponent(`${u.username}:${u.password}`)
: u.username ? decodeURIComponent(u.username)
: null

const getPath = u => u.pathname + u.search + u.hash
Expand Down
64 changes: 64 additions & 0 deletions test/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,70 @@ test('get proxy agent', async t => {
__type: 'socks-proxy',
}, 'socks proxy url, for http request')

t.strictSame(getProxy(new url.URL('http://user:pass@proxy.local:443/'), OPTS, false), {
host: 'proxy.local',
port: '443',
protocol: 'http:',
path: '/',
auth: 'user:pass',
ca: 'ca',
cert: 'cert',
key: undefined,
timeout: 2,
localAddress: 'local address',
maxSockets: 3,
rejectUnauthorized: true,
__type: 'http-proxy',
}, 'http proxy url, for http request')

t.strictSame(getProxy(new url.URL('http://user@proxy.local:443/'), OPTS, false), {
host: 'proxy.local',
port: '443',
protocol: 'http:',
path: '/',
auth: 'user',
ca: 'ca',
cert: 'cert',
key: undefined,
timeout: 2,
localAddress: 'local address',
maxSockets: 3,
rejectUnauthorized: true,
__type: 'http-proxy',
}, 'http proxy url, for http request')

t.strictSame(getProxy(new url.URL('http://user%231:pass@proxy.local:443/'), OPTS, false), {
host: 'proxy.local',
port: '443',
protocol: 'http:',
path: '/',
auth: 'user#1:pass',
ca: 'ca',
cert: 'cert',
key: undefined,
timeout: 2,
localAddress: 'local address',
maxSockets: 3,
rejectUnauthorized: true,
__type: 'http-proxy',
}, 'http proxy url, for http request')

t.strictSame(getProxy(new url.URL('http://user%231:pass%231@proxy.local:443/'), OPTS, false), {
host: 'proxy.local',
port: '443',
protocol: 'http:',
path: '/',
auth: 'user#1:pass#1',
ca: 'ca',
cert: 'cert',
key: undefined,
timeout: 2,
localAddress: 'local address',
maxSockets: 3,
rejectUnauthorized: true,
__type: 'http-proxy',
}, 'http proxy url, for http request')

t.throws(() => getProxy(new url.URL('gopher://proxy.local'), OPTS, false), {
message: 'unsupported proxy protocol: \'gopher:\'',
url: 'gopher://proxy.local',
Expand Down

0 comments on commit cabacdc

Please sign in to comment.