Skip to content

Commit

Permalink
tls: implement capture rejections for 'secureConnection' event
Browse files Browse the repository at this point in the history
PR-URL: #27867
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
mcollina authored and targos committed Dec 9, 2019
1 parent 4eecee0 commit 5ad3efb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ assertCrypto();
const { setImmediate } = require('timers');
const assert = require('internal/assert');
const crypto = require('crypto');
const EE = require('events');
const net = require('net');
const tls = require('tls');
const common = require('_tls_common');
Expand Down Expand Up @@ -1284,6 +1285,19 @@ Server.prototype.addContext = function(servername, context) {
this._contexts.push([re, tls.createSecureContext(context).context]);
};

Server.prototype[EE.captureRejectionSymbol] = function(
err, event, sock) {

switch (event) {
case 'secureConnection':
sock.destroy(err);
break;
default:
net.Server.prototype[Symbol.for('nodejs.rejection')]
.call(this, err, event, sock);
}
};

function SNICallback(servername, callback) {
const contexts = this.server._contexts;

Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-tls-server-capture-rejection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const events = require('events');
const fixtures = require('../common/fixtures');
const { createServer, connect } = require('tls');
const cert = fixtures.readKey('rsa_cert.crt');
const key = fixtures.readKey('rsa_private.pem');

events.captureRejections = true;

const server = createServer({ cert, key }, common.mustCall(async (sock) => {
server.close();

const _err = new Error('kaboom');
sock.on('error', common.mustCall((err) => {
assert.strictEqual(err, _err);
}));
throw _err;
}));

server.listen(0, common.mustCall(() => {
const sock = connect({
port: server.address().port,
host: server.address().host,
rejectUnauthorized: false
});

sock.on('close', common.mustCall());
}));

0 comments on commit 5ad3efb

Please sign in to comment.