Commit 371ac095ac2 for nodejs
commit 371ac095ac2025a917651e4cdf2f5b69113cbc2b
Author: inoway46 <inoueyuya416@gmail.com>
Date: Wed Nov 4 22:26:52 2026 +0900
tls: destroy sockets after fatal errors
Destroy established TLS sockets on fatal errors so pending writes
cannot prevent HTTP/2 session cleanup from completing. Defer closing
until after error handling to preserve connection details for listeners.
Add regression coverage for standalone TLS socket closure and HTTP/2
cleanup with default and custom TLS connections.
Fixes: https://github.com/nodejs/node/issues/67213
Signed-off-by: inoway46 <inoueyuya416@gmail.com>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/67215
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
diff --git a/lib/internal/tls/wrap.js b/lib/internal/tls/wrap.js
index cb6c48c108b..9444652c66d 100644
--- a/lib/internal/tls/wrap.js
+++ b/lib/internal/tls/wrap.js
@@ -625,8 +625,10 @@ function onerror(err) {
// Ignore server's authorization errors
owner.destroy();
} else {
- // Emit error
- owner._emitTLSError(err);
+ // A failed TLS connection cannot make progress on pending writes.
+ // Preserve connection details until the error has been handled.
+ owner._closeAfterHandlingError = true;
+ owner.destroy(err);
}
}
diff --git a/test/parallel/test-http2-tls-fatal-alert.js b/test/parallel/test-http2-tls-fatal-alert.js
new file mode 100644
index 00000000000..d60b05a9746
--- /dev/null
+++ b/test/parallel/test-http2-tls-fatal-alert.js
@@ -0,0 +1,63 @@
+'use strict';
+const common = require('../common');
+if (!common.hasCrypto)
+ common.skip('missing crypto');
+const assert = require('assert');
+const http2 = require('http2');
+const tls = require('tls');
+const fixtures = require('../common/fixtures');
+
+for (const customConnection of [false, true]) {
+ const server = http2.createSecureServer({
+ key: fixtures.readKey('agent1-key.pem'),
+ cert: fixtures.readKey('agent1-cert.pem'),
+ requestCert: true,
+ rejectUnauthorized: true,
+ minVersion: 'TLSv1.3',
+ maxVersion: 'TLSv1.3',
+ });
+ server.on('tlsClientError', common.mustCall((err) => {
+ assert.strictEqual(err.code, 'ERR_SSL_PEER_DID_NOT_RETURN_A_CERTIFICATE');
+ }));
+ server.listen(0, '127.0.0.1', common.mustCall(() => {
+ const options = {
+ host: '127.0.0.1',
+ port: server.address().port,
+ servername: 'localhost',
+ rejectUnauthorized: false,
+ ALPNProtocols: ['h2'],
+ minVersion: 'TLSv1.3',
+ maxVersion: 'TLSv1.3',
+ };
+ const socket = customConnection ? tls.connect(options) : undefined;
+ const session = http2.connect(`https://127.0.0.1:${server.address().port}`, {
+ ...options,
+ ...(customConnection ? { createConnection: () => socket } : {}),
+ });
+ const transport = socket || session.socket;
+ // Destroying the socket must not emit the TLS error a second time.
+ transport.on('error', common.mustCall());
+ let errorEmitted = false;
+ session.on('connect', common.mustCall(() => {
+ assert.strictEqual(transport.getProtocol(), 'TLSv1.3');
+ }));
+ session.on('error', common.mustCall((err) => {
+ // OpenSSL and BoringSSL use different names for the same TLS alert.
+ assert.ok([
+ 'ERR_SSL_TLSV13_ALERT_CERTIFICATE_REQUIRED',
+ 'ERR_SSL_TLSV1_ALERT_CERTIFICATE_REQUIRED',
+ ].includes(err.code), `Unexpected TLS error code: ${err.code}`);
+ errorEmitted = true;
+ }));
+ transport.on('close', common.mustCall(function() {
+ // The session's socket proxy may already be detached.
+ assert(this.destroyed);
+ }));
+ session.on('close', common.mustCall(() => {
+ assert(errorEmitted);
+ assert(session.destroyed);
+ if (socket) assert(socket.destroyed);
+ server.close(common.mustCall());
+ }));
+ }));
+}
diff --git a/test/parallel/test-tls-fatal-error-close.js b/test/parallel/test-tls-fatal-error-close.js
new file mode 100644
index 00000000000..6530c4ac984
--- /dev/null
+++ b/test/parallel/test-tls-fatal-error-close.js
@@ -0,0 +1,59 @@
+'use strict';
+
+const common = require('../common');
+if (!common.hasCrypto)
+ common.skip('missing crypto');
+
+const assert = require('assert');
+const net = require('net');
+const tls = require('tls');
+const fixtures = require('../common/fixtures');
+
+const server = tls.createServer({
+ key: fixtures.readKey('agent1-key.pem'),
+ cert: fixtures.readKey('agent1-cert.pem'),
+ minVersion: 'TLSv1.3',
+ maxVersion: 'TLSv1.3',
+}, common.mustCall((socket) => {
+ const remotePort = socket.remotePort;
+ let errorEmitted = false;
+ socket.on('error', common.mustCall((err) => {
+ assert.match(err.code, /^ERR_SSL_/);
+ assert(socket.destroyed);
+ assert.strictEqual(socket.errored, err);
+ // Destroy the socket while preserving connection details for this handler.
+ assert.strictEqual(socket.remoteAddress, '127.0.0.1');
+ assert.strictEqual(socket.remoteFamily, 'IPv4');
+ assert.strictEqual(socket.remotePort, remotePort);
+ assert.strictEqual(socket.getProtocol(), 'TLSv1.3');
+ errorEmitted = true;
+ }));
+ socket.on('close', common.mustCall(() => {
+ assert(errorEmitted);
+ assert(socket.destroyed);
+ assert.strictEqual(socket.getProtocol(), null);
+ client.destroy();
+ server.close(common.mustCall());
+ }));
+}));
+server.on('tlsClientError', common.mustNotCall());
+
+let client;
+server.listen(0, '127.0.0.1', common.mustCall(() => {
+ const transport = net.connect(server.address().port, '127.0.0.1');
+ client = tls.connect({
+ socket: transport,
+ rejectUnauthorized: false,
+ minVersion: 'TLSv1.3',
+ maxVersion: 'TLSv1.3',
+ });
+ client.on('error', () => {});
+ client.on('close', common.mustCall());
+ server.once('secureConnection', common.mustCall(() => {
+ // Corrupt an application-data record without closing the TCP connection.
+ // The server must close on the TLS error without application cleanup.
+ const record = Buffer.alloc(37);
+ record.set([23, 3, 3, 0, 32]);
+ transport.write(record);
+ }));
+}));