GeezerVison is showing github.com — tls: destroy sockets after fatal errors · nodejs/node@eb6a74b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/tls/wrap.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -625,8 +625,10 @@ function onerror(err) { | 625 | 625 | // Ignore server's authorization errors | 626 | 626 | owner.destroy(); | 627 | 627 | } 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); | 630 | 632 | } | 631 | 633 | } | 632 | 634 |
| |
Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboard +59Lines changed: 59 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +})); |
|