Skip to content

Commit 809bb0a

Browse files
committed
tls: nullify .ssl on handle close
This is an intermediate fix for an issue of accessing `TLSWrap` fields after the parent handle was destroyed. While `close` listener cleans up this field automatically, it can be done even earlier at the `TLSWrap.close` call. Proper fix is going to be submitted and landed after this one. Fix: #5108
1 parent 1693349 commit 809bb0a

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/_tls_wrap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ proxiedMethods.forEach(function(name) {
299299
});
300300

301301
tls_wrap.TLSWrap.prototype.close = function closeProxy(cb) {
302+
if (this.owner)
303+
this.owner.ssl = null;
304+
302305
if (this._parentWrap && this._parentWrap._handle === this._parent) {
303306
this._parentWrap.once('close', cb);
304307
return this._parentWrap.destroy();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
if (!common.hasCrypto) {
6+
console.log('1..0 # Skipped: node compiled without crypto.');
7+
return;
8+
}
9+
10+
const assert = require('assert');
11+
const tls = require('tls');
12+
const fs = require('fs');
13+
const path = require('path');
14+
15+
const keyDir = path.join(common.fixturesDir, 'keys');
16+
const key = fs.readFileSync(path.join(keyDir, 'agent1-key.pem'));
17+
const cert = fs.readFileSync(path.join(keyDir, 'agent1-cert.pem'));
18+
19+
const server = tls.createServer({
20+
key: key,
21+
cert: cert
22+
}, function(c) {
23+
c.end();
24+
}).listen(common.PORT, function() {
25+
var client = tls.connect({
26+
port: common.PORT,
27+
rejectUnauthorized: false
28+
}, common.mustCall(function() {
29+
client.destroy();
30+
if (!client._events.close)
31+
client._events.close = [];
32+
else if (!Array.isArray(client._events.close))
33+
client._events.close = [ client._events.close ];
34+
35+
client._events.close.unshift(common.mustCall(function() {
36+
setImmediate(function() {
37+
// Make it crash on unpatched node.js
38+
var fd = client.ssl && client.ssl.fd;
39+
assert(client.ssl === null);
40+
assert(!fd);
41+
});
42+
}));
43+
server.close();
44+
}));
45+
});

0 commit comments

Comments
 (0)