fix: proxy URLs with special chars in credentials (#3925)

close #3370
This commit is contained in:
Zoltan Kochan
2021-10-26 19:09:46 +03:00
committed by GitHub
parent cae34936e2
commit 3c7e5eced1
3 changed files with 40 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/npm-registry-agent": patch
---
Proxy URLs with special characters in credentials should work.

View File

@@ -33,7 +33,7 @@ export default function getAgent (uri: string, opts: AgentOptions) {
const key = [
`https:${isHttps.toString()}`,
pxuri
? `proxy:${pxuri.protocol}//${pxuri.host}:${pxuri.port}`
? `proxy:${pxuri.protocol}//${pxuri.username}:${pxuri.password}@${pxuri.host}:${pxuri.port}`
: '>no-proxy<',
`local-address:${opts.localAddress ?? '>no-local-address<'}`,
`strict-ssl:${isHttps ? Boolean(opts.strictSsl).toString() : '>no-strict-ssl<'}`,
@@ -154,7 +154,7 @@ function getProxy (
isHttps: boolean
) {
const popts = {
auth: (proxyUrl.username ? (proxyUrl.password ? `${proxyUrl.username}:${proxyUrl.password}` : proxyUrl.username) : undefined),
auth: getAuth(proxyUrl),
ca: opts.ca,
cert: opts.cert,
host: proxyUrl.hostname,
@@ -179,3 +179,14 @@ function getProxy (
return new SocksProxyAgent(popts)
}
}
function getAuth (user: { username?: string, password?: string }) {
if (!user.username) {
return undefined
}
let auth = user.username
if (user.password) {
auth += `:${user.password}`
}
return decodeURIComponent(auth)
}

View File

@@ -86,3 +86,25 @@ test('a socks proxy', () => {
type: 5,
})
})
test('proxy credentials are decoded', () => {
const opts = {
httpsProxy: 'https://use%21r:pas%2As@my.proxy:1234/foo',
...OPTS,
}
expect(getAgent('https://foo.com/bar', opts)).toEqual({
__type: 'https-proxy',
auth: 'use!r:pas*s',
ca: 'ca',
cert: 'cert',
host: 'my.proxy',
key: 'key',
localAddress: 'localAddress',
maxSockets: 5,
path: '/foo',
port: '1234',
protocol: 'https:',
rejectUnauthorized: true,
timeout: 6,
})
})