From 9a93873839753ddacc2227e93d450493b32e3ae9 Mon Sep 17 00:00:00 2001 From: Barath Date: Sat, 21 Nov 2026 12:20:08 +0000 Subject: [PATCH] tls: stop emitting data after ReadStop() When JS calls ReadStop() on a TLS socket, TLSWrap only stopped the TCP socket underneath. Records that were already received kept going through ClearOut() and were handed to JS anyway. I ran into this through the HTTP server. When a pipelined request comes in while a big response is still being sent, the server pauses the socket and the parser. Over TLS, the rest of the request was still pushed into the paused parser, which failed with HPE_PAUSED and the server dropped the connection. Plain HTTP was fine. With this change, ClearOut() stops after an emit if ReadStop() was called, and leaves the remaining records in enc_in_. ReadStart() then runs a cycle to deliver them, since the client may not send anything else that would trigger it. The cycle runs after the TCP socket is started again, because JS can pause and resume while handling the data, and starting the socket twice fails with EALREADY. Fixes: https://github.com/nodejs/node/issues/67455 Signed-off-by: Barath --- src/crypto/crypto_tls.cc | 21 ++++++- src/crypto/crypto_tls.h | 3 + .../test-https-pipeline-paused-parser.js | 55 +++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-https-pipeline-paused-parser.js diff --git a/src/crypto/crypto_tls.cc b/src/crypto/crypto_tls.cc index e092cb244684..6cbf55895dac 100644 --- a/src/crypto/crypto_tls.cc +++ b/src/crypto/crypto_tls.cc @@ -957,6 +957,14 @@ void TLSWrap::ClearOut() { } if (!emit()) return; + + // JS may have called ReadStop() while handling the data we just emitted, + // e.g. the HTTP server pausing a pipelined request under backpressure. + // Keep the remaining records in the SSL buffer until ReadStart(). + if (flags_.reading_stopped) { + Debug(this, "Returning from ClearOut(), reading stopped"); + return; + } } // We need to check whether an error occurred or the connection was @@ -1124,13 +1132,22 @@ bool TLSWrap::IsClosing() { int TLSWrap::ReadStart() { Debug(this, "ReadStart()"); + flags_.reading_stopped = false; + int err = 0; if (underlying_stream() != nullptr && !flags_.eof) - return underlying_stream()->ReadStart(); - return 0; + err = underlying_stream()->ReadStart(); + // ClearOut() stops emitting after ReadStop(), so encrypted records may + // still be waiting in enc_in_. Nothing else delivers them if no more data + // arrives from the network, so cycle now. This runs after the underlying + // stream is started, because the emitted data can make JS pause and resume + // the stream again. + if (ssl_ && BIO_pending(enc_in_) > 0) Cycle(); + return err; } int TLSWrap::ReadStop() { Debug(this, "ReadStop()"); + flags_.reading_stopped = true; return underlying_stream() != nullptr ? underlying_stream()->ReadStop() : 0; } diff --git a/src/crypto/crypto_tls.h b/src/crypto/crypto_tls.h index 7f5ac201a62c..aad0caf945e2 100644 --- a/src/crypto/crypto_tls.h +++ b/src/crypto/crypto_tls.h @@ -363,6 +363,9 @@ class TLSWrap : public AsyncWrap, // The handshake in progress is the peer's attempt to renegotiate, which // the TLS library is refusing. bool renegotiation_refused : 1; + // JS called ReadStop(). Decrypted data must not be emitted until + // ReadStart() is called. + bool reading_stopped : 1; }; Flags flags_{}; diff --git a/test/parallel/test-https-pipeline-paused-parser.js b/test/parallel/test-https-pipeline-paused-parser.js new file mode 100644 index 000000000000..b484101eef4d --- /dev/null +++ b/test/parallel/test-https-pipeline-paused-parser.js @@ -0,0 +1,55 @@ +'use strict'; +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +// Regression test for https://github.com/nodejs/node/issues/67455 +// When a pipelined request arrives while the server is draining a large +// response, the server pauses the socket and the parser. TLSWrap must not +// emit records it has already decrypted after ReadStop(), and must deliver +// them after ReadStart(), or the paused parser fails with HPE_PAUSED. + +const assert = require('assert'); +const https = require('https'); +const tls = require('tls'); +const fixtures = require('../common/fixtures'); + +// A small body arrives in one network read, so no later read would deliver +// the records held back during the pause. A large body spans several reads. +for (const bodySize of [20000, 100000]) { + const server = https.createServer({ + key: fixtures.readKey('agent1-key.pem'), + cert: fixtures.readKey('agent1-cert.pem'), + }, common.mustCall((req, res) => { + if (req.method === 'GET') { + // Large enough that the socket must drain, so the server pauses. + res.end(Buffer.alloc(1e7)); + return; + } + let received = 0; + req.on('data', (chunk) => received += chunk.length); + req.on('end', common.mustCall(() => { + assert.strictEqual(received, bodySize); + res.end(); + client.destroy(); + server.close(); + })); + }, 2)); + + server.on('clientError', common.mustNotCall()); + + let client; + server.listen(0, common.mustCall(() => { + client = tls.connect({ + port: server.address().port, + rejectUnauthorized: false, + }); + client.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n'); + // Pipeline a POST while the GET response is still being sent. + client.once('data', common.mustCall(() => { + client.write('POST / HTTP/1.1\r\nHost: localhost\r\n' + + `Content-Length: ${bodySize}\r\n\r\n` + + 'x'.repeat(bodySize)); + })); + })); +}