Skip to content

Commit 8c59f6b

Browse files
committed
fix: preserve Agent connection options
Signed-off-by: Matteo Collina <[email protected]> (cherry picked from commit f75eac0) (cherry picked from commit 04cb74b)
1 parent b40fbfc commit 8c59f6b

2 files changed

Lines changed: 105 additions & 2 deletions

File tree

‎lib/dispatcher/agent.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function defaultFactory (origin, opts) {
2323
}
2424

2525
class Agent extends DispatcherBase {
26-
constructor ({ factory = defaultFactory, maxRedirections = 0, connect, ...options } = {}) {
26+
constructor ({ factory = defaultFactory, maxRedirections = 0, connect, tls, ...options } = {}) {
2727
if (typeof factory !== 'function') {
2828
throw new InvalidArgumentError('factory must be a function.')
2929
}
@@ -42,11 +42,15 @@ class Agent extends DispatcherBase {
4242
connect = { ...connect }
4343
}
4444

45+
if (tls && typeof tls !== 'function') {
46+
tls = { ...tls }
47+
}
48+
4549
this[kInterceptors] = options.interceptors?.Agent && Array.isArray(options.interceptors.Agent)
4650
? options.interceptors.Agent
4751
: [createRedirectInterceptor({ maxRedirections })]
4852

49-
this[kOptions] = { ...util.deepClone(options), connect }
53+
this[kOptions] = { ...util.deepClone(options), connect, tls }
5054
this[kOptions].interceptors = options.interceptors
5155
? { ...options.interceptors }
5256
: undefined

‎test/node-test/agent.js‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
const { describe, test, after } = require('node:test')
44
const assert = require('node:assert/strict')
5+
const { once } = require('node:events')
6+
const fs = require('node:fs')
57
const http = require('node:http')
8+
const https = require('node:https')
9+
const path = require('node:path')
610
const { PassThrough } = require('node:stream')
711
const { kRunning } = require('../../lib/core/symbols')
812
const {
@@ -811,3 +815,98 @@ test('the dispatcher is truly global', t => {
811815
assert.ok(require.resolve('../../index.js') in require.cache)
812816
assert.strictEqual(agent, undiciFresh.getGlobalDispatcher())
813817
})
818+
819+
test('forwards TLS certificate validation callbacks via tls option', async (t) => {
820+
const server = https.createServer({
821+
key: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'key.pem')),
822+
cert: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'cert.pem'))
823+
}, (req, res) => {
824+
res.end('ok')
825+
})
826+
t.after(() => server.close())
827+
server.listen(0, '127.0.0.1')
828+
await once(server, 'listening')
829+
830+
const expectedError = new Error('rejected by custom checkServerIdentity')
831+
let identityChecks = 0
832+
const agent = new Agent({
833+
tls: {
834+
ca: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'ca.pem'), 'utf8'),
835+
servername: 'agent1',
836+
checkServerIdentity () {
837+
identityChecks++
838+
return expectedError
839+
}
840+
}
841+
})
842+
t.after(() => agent.close())
843+
844+
await assert.rejects(
845+
request(`https://localhost:${server.address().port}`, { dispatcher: agent }),
846+
/rejected by custom checkServerIdentity/
847+
)
848+
assert.strictEqual(identityChecks, 1)
849+
})
850+
851+
test('forwards TLS certificate validation callbacks via connect option', async (t) => {
852+
const server = https.createServer({
853+
key: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'key.pem')),
854+
cert: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'cert.pem'))
855+
}, (req, res) => {
856+
res.end('ok')
857+
})
858+
t.after(() => server.close())
859+
server.listen(0, '127.0.0.1')
860+
await once(server, 'listening')
861+
862+
const expectedError = new Error('rejected by custom checkServerIdentity')
863+
let identityChecks = 0
864+
const agent = new Agent({
865+
connect: {
866+
ca: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'ca.pem'), 'utf8'),
867+
servername: 'agent1',
868+
checkServerIdentity () {
869+
identityChecks++
870+
return expectedError
871+
}
872+
}
873+
})
874+
t.after(() => agent.close())
875+
876+
await assert.rejects(
877+
request(`https://localhost:${server.address().port}`, { dispatcher: agent }),
878+
/rejected by custom checkServerIdentity/
879+
)
880+
assert.strictEqual(identityChecks, 1)
881+
})
882+
883+
test('copies connect and tls options before storing them', async (t) => {
884+
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
885+
res.end('ok')
886+
})
887+
t.after(closeServerAsPromise(server))
888+
server.listen(0, '127.0.0.1')
889+
await once(server, 'listening')
890+
891+
const connect = { timeout: 100 }
892+
const tls = { servername: 'agent1' }
893+
let capturedOptions
894+
const agent = new Agent({
895+
connect,
896+
tls,
897+
factory (origin, options) {
898+
capturedOptions = options
899+
return new Pool(origin, options)
900+
}
901+
})
902+
t.after(() => agent.close())
903+
904+
connect.timeout = 200
905+
tls.servername = 'agent2'
906+
await request(`http://localhost:${server.address().port}`, { dispatcher: agent })
907+
908+
assert.notStrictEqual(capturedOptions.connect, connect)
909+
assert.notStrictEqual(capturedOptions.tls, tls)
910+
assert.deepStrictEqual(capturedOptions.connect, { timeout: 100 })
911+
assert.deepStrictEqual(capturedOptions.tls, { servername: 'agent1' })
912+
})

0 commit comments

Comments
 (0)