Repository navigation
Backlog
Description
Summary
decodeReplyFromBusboy only listens for 'field', 'file', 'finish', and 'error' on busboyStream (and 'data', 'error', 'end' on file streams), ignoring 'close', file 'limit', count-limit events, and the field callback's truncation metadata (info.valueTruncated / info.nameTruncated). With [email protected], this produces two concrete failure modes:
- Permanent hang after early Busboy
'close': Destroying anapplication/x-www-form-urlencodedBusboy parser (busboyStream.destroy()) before completion emits'close'with neither'finish'nor'error', leaving the thenable returned bydecodeReplyFromBusboypending forever. - Silent resolution of
fileSize-truncated uploads: When Busboy is configured withlimits: {fileSize}, exceeding the limit emits'limit'on the file stream rather than'error'.decodeReplyFromBusboyignores'limit'and resolves normally with a truncatedBlob/File.
Affected version
[email protected],[email protected], Node.jsv22.22.2- Verified unchanged on
mainatedae846a9a17442c00429f2b15375afbfa0376c9acrossreact-server-dom-webpack,react-server-dom-turbopack,react-server-dom-parcel,react-server-dom-esm, andreact-server-dom-unbundled
Reproduction
In an empty directory:
npm install [email protected] [email protected] [email protected] [email protected]
node --conditions=react-server <<'JS'
const busboy = require('busboy');
const {decodeReplyFromBusboy} = require('react-server-dom-webpack/server.node');
(async () => {
// Case 1: destroyed application/x-www-form-urlencoded Busboy parser -> permanent hang
const bbU = busboy({
headers: {'content-type': 'application/x-www-form-urlencoded'},
});
const evU = [];
for (const e of ['field', 'finish', 'close', 'error']) {
bbU.on(e, x => evU.push(e + (e === 'error' ? ':' + x.message : '')));
}
const pU = decodeReplyFromBusboy(bbU, {});
bbU.write('0=%7B%22a%22');
bbU.destroy();
const rU = await Promise.race([
Promise.resolve(pU).then(
v => 'RESOLVED:' + JSON.stringify(v),
e => 'REJECTED:' + e.message,
),
new Promise(r => setTimeout(() => r('HUNG_FOREVER'), 300)),
]);
console.log('Case 1 (urlencoded destroy()):', {events: evU, result: rU});
// Case 2: multipart file exceeding busboy limits.fileSize -> silently truncated Blob
global.__webpack_require__ = Object.assign(() => ({}), {u: () => ''});
const {encodeReply} = require('react-server-dom-webpack/client.browser');
const formData = await encodeReply([
new Blob(['0123456789'], {type: 'text/plain'}),
]);
const req = new Request('http://localhost/', {method: 'POST', body: formData});
const bodyBuf = Buffer.from(await req.arrayBuffer());
const bbL = busboy({
headers: {'content-type': req.headers.get('content-type')},
limits: {fileSize: 5},
});
let fileLimitFired = false;
bbL.on('file', (n, s) => s.on('limit', () => { fileLimitFired = true; }));
const pL = decodeReplyFromBusboy(bbL, {});
bbL.end(bodyBuf);
const rL = await Promise.resolve(pL);
console.log('Case 2 (multipart fileSize:5):', {
fileLimitFired,
gotSize: rL[0].size,
gotText: await rL[0].text(),
});
})();
JSActual output
Case 1 (urlencoded destroy()): { events: [ 'close' ], result: 'HUNG_FOREVER' }
Case 2 (multipart fileSize:5): { fileLimitFired: true, gotSize: 5, gotText: '01234' }
Expected behavior
- Case 1: When
busboyStreamemits'close'prematurely (before Busboy'finish'and without'error'),decodeReplyFromBusboyshould reject the reply instead of hanging indefinitely. - Case 2: When Busboy signals a
fileSizelimit breach viafileStream's'limit'event,decodeReplyFromBusboyshould reject the reply rather than silently resolving with a truncatedBlob/File. BecausedecodeReplyFromBusboyowns the returned reply promise, callers passing alimits-configured Busboy instance otherwise silently receive truncated data as if the upload succeeded intact.
Root cause
- Only
'field'(#L596),'file'(#L612),'finish'(#L652), and'error'(#L664) are attached onbusboyStream. There is no'close'listener for premature parser close beforebodyFinishedis set. - Inside the
'file'handler (#L637-L650),value.on('limit', ...)is never attached. Likewise, count-limit events ('fieldsLimit','filesLimit','partsLimit') are not handled, and the'field'callback at#L596ignores Busboy's thirdinfoargument (info.valueTruncated/info.nameTruncatedforfieldSize/fieldNameSizelimits).
The same implementation is duplicated in:
packages/react-server-dom-turbopack/src/server/ReactFlightDOMServerNode.js#L541-L674packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js#L549-L681packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js#L334-L467packages/react-server-dom-unbundled/src/server/ReactFlightDOMServerNode.js#L540-L673
Suggested fix
- Attach
busboyStream.on('close', ...)that distinguishes premature parser close from normal'finish': if!bodyFinished && !closed, markclosed = true, clearhead/tail, and callreportGlobalError(response, new Error('Connection closed.'))(and also markclosed = true/ clearhead/tailinside the'error'handler). Checking!bodyFinishedavoids falsely aborting a finished multipart parser whose last file stream is still draining. - Attach
value.on('limit', ...)(and, for complete Busboy limit enforcement, handle'fieldsLimit'/'filesLimit'/'partsLimit'and checkinfo.valueTruncated || info.nameTruncatedin the'field'callback) to destroybusboyStreamwith an explicit limit error (busboyStream.destroy(err), orreportGlobalError+busboyStream.destroy(err)) instead of resolving truncated data.
Metadata
Metadata
Assignees
Labels
No labels
Activity