https: handle invalid TLS options in proxied requests

tls.connect() can throw while validating TLS options. For proxied HTTPS
requests, it is called after the CONNECT response has been received, so
the throw happens asynchronously from the original https.request() call
and ends the process as an uncaught exception.

Catch the error, close the tunnel socket, and propagate it to the
request.

Signed-off-by: Christian Aurich <christian.aurichzm@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66096
Reviewed-By: Xuguang Mei <meixuguang@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
This commit is contained in:
Christian Aurich Zanettini Martins authored and Antoine du Hamel committed 2026-09-27 17:26:11 +02:00
1 parent 70c269aadc
commit 33e4ba358b
2 files changed
+89 -5

No files matched your search

+14 -5
View File
@@ -292,11 +292,20 @@ function establishTunnel(agent, socket, options, tunnelConfig, afterSocket) {
debug('Propagate error event from tunneled socket to tunnel socket');
afterSocket(err, tunneldSocket);
};
tunneldSocket = tls.connect(requestOptions, () => {
debug('TLS handshake over tunnel succeeded');
tunneldSocket.removeListener('error', onTLSHandshakeError);
afterSocket(null, tunneldSocket);
});
// tls.connect() validates options synchronously, and here it runs after
// https.request() has returned, so a throw would be uncaught.
try {
tunneldSocket = tls.connect(requestOptions, () => {
debug('TLS handshake over tunnel succeeded');
tunneldSocket.removeListener('error', onTLSHandshakeError);
afterSocket(null, tunneldSocket);
});
} catch (err) {
debug('tls.connect() over tunnel threw', err);
socket.destroy();
afterSocket(err, socket);
return true;
}
if (requestOptions[kPerRequestCheckServerIdentity])
tunneldSocket[kPerRequestCheckServerIdentity] = true;
tunneldSocket.on('free', () => {