Skip to content

Commit 371ac09

Browse files
inoway46nodejs-github-bot
authored andcommitted
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: #67213 Signed-off-by: inoway46 <inoueyuya416@gmail.com> Assisted-by: Codex PR-URL: #67215 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent 3f61461 commit 371ac09

3 files changed

Lines changed: 126 additions & 2 deletions

File tree

‎lib/internal/tls/wrap.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,10 @@ function onerror(err) {
625625
// Ignore server's authorization errors
626626
owner.destroy();
627627
} else {
628-
// Emit error
629-
owner._emitTLSError(err);
628+
// A failed TLS connection cannot make progress on pending writes.
629+
// Preserve connection details until the error has been handled.
630+
owner._closeAfterHandlingError = true;
631+
owner.destroy(err);
630632
}
631633
}
632634

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
const assert = require('assert');
6+
const http2 = require('http2');
7+
const tls = require('tls');
8+
const fixtures = require('../common/fixtures');
9+
10+
for (const customConnection of [false, true]) {
11+
const server = http2.createSecureServer({
12+
key: fixtures.readKey('agent1-key.pem'),
13+
cert: fixtures.readKey('agent1-cert.pem'),
14+
requestCert: true,
15+
rejectUnauthorized: true,
16+
minVersion: 'TLSv1.3',
17+
maxVersion: 'TLSv1.3',
18+
});
19+
server.on('tlsClientError', common.mustCall((err) => {
20+
assert.strictEqual(err.code, 'ERR_SSL_PEER_DID_NOT_RETURN_A_CERTIFICATE');
21+
}));
22+
server.listen(0, '127.0.0.1', common.mustCall(() => {
23+
const options = {
24+
host: '127.0.0.1',
25+
port: server.address().port,
26+
servername: 'localhost',
27+
rejectUnauthorized: false,
28+
ALPNProtocols: ['h2'],
29+
minVersion: 'TLSv1.3',
30+
maxVersion: 'TLSv1.3',
31+
};
32+
const socket = customConnection ? tls.connect(options) : undefined;
33+
const session = http2.connect(`https://127.0.0.1:${server.address().port}`, {
34+
...options,
35+
...(customConnection ? { createConnection: () => socket } : {}),
36+
});
37+
const transport = socket || session.socket;
38+
// Destroying the socket must not emit the TLS error a second time.
39+
transport.on('error', common.mustCall());
40+
let errorEmitted = false;
41+
session.on('connect', common.mustCall(() => {
42+
assert.strictEqual(transport.getProtocol(), 'TLSv1.3');
43+
}));
44+
session.on('error', common.mustCall((err) => {
45+
// OpenSSL and BoringSSL use different names for the same TLS alert.
46+
assert.ok([
47+
'ERR_SSL_TLSV13_ALERT_CERTIFICATE_REQUIRED',
48+
'ERR_SSL_TLSV1_ALERT_CERTIFICATE_REQUIRED',
49+
].includes(err.code), `Unexpected TLS error code: ${err.code}`);
50+
errorEmitted = true;
51+
}));
52+
transport.on('close', common.mustCall(function() {
53+
// The session's socket proxy may already be detached.
54+
assert(this.destroyed);
55+
}));
56+
session.on('close', common.mustCall(() => {
57+
assert(errorEmitted);
58+
assert(session.destroyed);
59+
if (socket) assert(socket.destroyed);
60+
server.close(common.mustCall());
61+
}));
62+
}));
63+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
minVersion: 'TLSv1.3',
16+
maxVersion: 'TLSv1.3',
17+
}, common.mustCall((socket) => {
18+
const remotePort = socket.remotePort;
19+
let errorEmitted = false;
20+
socket.on('error', common.mustCall((err) => {
21+
assert.match(err.code, /^ERR_SSL_/);
22+
assert(socket.destroyed);
23+
assert.strictEqual(socket.errored, err);
24+
// Destroy the socket while preserving connection details for this handler.
25+
assert.strictEqual(socket.remoteAddress, '127.0.0.1');
26+
assert.strictEqual(socket.remoteFamily, 'IPv4');
27+
assert.strictEqual(socket.remotePort, remotePort);
28+
assert.strictEqual(socket.getProtocol(), 'TLSv1.3');
29+
errorEmitted = true;
30+
}));
31+
socket.on('close', common.mustCall(() => {
32+
assert(errorEmitted);
33+
assert(socket.destroyed);
34+
assert.strictEqual(socket.getProtocol(), null);
35+
client.destroy();
36+
server.close(common.mustCall());
37+
}));
38+
}));
39+
server.on('tlsClientError', common.mustNotCall());
40+
41+
let client;
42+
server.listen(0, '127.0.0.1', common.mustCall(() => {
43+
const transport = net.connect(server.address().port, '127.0.0.1');
44+
client = tls.connect({
45+
socket: transport,
46+
rejectUnauthorized: false,
47+
minVersion: 'TLSv1.3',
48+
maxVersion: 'TLSv1.3',
49+
});
50+
client.on('error', () => {});
51+
client.on('close', common.mustCall());
52+
server.once('secureConnection', common.mustCall(() => {
53+
// Corrupt an application-data record without closing the TCP connection.
54+
// The server must close on the TLS error without application cleanup.
55+
const record = Buffer.alloc(37);
56+
record.set([23, 3, 3, 0, 32]);
57+
transport.write(record);
58+
}));
59+
}));

0 commit comments

Comments
 (0)