Skip to content

Commit f690157

Browse files
committed
fix: preserve BalancedPool connection options
BalancedPool forwarded its options through a JSON-based deep clone, dropping function-valued connection options such as TLS certificate validation callbacks. Preserve the connect and legacy tls options outside the clone and copy object-valued options so caller mutations cannot affect new upstreams. Fixes: GHSA-w293-vg96-wgc3 CVE: CVE-2026-84961 Signed-off-by: Matteo Collina <[email protected]> (cherry picked from commit 920076347c754280ff87bea96cc6f37a235709dc)
1 parent b61d943 commit f690157

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎lib/dispatcher/balanced-pool.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ function defaultFactory (origin, opts) {
4949
}
5050

5151
class BalancedPool extends PoolBase {
52-
constructor (upstreams = [], { factory = defaultFactory, ...opts } = {}) {
52+
constructor (upstreams = [], { factory = defaultFactory, connect, tls, ...opts } = {}) {
5353
if (typeof factory !== 'function') {
5454
throw new InvalidArgumentError('factory must be a function.')
5555
}
5656

5757
super(opts)
5858

59-
this[kOptions] = { ...util.deepClone(opts) }
59+
if (connect && typeof connect !== 'function') connect = { ...connect }
60+
if (tls && typeof tls !== 'function') tls = { ...tls }
61+
this[kOptions] = { ...util.deepClone(opts), connect, tls }
6062
this[kOptions].interceptors = opts.interceptors
6163
? { ...opts.interceptors }
6264
: undefined

‎test/node-test/balanced-pool.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
const { describe, test } = require('node:test')
44
const assert = require('node:assert/strict')
55
const { BalancedPool, Pool, Client, errors } = require('../..')
6+
const { EventEmitter } = require('node:events')
7+
const fs = require('node:fs')
68
const { createServer } = require('node:http')
9+
const https = require('node:https')
10+
const path = require('node:path')
711
const { promisify } = require('node:util')
812
const { tspl } = require('@matteo.collina/tspl')
913

@@ -602,6 +606,61 @@ describe('weighted round robin', () => {
602606
}
603607
})
604608

609+
test('forwards TLS certificate validation callbacks', async (t) => {
610+
const server = https.createServer({
611+
key: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'key.pem')),
612+
cert: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'cert.pem'))
613+
}, (req, res) => {
614+
res.end('ok')
615+
})
616+
t.after(server.close.bind(server))
617+
await promisify(server.listen).call(server, 0)
618+
619+
const expectedError = new Error('rejected by custom checkServerIdentity')
620+
let identityChecks = 0
621+
const pool = new BalancedPool(`https://localhost:${server.address().port}`, {
622+
connect: {
623+
ca: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'ca.pem'), 'utf8'),
624+
servername: 'agent1',
625+
checkServerIdentity () {
626+
identityChecks++
627+
return expectedError
628+
}
629+
}
630+
})
631+
t.after(() => pool.close())
632+
633+
await assert.rejects(
634+
pool.request({ path: '/', method: 'GET' }),
635+
/rejected by custom checkServerIdentity/
636+
)
637+
assert.strictEqual(identityChecks, 1)
638+
})
639+
640+
test('copies connect and tls options before storing them', (t) => {
641+
const connect = { timeout: 100 }
642+
const tls = { servername: 'agent1' }
643+
let capturedOptions
644+
const pool = new BalancedPool([], {
645+
connect,
646+
tls,
647+
factory (origin, options) {
648+
capturedOptions = options
649+
return new Pool(origin, options)
650+
}
651+
})
652+
t.after(() => pool.close())
653+
654+
connect.timeout = 200
655+
tls.servername = 'agent2'
656+
pool.addUpstream('https://localhost:3000')
657+
658+
assert.notStrictEqual(capturedOptions.connect, connect)
659+
assert.notStrictEqual(capturedOptions.tls, tls)
660+
assert.deepStrictEqual(capturedOptions.connect, { timeout: 100 })
661+
assert.deepStrictEqual(capturedOptions.tls, { servername: 'agent1' })
662+
})
663+
605664
test('should not be vulnerable to __proto__ pollution via options', async (t) => {
606665
const { EventEmitter } = require('node:events')
607666

0 commit comments

Comments
 (0)