mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
An empty write on a TLS socket calls `ClearOut()` directly, even when it comes from JS that runs inside `Cycle()`, for example the HTTP parser's `on_body`. `ClearOut()` then decrypts the next record and passes it to the same listener while it is still processing the current one. For an HTTPS server this runs `llhttp_execute()` inside itself. The nested call can stop in the middle of a body chunk and leave the open span pointing into its own buffer, which is freed when it returns. The outer call then reports that span against its own buffer, so the request body contains server heap memory, or the process aborts. Skip the read on an empty write while a cycle is running. The cycle reads the next record once the listener has returned. In the HTTP parser, re-entrant `Execute()` calls remain supported, but each call now restores the caller's buffer when it returns, and data callbacks fail with `HPE_REENTRANT_EXECUTE` when they point outside the buffer that is being executed. Refs: https://hackerone.com/reports/4150886 Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs-private/node-private/pull/1021 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Tim Perry <pimterry@gmail.com> CVE-ID: CVE-2026-123864
129 lines
4.2 KiB
JavaScript
129 lines
4.2 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
|
|
// A zero-length write on a TLS socket used to read the next TLS record
|
|
// synchronously. When that write came from JavaScript running inside the
|
|
// HTTP parser's `on_body` callback, the parser was executed re-entrantly and
|
|
// the request body included memory that the client never sent.
|
|
//
|
|
// Request 1 is a HEAD request whose response headers are flushed early, so
|
|
// `res.end('done')` writes '' to the socket. Its 'end' handler runs from the
|
|
// tick queue while the body of request 2 is being parsed, with the next TLS
|
|
// record already received.
|
|
|
|
const assert = require('assert');
|
|
const https = require('https');
|
|
const net = require('net');
|
|
const tls = require('tls');
|
|
const { Duplex } = require('stream');
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
// Holds back writes so that several TLS records reach the server in a single
|
|
// TCP write.
|
|
class Batch extends Duplex {
|
|
constructor(socket) {
|
|
super();
|
|
this.socket = socket;
|
|
this.hold = false;
|
|
this.queue = [];
|
|
socket.on('data', (chunk) => this.push(chunk));
|
|
socket.on('end', () => this.push(null));
|
|
socket.on('close', () => this.destroy());
|
|
}
|
|
_read() {}
|
|
_write(chunk, encoding, callback) {
|
|
if (this.hold)
|
|
this.queue.push(chunk);
|
|
else
|
|
this.socket.write(chunk);
|
|
callback();
|
|
}
|
|
_final(callback) {
|
|
this.socket.end(callback);
|
|
}
|
|
_destroy(err, callback) {
|
|
this.socket.destroy();
|
|
callback(err);
|
|
}
|
|
flush() {
|
|
this.hold = false;
|
|
this.socket.write(Buffer.concat(this.queue));
|
|
this.queue = [];
|
|
}
|
|
}
|
|
|
|
const options = {
|
|
key: fixtures.readKey('agent1-key.pem'),
|
|
cert: fixtures.readKey('agent1-cert.pem'),
|
|
};
|
|
|
|
// Sends request 1, then the rest of it plus the start of request 2 and
|
|
// `record` as two TLS records in one TCP write, then `tail`.
|
|
function test(record, tail, check) {
|
|
return new Promise((resolve) => {
|
|
const bodies = [];
|
|
const server = https.createServer(options, (req, res) => {
|
|
res.flushHeaders();
|
|
const chunks = [];
|
|
bodies.push(chunks);
|
|
req.on('data', (chunk) => chunks.push(chunk));
|
|
req.on('error', () => {});
|
|
req.on('end', () => res.end('done'));
|
|
});
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const batch = new Batch(net.connect(server.address().port));
|
|
const socket = tls.connect({ socket: batch, rejectUnauthorized: false },
|
|
common.mustCall(() => {
|
|
socket.write('HEAD / HTTP/1.1\r\nHost: x\r\n' +
|
|
'Transfer-Encoding: chunked\r\n\r\n');
|
|
}));
|
|
let response = '';
|
|
socket.setEncoding('utf8');
|
|
socket.on('data', (chunk) => {
|
|
response += chunk;
|
|
if (response.endsWith('\r\n4\r\ndone\r\n0\r\n\r\n'))
|
|
socket.end();
|
|
});
|
|
socket.on('error', () => {});
|
|
socket.once('data', common.mustCall(() => {
|
|
batch.hold = true;
|
|
socket.write('0\r\n\r\nPOST / HTTP/1.1\r\nHost: x\r\n' +
|
|
'Transfer-Encoding: chunked\r\n\r\n1\r\nZ', common.mustCall(() => {
|
|
socket.write(record, common.mustCall(() => {
|
|
batch.flush();
|
|
if (tail)
|
|
socket.write(tail);
|
|
}));
|
|
}));
|
|
}));
|
|
socket.on('close', common.mustCall(() => {
|
|
server.close();
|
|
check(bodies.map((chunks) => Buffer.concat(chunks).toString('latin1')),
|
|
response);
|
|
resolve();
|
|
}));
|
|
}));
|
|
});
|
|
}
|
|
|
|
(async () => {
|
|
// A valid request 2: its body arrives intact and both requests get a
|
|
// response.
|
|
await test('\r\n5\r\nHE', 'LLO\r\n0\r\n\r\n', (bodies, response) => {
|
|
assert.deepStrictEqual(bodies, ['', 'ZHELLO']);
|
|
assert.strictEqual(response.split('HTTP/1.1 200 OK\r\n').length, 3);
|
|
assert.strictEqual(response.split('done').length, 2);
|
|
});
|
|
|
|
// The chunk 'Z' is not followed by CRLF, so request 2 is a parse error. The
|
|
// body must not contain anything but the byte that was sent.
|
|
await test('5\r\nHE', null, (bodies, response) => {
|
|
assert.deepStrictEqual(bodies, ['', 'Z']);
|
|
assert.match(response, /^HTTP\/1\.1 200 OK\r\n/);
|
|
});
|
|
})().then(common.mustCall());
|