Skip to content

Commit f240da5

Browse files
committed
TLS/SSL: TLSSocket emits 'error' on handshake failure
Fixes #8803
1 parent b2534f1 commit f240da5

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/_tls_wrap.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,13 @@ TLSSocket.prototype._init = function(socket, wrap) {
426426

427427
// Destroy socket if error happened before handshake's finish
428428
if (!self._secureEstablished) {
429-
self.destroy(self._tlsError(err));
429+
if (!self._controlReleased) {
430+
// When handshake fails control is not yet released,
431+
// so self._tlsError will return null instead of actual error
432+
self.destroy(err);
433+
} else {
434+
self.destroy(self._tlsError(err));
435+
}
430436
} else if (options.isServer &&
431437
rejectUnauthorized &&
432438
/peer did not return a certificate/.test(err.message)) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
if (!common.hasCrypto) {
5+
common.skip('missing crypto');
6+
return;
7+
}
8+
const tls = require('tls');
9+
const net = require('net');
10+
const assert = require('assert');
11+
12+
const bonkers = Buffer.alloc(1024, 42);
13+
14+
const server = net.createServer(function(c) {
15+
setTimeout(function() {
16+
const s = new tls.TLSSocket(c, {
17+
isServer: true,
18+
server: server
19+
});
20+
21+
s.on('error', common.mustCall(function(e) {
22+
assert.ok(e instanceof Error,
23+
'Instance of Error should be passed to error handler');
24+
assert.ok(e.message.match(
25+
/SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol/),
26+
'Expecting SSL unknown protocol');
27+
}));
28+
29+
s.on('close', function() {
30+
server.close();
31+
s.destroy();
32+
});
33+
}, common.platformTimeout(200));
34+
}).listen(0, function() {
35+
const c = net.connect({port: this.address().port}, function() {
36+
c.write(bonkers);
37+
});
38+
});

0 commit comments

Comments
 (0)