From 5536093894f700920cbd0c0d729fe5ef415d518e Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 24 Oct 2026 22:21:10 -0500 Subject: [PATCH] fix(multipart): accept exactly maxParts parts (#118) busboy emits `partsLimit` once its part count reaches `limits.parts`, not when it passes it, and parseMultipart treats that event as a malformed body. With `maxParts: N` a body of exactly N parts was therefore rejected with 400 and only N-1 were accepted. parseMultipart now passes `maxParts + 1` to busboy, the same one-unit headroom it already gives `fieldSize`. N parts are accepted, N+1 are rejected. Route-level and server-level maxParts share this path, as do the Node server and app.fetch. test/multipart.test.ts adds the exact-N case on parseMultipart; test/multipart-http.test.ts sends exactly N parts at route and server level. All three new assertions fail without the fix. Signed-off-by: Diego Resendez --- CHANGELOG.md | 6 ++++++ src/multipart.ts | 5 ++++- test/multipart-http.test.ts | 2 ++ test/multipart.test.ts | 6 ++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bbc92d..61cf90d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -327,6 +327,12 @@ npm treats versions as semver and semver forbids leading zeros. handing the handler partial data. The Node server and `app.fetch` parse multipart through the same path, so the fix applies to both. +- **`maxParts: N` accepts exactly N parts** (#118). busboy signals its parts limit once the count + reaches it, so a body of exactly `maxParts` parts was rejected with `400 Invalid multipart body` + and only Nāˆ’1 got through. busboy now gets one part of headroom, the same way `fieldSize` does: + N parts are accepted and N+1 are rejected, as the option's name says. Both the route-level and + the server-level `maxParts` were affected, on the Node server and `app.fetch` alike. + - **`listen()` rejects when the port is taken** (#138). A bind failure such as `EADDRINUSE` or `EACCES` arrives as the server's `error` event, which `listen()` did not handle: the promise never settled, and the error surfaced as an unhandled one. It now rejects with that error. The diff --git a/src/multipart.ts b/src/multipart.ts index 8873784..b4735bd 100644 --- a/src/multipart.ts +++ b/src/multipart.ts @@ -99,11 +99,14 @@ export function parseMultipart(raw: Buffer, boundary: string, opts: MultipartOpt // 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; + // Same for parts: busboy emits `partsLimit` once the count *reaches* `parts`, so a body of + // exactly `maxParts` parts needs one of headroom to be accepted (Gitea #118). + const parts = opts.maxParts + 1; try { bb = busboy({ headers: { 'content-type': `multipart/form-data; boundary=${boundary}` }, - limits: { parts: opts.maxParts, fieldSize }, + limits: { parts, fieldSize }, }); } catch { return fail(); diff --git a/test/multipart-http.test.ts b/test/multipart-http.test.ts index 9a32b5d..0d4f156 100644 --- a/test/multipart-http.test.ts +++ b/test/multipart-http.test.ts @@ -147,6 +147,7 @@ describe('multipart limits on app.fetch (#105)', () => { @Module({ mountpoint: '/', controllers: [C] }) class CMod {} app = createApp({ modules: [CMod] }); + expect((await app.fetch(new Request('http://x/u', { method: 'POST', body: form(2) }))).status).toBe(200); // #118 const r = await app.fetch(new Request('http://x/u', { method: 'POST', body: form(3) })); expect(r.status).toBe(400); @@ -175,6 +176,7 @@ describe('multipart limits on app.fetch (#105)', () => { app = createApp({ modules: [CMod], limits: { maxParts: 2 } }); expect((await app.fetch(new Request('http://x/u', { method: 'POST', body: form(1) }))).status).toBe(200); + expect((await app.fetch(new Request('http://x/u', { method: 'POST', body: form(2) }))).status).toBe(200); expect((await app.fetch(new Request('http://x/u', { method: 'POST', body: form(3) }))).status).toBe(400); }); }); diff --git a/test/multipart.test.ts b/test/multipart.test.ts index 2efc0ae..d4d01c0 100644 --- a/test/multipart.test.ts +++ b/test/multipart.test.ts @@ -79,6 +79,12 @@ describe('parseMultipart', () => { await expect(parseMultipart(b, B, { maxParts: 2, duplicates: 'last' })).rejects.toThrow(); }); + // Gitea #118: busboy signals the limit when the count reaches it, which used to reject exactly N. + it('exactly maxParts parts → accepted', async () => { + const b = body([field('a', '1'), field('b', '2')]); + expect((await parseMultipart(b, B, { maxParts: 2, duplicates: 'last' })).fields).toEqual({ a: '1', b: '2' }); + }); + // 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 () => { -- 2.54.1