Skip to content

Commit f7ff8b4

Browse files
indutnyisaacs
authored andcommitted
tls: retry writing after hello parse error
When writing bad data to EncryptedStream it'll first get to the ClientHello parser, and, only after it will refuse it, to the OpenSSL. But ClientHello parser has limited buffer and therefore write could return `bytes_written` < `incoming_bytes`, which is not the case when working with OpenSSL. After such errors ClientHello parser disables itself and will pass-through all data to the OpenSSL. So just trying to write data one more time will throw the rest into OpenSSL and let it handle it.
1 parent 074e823 commit f7ff8b4

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

lib/tls.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ function CryptoStream(pair, options) {
253253
this._pendingEncoding = '';
254254
this._pendingCallback = null;
255255
this._doneFlag = false;
256+
this._retryAfterPartial = false;
256257
this._resumingSession = false;
257258
this._reading = true;
258259
this._destroyed = false;
@@ -361,7 +362,13 @@ CryptoStream.prototype._write = function write(data, encoding, cb) {
361362

362363
return cb(null);
363364
}
364-
assert(written === 0 || written === -1);
365+
if (written !== 0 && written !== -1) {
366+
assert(!this._retryAfterPartial);
367+
this._retryAfterPartial = true;
368+
this._write(data.slice(written), encoding, cb);
369+
this._retryAfterPartial = false;
370+
return;
371+
}
365372
} else {
366373
debug('cleartext.write queue is full');
367374

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var tls = require('tls');
24+
var net = require('net');
25+
var fs = require('fs');
26+
var assert = require('assert');
27+
28+
var options = {
29+
key: fs.readFileSync(common.fixturesDir + '/test_key.pem'),
30+
cert: fs.readFileSync(common.fixturesDir + '/test_cert.pem')
31+
};
32+
33+
var server = tls.createServer(options, function(c) {
34+
35+
}).listen(common.PORT, function() {
36+
var client = net.connect(common.PORT, function() {
37+
var bonkers = new Buffer(1024 * 1024);
38+
bonkers.fill(42);
39+
client.end(bonkers);
40+
});
41+
42+
var once = false;
43+
client.on('error', function() {
44+
if (!once) {
45+
once = true;
46+
client.destroy();
47+
server.close();
48+
}
49+
});
50+
});

0 commit comments

Comments
 (0)