fix(multipart): bound a text field by the body limit, not busboy's 1 MiB (GitHub #39)
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 33s

parseMultipart created busboy with `limits: { parts }` only, so
busboy's `fieldSize` default of 1 MiB applied whatever maxBodyBytes
allowed. busboy cuts a field at that size and only sets
`valueTruncated`, which nothing read: with the body limit raised, a
1,100,000-character field reached the handler as 1,048,576 characters.

acquireBody now passes the route's resolved body limit down as
`maxFieldBytes`, and parseMultipart gives it to busboy as `fieldSize`.
busboy flags a field once it reaches `fieldSize`, so it gets one byte
of headroom; otherwise a field of exactly the limit would count as
cut. A field that busboy still reports as truncated rejects with
HttpError 413, which parseRequestBody already renders with that
status, matching how an over-limit body fails. Node and app.fetch
(Deno, Bun, edge) share this path.

test/multipart-http.test.ts sends a 1,100,000-character field to a
route with maxBodyBytes 2,000,000 over both app.fetch and the Node
server. test/multipart.test.ts checks the 413 and the exact-limit
boundary on parseMultipart directly.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-24 19:29:31 -05:00
1 parent e7f08052f4
commit 235e483bfb
5 files changed
+56 -7

No files matched your search

+14 -2
View File
@@ -21,6 +21,8 @@ export interface MultipartBody {
export interface MultipartOpts {
maxParts: number;
duplicates: 'array' | 'last';
/** Largest text field accepted, in bytes. Unset leaves busboy's own default of 1 MiB. */
maxFieldBytes?: number;
}
/**
@@ -79,6 +81,7 @@ function loadBusboy(): ((cfg: unknown) => NodeJS.WritableStream & NodeJS.EventEm
* @param raw The full (already size-capped) request body.
* @param boundary The boundary token (see {@link extractBoundary}).
* @throws HttpError 501 when the optional `busboy` peer dependency is not installed.
* @throws HttpError 413 when a text field is larger than `maxFieldBytes`.
* @throws Error 'Invalid multipart body' on malformed input or when `maxParts` is exceeded.
*/
export function parseMultipart(raw: Buffer, boundary: string, opts: MultipartOpts): Promise<MultipartBody> {
@@ -93,16 +96,25 @@ export function parseMultipart(raw: Buffer, boundary: string, opts: MultipartOpt
const fail = () => reject(new Error('Invalid multipart body'));
let bb: NodeJS.WritableStream & NodeJS.EventEmitter;
// busboy flags a field as truncated once it *reaches* `fieldSize`, so a field of exactly
// `maxFieldBytes` needs one byte of headroom to count as whole.
const fieldSize = opts.maxFieldBytes === undefined ? undefined : opts.maxFieldBytes + 1;
try {
bb = busboy({
headers: { 'content-type': `multipart/form-data; boundary=${boundary}` },
limits: { parts: opts.maxParts },
limits: { parts: opts.maxParts, fieldSize },
});
} catch {
return fail();
}
bb.on('field', (name: string, value: string) => addField(body.fields, name, value, opts.duplicates));
bb.on('field', (name: string, value: string, info: { valueTruncated: boolean }) => {
// busboy cuts a field at `fieldSize` and only flags it. Partial data is not the field the
// client sent, so refuse the body as too large, like any other over-limit body.
if (info.valueTruncated) return reject(new HttpError(413, 'Payload Too Large'));
addField(body.fields, name, value, opts.duplicates);
});
bb.on('file', (name: string, stream: Readable, info: { filename?: string; mimeType?: string }) => {
const chunks: Buffer[] = [];