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: https://github.com/nodejs/node/pull/11776
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
This commit is contained in:
jBarz 2017-03-09 08:33:59 -05:00 коммит произвёл James M Snell
Родитель 303962aca1
Коммит 4cdb0e89d8
4 изменённых файлов: 60 добавлений и 1 удалений

Просмотреть файл

@ -395,6 +395,12 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
res = null; res = null;
}); });
if (wrap) {
wrap.on('close', function() {
res.onStreamClose();
});
}
return res; return res;
}; };

Просмотреть файл

@ -543,7 +543,7 @@ int TLSWrap::GetFD() {
bool TLSWrap::IsAlive() { bool TLSWrap::IsAlive() {
return ssl_ != nullptr && stream_->IsAlive(); return ssl_ != nullptr && stream_ != nullptr && stream_->IsAlive();
} }
@ -802,6 +802,14 @@ void TLSWrap::EnableSessionCallbacks(
} }
void TLSWrap::OnStreamClose(const FunctionCallbackInfo<Value>& args) {
TLSWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
wrap->stream_ = nullptr;
}
void TLSWrap::DestroySSL(const FunctionCallbackInfo<Value>& args) { void TLSWrap::DestroySSL(const FunctionCallbackInfo<Value>& args) {
TLSWrap* wrap; TLSWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
@ -932,6 +940,7 @@ void TLSWrap::Initialize(Local<Object> target,
env->SetProtoMethod(t, "enableSessionCallbacks", EnableSessionCallbacks); env->SetProtoMethod(t, "enableSessionCallbacks", EnableSessionCallbacks);
env->SetProtoMethod(t, "destroySSL", DestroySSL); env->SetProtoMethod(t, "destroySSL", DestroySSL);
env->SetProtoMethod(t, "enableCertCb", EnableCertCb); env->SetProtoMethod(t, "enableCertCb", EnableCertCb);
env->SetProtoMethod(t, "onStreamClose", OnStreamClose);
StreamBase::AddMethods<TLSWrap>(env, t, StreamBase::kFlagHasWritev); StreamBase::AddMethods<TLSWrap>(env, t, StreamBase::kFlagHasWritev);
SSLWrap<TLSWrap>::AddMethods(env, t); SSLWrap<TLSWrap>::AddMethods(env, t);

Просмотреть файл

@ -158,6 +158,7 @@ class TLSWrap : public AsyncWrap,
static void EnableCertCb( static void EnableCertCb(
const v8::FunctionCallbackInfo<v8::Value>& args); const v8::FunctionCallbackInfo<v8::Value>& args);
static void DestroySSL(const v8::FunctionCallbackInfo<v8::Value>& args); static void DestroySSL(const v8::FunctionCallbackInfo<v8::Value>& args);
static void OnStreamClose(const v8::FunctionCallbackInfo<v8::Value>& args);
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args); static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);

Просмотреть файл

@ -0,0 +1,43 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const tls = require('tls');
const fs = require('fs');
const net = require('net');
const key = fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem');
const cert = fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem');
const T = 100;
// tls server
const tlsServer = tls.createServer({ cert, key }, (socket) => {
setTimeout(() => {
socket.on('error', (error) => {
assert.strictEqual(error.code, 'EINVAL');
tlsServer.close();
netServer.close();
});
socket.write('bar');
}, T * 2);
});
// plain tcp server
const netServer = net.createServer((socket) => {
// if client wants to use tls
tlsServer.emit('connection', socket);
socket.setTimeout(T, () => {
// this breaks if TLSSocket is already managing the socket:
socket.destroy();
});
}).listen(0, common.mustCall(function() {
// connect client
tls.connect({
host: 'localhost',
port: this.address().port,
rejectUnauthorized: false
}).write('foo');
}));