Files
node/test/parallel/test-http-parser-reentrant-data.js
Matteo Collina 6c5b08e1fc http: never execute llhttp re-entrantly
llhttp is not reentrant: it keeps its position in the input in the
parser state and only updates it when `llhttp_execute()` returns.
Instead of supporting nested calls, make sure that llhttp never runs
inside itself.

- `socketOnData` on the server and the client puts data that arrives
  while the parser is executing back into the socket, and resumes the
  socket after the current call.
- `Parser::Execute()` does not run llhttp while it is already running.
  A nested call with data fails the outer call with
  `HPE_REENTRANT_EXECUTE`. A nested `finish()`, which comes from
  tearing down the connection in a callback, is skipped.

This replaces the buffer bounds check on data callbacks.

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
2026-11-11 17:59:41 +01:00

82 lines
2.4 KiB
JavaScript

// Flags: --expose-internals
'use strict';
// Data that a socket emits synchronously from inside a parser callback, for
// example by pushing into a Duplex, must be parsed after the current call
// to the parser returns, not from inside it.
const common = require('../common');
const assert = require('assert');
const http = require('http');
const { Duplex } = require('stream');
const { internalBinding } = require('internal/test/binding');
const { HTTPParser } = internalBinding('http_parser');
const execute = HTTPParser.prototype.execute;
let depth = 0;
HTTPParser.prototype.execute = common.mustCallAtLeast(function(...args) {
assert.strictEqual(depth, 0);
depth++;
try {
return execute.apply(this, args);
} finally {
depth--;
}
}, 4);
{
// Server: the request handler of the first request pushes the second one.
const urls = [];
let output = '';
const connection = new Duplex({
read() {},
write(chunk, encoding, callback) {
output += chunk;
callback();
},
});
const server = http.createServer(common.mustCall((req, res) => {
urls.push(req.url);
if (req.url === '/a')
connection.push('GET /b HTTP/1.1\r\nHost: x\r\n\r\n');
res.end('ok');
}, 2));
server.emit('connection', connection);
connection.push('GET /a HTTP/1.1\r\nHost: x\r\n\r\n');
setImmediate(common.mustCall(() => {
assert.deepStrictEqual(urls, ['/a', '/b']);
assert.strictEqual(output.split('HTTP/1.1 200 OK\r\n').length, 3);
}));
}
{
// Client: the 'information' handler pushes the final response.
let socket;
const req = http.request('http://localhost', {
createConnection() {
socket = new Duplex({
read() {},
write(chunk, encoding, callback) {
if (chunk.toString().includes('\r\n\r\n'))
this.push('HTTP/1.1 100 Continue\r\n\r\n');
callback();
},
});
return socket;
},
});
req.on('information', common.mustCall(({ statusCode }) => {
assert.strictEqual(statusCode, 100);
socket.push('HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok');
socket.push(null);
}));
req.on('response', common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
res.setEncoding('utf8');
let body = '';
res.on('data', (chunk) => body += chunk);
res.on('end', common.mustCall(() => assert.strictEqual(body, 'ok')));
}));
req.end();
}