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
@@ -153,6 +153,22 @@ describe('multipart limits on app.fetch (#105)', () => {
|
||||
expect(await r.text()).toContain('Invalid multipart body');
|
||||
});
|
||||
|
||||
// GitHub #39: busboy's own 1 MiB field limit used to apply whatever maxBodyBytes allowed, and cut
|
||||
// the field without an error. A field is now bounded only by the body limit, on both adapters.
|
||||
it('a text field over 1 MiB reaches the handler whole when maxBodyBytes allows it', async () => {
|
||||
@Route('/') class C { @Post('/u', { maxBodyBytes: 2_000_000 }) u(@body() b: any) { return { length: b.fields.big.length }; } }
|
||||
@Module({ mountpoint: '/', controllers: [C] }) class CMod {}
|
||||
app = createApp({ modules: [CMod] });
|
||||
const big = () => { const fd = new FormData(); fd.set('big', 'x'.repeat(1_100_000)); return fd; };
|
||||
|
||||
const viaFetch = await app.fetch(new Request('http://x/u', { method: 'POST', body: big() }));
|
||||
expect(await viaFetch.json()).toEqual({ length: 1_100_000 });
|
||||
|
||||
const server = await app.listen(0);
|
||||
const viaNode = await fetch(`http://127.0.0.1:${(server.address() as any).port}/u`, { method: 'POST', body: big() });
|
||||
expect(await viaNode.json()).toEqual({ length: 1_100_000 });
|
||||
});
|
||||
|
||||
it('T7: the server-level maxParts applies when the route sets none', async () => {
|
||||
@Route('/') class C { @Post('/u') u(@body() b: any) { return b; } }
|
||||
@Module({ mountpoint: '/', controllers: [C] }) class CMod {}
|
||||
|
||||
@@ -79,6 +79,14 @@ describe('parseMultipart', () => {
|
||||
await expect(parseMultipart(b, B, { maxParts: 2, duplicates: 'last' })).rejects.toThrow();
|
||||
});
|
||||
|
||||
// GitHub #39: busboy cuts a field at its size limit and only flags it, so the parser has to refuse
|
||||
// the body rather than hand the handler partial data.
|
||||
it('a field over maxFieldBytes → rejects with 413 instead of truncating', async () => {
|
||||
const b = body([field('a', 'x'.repeat(10))]);
|
||||
await expect(parseMultipart(b, B, { ...opts, maxFieldBytes: 4 })).rejects.toMatchObject({ status: 413 });
|
||||
expect((await parseMultipart(b, B, { ...opts, maxFieldBytes: 10 })).fields.a).toBe('x'.repeat(10));
|
||||
});
|
||||
|
||||
it('malformed input with no matching boundary → rejects', async () => {
|
||||
await expect(parseMultipart(Buffer.from('garbage'), B, opts)).rejects.toThrow();
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user