net: clear closed handles before emitting close

Clear a deferred socket handle before invoking close listeners. TLS has
already freed its SSL state at this point, so retaining the handle lets
getProtocol() access a null SSL pointer and crash the process.

Keep connection details available during tlsClientError and add coverage
for querying the protocol after a failed handshake closes the socket.

Signed-off-by: inoway46 <inoueyuya416@gmail.com>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/67215
Fixes: https://github.com/nodejs/node/issues/67213
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
This commit is contained in:
inoway46 authored and Antoine du Hamel committed 2026-11-19 15:44:17 +01:00
1 parent 66b97e61bc
commit bed7be9dbe
2 files changed
+44 -1

No files matched your search

+2 -1
View File
@@ -368,12 +368,13 @@ function closeSocketHandle(self, isException, isCleanupPending = false) {
if (self._handle) {
self._handle.close(() => {
debug('emit close');
self.emit('close', isException);
if (isCleanupPending) {
// The handle is closed and must not be used by close listeners.
self._handle.onread = noop;
self._handle = null;
self._sockname = null;
}
self.emit('close', isException);
});
}
}
@@ -0,0 +1,42 @@
'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'),
}, common.mustNotCall());
server.on('tlsClientError', common.mustCall((err, socket) => {
assert.match(err.code, /^ERR_SSL_/);
assert(socket.destroyed);
// Connection details must remain available while handling the error.
assert.strictEqual(socket.remoteAddress, '127.0.0.1');
assert.strictEqual(socket.remoteFamily, 'IPv4');
assert(socket.remotePort > 0);
socket.on('close', common.mustCall(() => {
// The SSL state has been freed before close listeners run.
assert.strictEqual(socket.getProtocol(), null);
server.close(common.mustCall());
}));
}));
server.listen(0, '127.0.0.1', common.mustCall(() => {
const client = net.connect(server.address().port, '127.0.0.1');
client.on('connect', common.mustCall(() => {
// Reject the handshake without a TLS client.
client.write(Buffer.alloc(64, 42));
}));
client.on('error', common.mustCallAtLeast((err) => {
assert.strictEqual(err.code, 'ECONNRESET');
}, 0));
client.on('close', common.mustCall());
client.resume();
}));