From 704ee0293572aaa9b9821211c4aa72321574fdd5 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Fri, 20 Nov 2026 12:44:34 +0100 Subject: [PATCH] tls: fix newSession after destroy and OCSP cert The callback of the `newSession` event only checked that the handle was still set. If a TLS error destroyed the socket while the callback was pending, calling it afterwards still completed the handshake and emitted `'secure'` and `'secureConnection'` on the destroyed socket. Return early from it if the socket is already destroyed. The callbacks of `OCSPRequest`, `SNICallback` and `resumeSession` do not need the same check: the handshake does not read while they are pending, so a TLS error cannot destroy the socket in the meantime, and if the socket is closed some other way the existing `_handle` checks already cover it. Also, when the `SNICallback` returns a context that has no certificate, for example one that only provides CAs, the handshake uses the certificate of the server. The `OCSPRequest` event was still emitted with the certificate and the issuer of that context, which are both `null`. Use the context of the server in that case, so that the event reports the certificate that is actually in use. Assisted-By: OpenAI GPT-5.6 Sol Signed-off-by: Paolo Insogna --- lib/internal/tls/wrap.js | 9 +++ ...shake-complete-after-destroy-newsession.js | 76 +++++++++++++++++++ ...ls-ocsp-request-sni-without-certificate.js | 46 +++++++++++ 3 files changed, 131 insertions(+) create mode 100644 test/parallel/test-tls-handshake-complete-after-destroy-newsession.js create mode 100644 test/parallel/test-tls-ocsp-request-sni-without-certificate.js diff --git a/lib/internal/tls/wrap.js b/lib/internal/tls/wrap.js index 9444652c66d4..cb55387f9b7d 100644 --- a/lib/internal/tls/wrap.js +++ b/lib/internal/tls/wrap.js @@ -406,6 +406,12 @@ function requestOCSP(socket, info) { let ctx = socket._handle.sni_context; + // A context selected by the SNI callback may not have a certificate, for + // example when it only provides the CAs. The handshake then uses the + // default certificate, so report that one. + if (ctx && !(ctx.context ?? ctx).getCertificate()) + ctx = undefined; + if (!ctx) { ctx = socket.server._sharedCreds; @@ -491,6 +497,9 @@ function onnewsession(sessionId, session) { return; once = true; + if (owner.destroyed) + return; + if (owner._handle === null) return owner.destroy(new ERR_SOCKET_CLOSED()); diff --git a/test/parallel/test-tls-handshake-complete-after-destroy-newsession.js b/test/parallel/test-tls-handshake-complete-after-destroy-newsession.js new file mode 100644 index 000000000000..51232c4d6783 --- /dev/null +++ b/test/parallel/test-tls-handshake-complete-after-destroy-newsession.js @@ -0,0 +1,76 @@ +'use strict'; + +// A socket that failed during the handshake does not complete it when the +// callback of the `newSession` event is called afterwards. + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const net = require('net'); +const tls = require('tls'); +const { constants } = require('crypto'); +const fixtures = require('../common/fixtures'); + +const key = fixtures.readKey('agent1-key.pem'); +const cert = fixtures.readKey('agent1-cert.pem'); + +const server = tls.createServer({ + key, + cert, + maxVersion: 'TLSv1.2', + secureOptions: constants.SSL_OP_NO_TICKET, +}); + +// An invalid TLS record is appended to the client chunk that follows the +// ClientHello, which is expected to carry ClientKeyExchange, ChangeCipherSpec +// and Finished in a single `data` event. The server then fails with a TLS +// error after it has requested the callback of the `newSession` event. +const invalidRecord = Buffer.from([0x17, 0x03, 0x03, 0x00, 0x05, 1, 2, 3, 4, 5]); +const proxy = net.createServer(common.mustCall((downstream) => { + const upstream = net.connect(server.address().port); + let first = true; + let injected = false; + downstream.on('data', (chunk) => { + // Handshake records have the content type 0x16. + if (!first && !injected && chunk[0] === 0x16) { + injected = true; + chunk = Buffer.concat([chunk, invalidRecord]); + } + first = false; + upstream.write(chunk); + }); + upstream.pipe(downstream); + downstream.on('error', () => {}); + upstream.on('error', () => {}); + downstream.on('close', () => upstream.destroy()); + upstream.on('close', () => downstream.destroy()); +})); + +let done; +server.on('secureConnection', common.mustNotCall()); +server.on('newSession', common.mustCall((id, data, callback) => { + done = callback; +})); +server.on('tlsClientError', common.mustCall((err, socket) => { + assert.ok(socket.destroyed); + socket.on('secure', common.mustNotCall()); + done(); + setImmediate(common.mustCall(() => { + proxy.close(); + server.close(); + })); +})); + +server.listen(0, common.mustCall(() => { + proxy.listen(0, common.mustCall(() => { + const client = tls.connect({ + port: proxy.address().port, + maxVersion: 'TLSv1.2', + secureOptions: constants.SSL_OP_NO_TICKET, + rejectUnauthorized: false, + }); + client.on('error', () => {}); + })); +})); diff --git a/test/parallel/test-tls-ocsp-request-sni-without-certificate.js b/test/parallel/test-tls-ocsp-request-sni-without-certificate.js new file mode 100644 index 000000000000..e1e1f50a87e6 --- /dev/null +++ b/test/parallel/test-tls-ocsp-request-sni-without-certificate.js @@ -0,0 +1,46 @@ +'use strict'; + +// The OCSPRequest event reports the certificate that the connection uses, +// also when the SNI context only carries a CA. + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const crypto = require('crypto'); +const tls = require('tls'); +const fixtures = require('../common/fixtures'); + +const key = fixtures.readKey('agent1-key.pem'); +const cert = fixtures.readKey('agent1-cert.pem'); +const ca = fixtures.readKey('ca1-cert.pem'); + +const server = tls.createServer({ + key, + cert, + SNICallback: common.mustCall((servername, callback) => { + callback(null, tls.createSecureContext({ ca })); + }), +}); + +server.on('OCSPRequest', common.mustCall((certificate, issuer, callback) => { + assert.deepStrictEqual( + certificate, + new crypto.X509Certificate(cert).raw); + callback(null, null); +})); + +server.listen(0, common.mustCall(() => { + const client = tls.connect({ + port: server.address().port, + servername: 'sni.example.com', + requestOCSP: true, + rejectUnauthorized: false, + }); + + client.on('OCSPResponse', common.mustCall(() => { + client.destroy(); + server.close(); + })); +}));