Skip to content

[Flight] decodeReplyFromBusboy hangs after early Busboy close and resolves fileSize-truncated uploads #38451

Backlog

Description

@Tortleya

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:

  1. Permanent hang after early Busboy 'close': Destroying an application/x-www-form-urlencoded Busboy parser (busboyStream.destroy()) before completion emits 'close' with neither 'finish' nor 'error', leaving the thenable returned by decodeReplyFromBusboy pending forever.
  2. Silent resolution of fileSize-truncated uploads: When Busboy is configured with limits: {fileSize}, exceeding the limit emits 'limit' on the file stream rather than 'error'. decodeReplyFromBusboy ignores 'limit' and resolves normally with a truncated Blob/File.

Affected version

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(),
  });
})();
JS

Actual 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 busboyStream emits 'close' prematurely (before Busboy 'finish' and without 'error'), decodeReplyFromBusboy should reject the reply instead of hanging indefinitely.
  • Case 2: When Busboy signals a fileSize limit breach via fileStream's 'limit' event, decodeReplyFromBusboy should reject the reply rather than silently resolving with a truncated Blob/File. Because decodeReplyFromBusboy owns the returned reply promise, callers passing a limits-configured Busboy instance otherwise silently receive truncated data as if the upload succeeded intact.

Root cause

In decodeReplyFromBusboy:

  • Only 'field' (#L596), 'file' (#L612), 'finish' (#L652), and 'error' (#L664) are attached on busboyStream. There is no 'close' listener for premature parser close before bodyFinished is 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 #L596 ignores Busboy's third info argument (info.valueTruncated / info.nameTruncated for fieldSize / fieldNameSize limits).

The same implementation is duplicated in:

Suggested fix

  1. Attach busboyStream.on('close', ...) that distinguishes premature parser close from normal 'finish': if !bodyFinished && !closed, mark closed = true, clear head/tail, and call reportGlobalError(response, new Error('Connection closed.')) (and also mark closed = true / clear head/tail inside the 'error' handler). Checking !bodyFinished avoids falsely aborting a finished multipart parser whose last file stream is still draining.
  2. Attach value.on('limit', ...) (and, for complete Busboy limit enforcement, handle 'fieldsLimit' / 'filesLimit' / 'partsLimit' and check info.valueTruncated || info.nameTruncated in the 'field' callback) to destroy busboyStream with an explicit limit error (busboyStream.destroy(err), or reportGlobalError + busboyStream.destroy(err)) instead of resolving truncated data.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions