Skip to content

Commit 3f61461

Browse files
inoway46nodejs-github-bot
authored andcommitted
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: #67215 Fixes: #67213 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent b7df44b commit 3f61461

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎lib/net.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,13 @@ function closeSocketHandle(self, isException, isCleanupPending = false) {
368368
if (self._handle) {
369369
self._handle.close(() => {
370370
debug('emit close');
371-
self.emit('close', isException);
372371
if (isCleanupPending) {
372+
// The handle is closed and must not be used by close listeners.
373373
self._handle.onread = noop;
374374
self._handle = null;
375375
self._sockname = null;
376376
}
377+
self.emit('close', isException);
377378
});
378379
}
379380
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const net = require('net');
9+
const tls = require('tls');
10+
const fixtures = require('../common/fixtures');
11+
12+
const server = tls.createServer({
13+
key: fixtures.readKey('agent1-key.pem'),
14+
cert: fixtures.readKey('agent1-cert.pem'),
15+
}, common.mustNotCall());
16+
17+
server.on('tlsClientError', common.mustCall((err, socket) => {
18+
assert.match(err.code, /^ERR_SSL_/);
19+
assert(socket.destroyed);
20+
// Connection details must remain available while handling the error.
21+
assert.strictEqual(socket.remoteAddress, '127.0.0.1');
22+
assert.strictEqual(socket.remoteFamily, 'IPv4');
23+
assert(socket.remotePort > 0);
24+
socket.on('close', common.mustCall(() => {
25+
// The SSL state has been freed before close listeners run.
26+
assert.strictEqual(socket.getProtocol(), null);
27+
server.close(common.mustCall());
28+
}));
29+
}));
30+
31+
server.listen(0, '127.0.0.1', common.mustCall(() => {
32+
const client = net.connect(server.address().port, '127.0.0.1');
33+
client.on('connect', common.mustCall(() => {
34+
// Reject the handshake without a TLS client.
35+
client.write(Buffer.alloc(64, 42));
36+
}));
37+
client.on('error', common.mustCallAtLeast((err) => {
38+
assert.strictEqual(err.code, 'ECONNRESET');
39+
}, 0));
40+
client.on('close', common.mustCall());
41+
client.resume();
42+
}));

0 commit comments

Comments
 (0)