Skip to content

Commit 4cdb0e8

Browse files
jBarzjasnell
authored andcommitted
tls: keep track of stream that is closed
TLSWrap object keeps a pointer reference to the underlying TCPWrap object. This TCPWrap object could be closed and deleted by the event-loop which leaves us with a dangling pointer. So the TLSWrap object needs to track the "close" event on the TCPWrap object. PR-URL: #11776 Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
1 parent 303962a commit 4cdb0e8

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

lib/_tls_wrap.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
395395
res = null;
396396
});
397397

398+
if (wrap) {
399+
wrap.on('close', function() {
400+
res.onStreamClose();
401+
});
402+
}
403+
398404
return res;
399405
};
400406

src/tls_wrap.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ int TLSWrap::GetFD() {
543543

544544

545545
bool TLSWrap::IsAlive() {
546-
return ssl_ != nullptr && stream_->IsAlive();
546+
return ssl_ != nullptr && stream_ != nullptr && stream_->IsAlive();
547547
}
548548

549549

@@ -802,6 +802,14 @@ void TLSWrap::EnableSessionCallbacks(
802802
}
803803

804804

805+
void TLSWrap::OnStreamClose(const FunctionCallbackInfo<Value>& args) {
806+
TLSWrap* wrap;
807+
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
808+
809+
wrap->stream_ = nullptr;
810+
}
811+
812+
805813
void TLSWrap::DestroySSL(const FunctionCallbackInfo<Value>& args) {
806814
TLSWrap* wrap;
807815
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
@@ -932,6 +940,7 @@ void TLSWrap::Initialize(Local<Object> target,
932940
env->SetProtoMethod(t, "enableSessionCallbacks", EnableSessionCallbacks);
933941
env->SetProtoMethod(t, "destroySSL", DestroySSL);
934942
env->SetProtoMethod(t, "enableCertCb", EnableCertCb);
943+
env->SetProtoMethod(t, "onStreamClose", OnStreamClose);
935944

936945
StreamBase::AddMethods<TLSWrap>(env, t, StreamBase::kFlagHasWritev);
937946
SSLWrap<TLSWrap>::AddMethods(env, t);

src/tls_wrap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class TLSWrap : public AsyncWrap,
158158
static void EnableCertCb(
159159
const v8::FunctionCallbackInfo<v8::Value>& args);
160160
static void DestroySSL(const v8::FunctionCallbackInfo<v8::Value>& args);
161+
static void OnStreamClose(const v8::FunctionCallbackInfo<v8::Value>& args);
161162

162163
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
163164
static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
const tls = require('tls');
6+
const fs = require('fs');
7+
const net = require('net');
8+
9+
const key = fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem');
10+
const cert = fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem');
11+
12+
const T = 100;
13+
14+
// tls server
15+
const tlsServer = tls.createServer({ cert, key }, (socket) => {
16+
setTimeout(() => {
17+
socket.on('error', (error) => {
18+
assert.strictEqual(error.code, 'EINVAL');
19+
tlsServer.close();
20+
netServer.close();
21+
});
22+
socket.write('bar');
23+
}, T * 2);
24+
});
25+
26+
// plain tcp server
27+
const netServer = net.createServer((socket) => {
28+
// if client wants to use tls
29+
tlsServer.emit('connection', socket);
30+
31+
socket.setTimeout(T, () => {
32+
// this breaks if TLSSocket is already managing the socket:
33+
socket.destroy();
34+
});
35+
}).listen(0, common.mustCall(function() {
36+
37+
// connect client
38+
tls.connect({
39+
host: 'localhost',
40+
port: this.address().port,
41+
rejectUnauthorized: false
42+
}).write('foo');
43+
}));

0 commit comments

Comments
 (0)