fix(multipart): bound a text field by the body limit, not busboy's 1 MiB (GitHub #39)
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:
5 files changed
+56
-7
No files matched your search
+10
-5
@@ -71,6 +71,7 @@ export async function parseRequestBody(
|
||||
contentType: string,
|
||||
duplicates: 'array' | 'last',
|
||||
maxParts: number,
|
||||
maxFieldBytes?: number,
|
||||
): Promise<ParseResult> {
|
||||
const type = mediaType(contentType);
|
||||
|
||||
@@ -93,9 +94,10 @@ export async function parseRequestBody(
|
||||
if (!boundary) return { error: 'Invalid multipart body' };
|
||||
|
||||
try {
|
||||
return { body: await parseMultipart(buf, boundary, { maxParts, duplicates }) };
|
||||
return { body: await parseMultipart(buf, boundary, { maxParts, duplicates, maxFieldBytes }) };
|
||||
} catch (error) {
|
||||
// HttpError = the busboy peer dep is missing (501); anything else = malformed input (400).
|
||||
// HttpError = the busboy peer dep is missing (501) or a field is over the limit (413);
|
||||
// anything else = malformed input (400).
|
||||
if (isHttpError(error)) return { error: error.message, status: error.status };
|
||||
return { error: 'Invalid multipart body' };
|
||||
}
|
||||
@@ -128,12 +130,13 @@ export function acquireBody(
|
||||
// this whole path without a single promise, and an `async` wrapper here would allocate one
|
||||
// regardless. Same reason `handle()` is not async.
|
||||
return isPromise(acquired)
|
||||
? acquired.then((settled) => parseAcquired(settled, matched, opts, req))
|
||||
: parseAcquired(acquired, matched, opts, req);
|
||||
? acquired.then((settled) => parseAcquired(settled, limit, matched, opts, req))
|
||||
: parseAcquired(acquired, limit, matched, opts, req);
|
||||
}
|
||||
|
||||
function parseAcquired(
|
||||
acquired: { bytes: Buffer | undefined } | BodyFailure,
|
||||
limit: number,
|
||||
matched: MatchedRoute,
|
||||
opts: HttpOptions | undefined,
|
||||
req: ErrorRequest,
|
||||
@@ -149,7 +152,9 @@ function parseAcquired(
|
||||
const duplicates = matched.def.bodyDuplicates ?? opts?.bodyDuplicates ?? 'last';
|
||||
const maxParts = matched.def.maxParts ?? opts?.limits?.maxParts ?? 1000;
|
||||
|
||||
return parseRequestBody(acquired.bytes, contentType, duplicates, maxParts).then((parsed) =>
|
||||
// A field is bounded by the body limit alone. busboy's own 1 MiB default would otherwise cut any
|
||||
// larger field that `maxBodyBytes` was raised to admit.
|
||||
return parseRequestBody(acquired.bytes, contentType, duplicates, maxParts, limit).then((parsed) =>
|
||||
'error' in parsed
|
||||
? { fail: renderError(new HttpError(parsed.status ?? 400, parsed.error), req, opts?.onError, opts?.logger) }
|
||||
: { body: parsed.body },
|
||||
|
||||
Reference in new issue
Block a user