fix: honor requestTls when proxy is SOCKS5 · nodejs/undici@04201f8 · GitHub
Skip to content

Commit 04201f8

Browse files
tonghuarootmcollina
authored andcommitted
fix: honor requestTls when proxy is SOCKS5
ProxyAgent silently dropped opts.requestTls when the configured proxy URI was socks5:// (or socks://). Socks5ProxyAgent also lacked any equivalent of requestTls, so even constructed directly it had no way to configure target HTTPS TLS settings (ca, cert, key, rejectUnauthorized, servername). The inner connect callback called tls.connect with ...connectOpts.tls || {} but undici's internal connectOpts never populates a .tls field, so the spread was a dead expression. This change: - Forwards opts.requestTls from ProxyAgent to Socks5ProxyAgent. - Adds a requestTls constructor option on Socks5ProxyAgent stored in the kRequestTls symbol. - Applies it on the inner tls.connect for the target HTTPS upgrade, with socket overriding (must point at the SOCKS5 tunnel) and servername defaulting to targetHost but allowing user override to match the HTTP-proxy path semantics. - Documents the new option in Socks5ProxyAgent.md. - Adds two regression tests covering both the direct Socks5ProxyAgent path and the ProxyAgent forwarding path. Refs GHSA-vmh5-mc38-953g (cherry picked from commit 42d4955) Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent fcd642f commit 04201f8

4 files changed

Lines changed: 132 additions & 5 deletions

File tree

docs/docs/api/Socks5ProxyAgent.md

Lines changed: 1 addition & 0 deletions

lib/dispatcher/proxy-agent.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ class ProxyAgent extends DispatcherBase {
142142
factory: agentFactory,
143143
username: opts.username || username,
144144
password: opts.password || password,
145-
proxyTls: opts.proxyTls
145+
proxyTls: opts.proxyTls,
146+
requestTls: opts.requestTls
146147
})
147148
}
148149

lib/dispatcher/socks5-proxy-agent.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const kProxyAuth = Symbol('proxy auth')
1919
const kProxyProtocol = Symbol('proxy protocol')
2020
const kPools = Symbol('pools')
2121
const kConnector = Symbol('connector')
22+
const kRequestTls = Symbol('request tls settings')
2223

2324
// Static flag to ensure warning is only emitted once per process
2425
let experimentalWarningEmitted = false
@@ -53,6 +54,7 @@ class Socks5ProxyAgent extends DispatcherBase {
5354
this[kProxyUrl] = url
5455
this[kProxyHeaders] = options.headers || {}
5556
this[kProxyProtocol] = options.proxyTls ? 'https:' : 'http:'
57+
this[kRequestTls] = options.requestTls
5658

5759
// Extract auth from URL or options
5860
this[kProxyAuth] = {
@@ -199,9 +201,9 @@ class Socks5ProxyAgent extends DispatcherBase {
199201
}
200202
debug('upgrading to TLS')
201203
finalSocket = tls.connect({
204+
...this[kRequestTls],
202205
socket,
203-
servername: targetHost,
204-
...connectOpts.tls || {}
206+
servername: this[kRequestTls]?.servername || targetHost
205207
})
206208

207209
await new Promise((resolve, reject) => {

test/socks5-proxy-agent.js

Lines changed: 125 additions & 2 deletions

0 commit comments

Comments
 (0)