Skip to content

Commit d3027e8

Browse files
committed
TLS/SSL: Removes unnecessary code branching
Removes branch that would make TLSSocket emit '_tlsError' event if error occured on handshake and controll was not released, as it was never happening. Addedd test for tls.Server to ensure it still emits 'tlsClientError' as expected. Makes tests conform to defined linting rules.
1 parent f240da5 commit d3027e8

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

lib/_tls_wrap.js

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

427427
// Destroy socket if error happened before handshake's finish
428428
if (!self._secureEstablished) {
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-
}
429+
// When handshake fails control is not yet released,
430+
// so self._tlsError will return null instead of actual error
431+
self.destroy(err);
436432
} else if (options.isServer &&
437433
rejectUnauthorized &&
438434
/peer did not return a certificate/.test(err.message)) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
let tlsClientErrorEmited = false;
15+
16+
const server = tls.createServer({})
17+
.listen(0, function() {
18+
const c = net.connect({ port: this.address().port }, function() {
19+
c.write(bonkers);
20+
});
21+
22+
}).on('tlsClientError', function(e) {
23+
tlsClientErrorEmited = true;
24+
assert.ok(e instanceof Error,
25+
'Instance of Error should be passed to error handler');
26+
assert.ok(e.message.match(
27+
/SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol/),
28+
'Expecting SSL unknown protocol');
29+
});
30+
31+
setTimeout(function() {
32+
server.close();
33+
34+
assert.ok(tlsClientErrorEmited,
35+
'tlsClientError should be emited');
36+
37+
}, common.platformTimeout(200));

test/parallel/test-tls-failed-handshake-emits-error.js renamed to test/parallel/test-tls-socket-failed-handshake-emits-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const server = net.createServer(function(c) {
2020

2121
s.on('error', common.mustCall(function(e) {
2222
assert.ok(e instanceof Error,
23-
'Instance of Error should be passed to error handler');
23+
'Instance of Error should be passed to error handler');
2424
assert.ok(e.message.match(
2525
/SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol/),
2626
'Expecting SSL unknown protocol');

0 commit comments

Comments
 (0)