Files
core/test/multipart.test.ts
T
chrnx 5536093894
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 30s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 34s
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 <diego.resendez@zero-oneit.com>
2026-10-24 22:21:10 -05:00

100 lines
4.6 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { parseMultipart, extractBoundary } from '../src/multipart';
const B = 'X-BOUND';
function body(parts: string[]): Buffer {
return Buffer.concat([...parts.map((p) => Buffer.from(`--${B}\r\n${p}\r\n`)), Buffer.from(`--${B}--\r\n`)]);
}
const field = (name: string, val: string) => `Content-Disposition: form-data; name="${name}"\r\n\r\n${val}`;
const file = (name: string, fn: string, ct: string, data: string) =>
`Content-Disposition: form-data; name="${name}"; filename="${fn}"\r\nContent-Type: ${ct}\r\n\r\n${data}`;
const opts = { maxParts: 1000, duplicates: 'last' as const };
describe('extractBoundary', () => {
it('plain and quoted', () => {
expect(extractBoundary('multipart/form-data; boundary=abc')).toBe('abc');
expect(extractBoundary('multipart/form-data; boundary="a b"')).toBe('a b');
expect(extractBoundary('multipart/form-data')).toBeUndefined();
});
});
describe('parseMultipart', () => {
it('single text field is decoded', async () => {
const r = await parseMultipart(body([field('title', 'hello')]), B, opts);
expect(r.fields.title).toBe('hello');
});
it('multiple distinct fields', async () => {
const r = await parseMultipart(body([field('a', '1'), field('b', '2')]), B, opts);
expect(r.fields).toEqual({ a: '1', b: '2' });
});
it("duplicates 'last' vs 'array'", async () => {
const b = body([field('t', 'x'), field('t', 'y')]);
expect((await parseMultipart(b, B, { maxParts: 1000, duplicates: 'last' })).fields.t).toBe('y');
expect((await parseMultipart(b, B, { maxParts: 1000, duplicates: 'array' })).fields.t).toEqual(['x', 'y']);
});
it('single file: metadata + exact bytes incl. embedded CRLF and dashes', async () => {
// A real boundary is a random token that never appears in content; this exercises CRLF + `--` runs.
const payload = 'ab\r\ncd\r\n--decoy--\r\nef';
const r = await parseMultipart(body([file('doc', 'a.txt', 'text/plain', payload)]), B, opts);
const f = r.files.doc as any;
expect(f.filename).toBe('a.txt');
expect(f.contentType).toBe('text/plain');
expect(f.size).toBe(Buffer.byteLength(payload));
expect(f.data.toString()).toBe(payload);
});
it('multiple files same name → array', async () => {
const r = await parseMultipart(
body([file('f', '1', 'text/plain', 'a'), file('f', '2', 'text/plain', 'b')]),
B,
opts,
);
expect(Array.isArray(r.files.f)).toBe(true);
expect((r.files.f as any).length).toBe(2);
});
it('mixed fields + files', async () => {
const r = await parseMultipart(body([field('name', 'joe'), file('avatar', 'p.png', 'image/png', 'PNG')]), B, opts);
expect(r.fields.name).toBe('joe');
expect((r.files.avatar as any).filename).toBe('p.png');
});
it('case-insensitive headers + filename-before-name (order independent)', async () => {
const part = `content-disposition: form-data; filename="z.bin"; name="up"\r\ncontent-type: application/octet-stream\r\n\r\nDATA`;
const r = await parseMultipart(body([part]), B, opts);
expect((r.files.up as any).filename).toBe('z.bin');
});
it('filename="" with empty body is skipped', async () => {
const part = `Content-Disposition: form-data; name="up"; filename=""\r\n\r\n`;
const r = await parseMultipart(body([part]), B, opts);
expect(r.files.up).toBeUndefined();
});
it('more parts than maxParts → rejects', async () => {
const b = body([field('a', '1'), field('b', '2'), field('c', '3')]);
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 () => {
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();
});
});