Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/dispatcher/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const buildConnector = require('../core/connect')
const Client = require('./client')
const { channels } = require('../core/diagnostics')
const Socks5ProxyAgent = require('./socks5-proxy-agent')
const { hasSafeIterator } = require('../core/util')

const kAgent = Symbol('proxy agent')
const kClient = Symbol('proxy client')
Expand Down Expand Up @@ -345,6 +346,21 @@ function buildHeaders (headers) {
return headersPair
}

// Materialize iterable header containers (e.g. Map, Headers) into a record so
// that throwIfProxyAuthIsSent() can inspect their entries. Object.keys and
// for...in see nothing on a Map/Headers instance, so without this the
// Proxy-Authorization guard is bypassed and proxy credentials can reach the
// origin server (GHSA-6cv7-626c-qhqw).
if (headers && typeof headers === 'object' && hasSafeIterator(headers)) {
const headersPair = {}

for (const [key, value] of headers) {
headersPair[key] = value
}

return headersPair
}

return headers
}

Expand Down
27 changes: 25 additions & 2 deletions test/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
const diagnosticsChannel = require('node:diagnostics_channel')
const { request, fetch, setGlobalDispatcher, getGlobalDispatcher } = require('..')
const { request, fetch, Headers, setGlobalDispatcher, getGlobalDispatcher } = require('..')
const { InvalidArgumentError, ConnectTimeoutError, SecureProxyConnectionError } = require('../lib/core/errors')
const ProxyAgent = require('../lib/dispatcher/proxy-agent')
const Pool = require('../lib/dispatcher/pool')
Expand Down Expand Up @@ -837,7 +837,7 @@ test('use proxy-agent with custom headers with tunneling enabled', async (t) =>
})

test('sending proxy-authorization in request headers should throw', async (t) => {
t = tspl(t, { plan: 3 })
t = tspl(t, { plan: 5 })
const server = await buildServer()
const proxy = await buildProxy()

Expand Down Expand Up @@ -888,6 +888,29 @@ test('sending proxy-authorization in request headers should throw', async (t) =>
'Proxy-Authorization should be sent in ProxyAgent'
)

// Iterable containers (Map/Headers) must not bypass the guard (GHSA-6cv7-626c-qhqw)
await t.rejects(
request(
serverUrl + '/hello?foo=bar',
{
dispatcher: proxyAgent,
headers: new Map([['proxy-authorization', Buffer.from('user:pass').toString('base64')]])
}
),
'Proxy-Authorization should be sent in ProxyAgent'
)

await t.rejects(
request(
serverUrl + '/hello?foo=bar',
{
dispatcher: proxyAgent,
headers: new Headers({ 'proxy-authorization': Buffer.from('user:pass').toString('base64') })
}
),
'Proxy-Authorization should be sent in ProxyAgent'
)

server.close()
proxy.close()
proxyAgent.close()
Expand Down
Loading