Files
core/src/multipart.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

146 lines
5.6 KiB
TypeScript

// src/multipart.ts — multipart/form-data parsing on top of the optional `busboy` peer dependency.
import { Readable } from 'stream';
import { HttpError } from './signals';
import { nodeRequire } from './node-require';
/** A single uploaded file part decoded from a multipart body. */
export interface UploadedFile {
filename: string;
contentType: string;
data: Buffer;
size: number;
}
/** Decoded multipart body: text fields and file uploads, keyed by part name. */
export interface MultipartBody {
fields: Record<string, string | string[]>;
files: Record<string, UploadedFile | UploadedFile[]>;
}
/** Parser limits and duplicate-key policy for {@link parseMultipart}. */
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;
}
/**
* Extracts the boundary token from a Content-Type value (supports the quoted form).
* @returns The boundary, or undefined when absent.
*/
export function extractBoundary(contentType: string): string | undefined {
const match = /boundary=(?:"([^"]+)"|([^;]+))/i.exec(contentType);
const boundary = (match?.[1] ?? match?.[2])?.trim();
return boundary || undefined;
}
function addField(fields: MultipartBody['fields'], name: string, value: string, duplicates: 'array' | 'last'): void {
if (duplicates === 'array') {
const existing = fields[name];
if (Array.isArray(existing)) existing.push(value);
else fields[name] = [value];
} else {
fields[name] = value; // last-wins
}
}
/**
* Collapses an iterable of [key, value] pairs (e.g. URLSearchParams) into an object,
* applying the same duplicate-key policy as multipart fields so urlencoded bodies behave identically.
*/
export function collapseDuplicates(
pairs: Iterable<[string, string]>,
duplicates: 'array' | 'last',
): Record<string, string | string[]> {
const out: Record<string, string | string[]> = {};
for (const [key, value] of pairs) addField(out, key, value, duplicates);
return out;
}
function addFile(files: MultipartBody['files'], name: string, file: UploadedFile): void {
const existing = files[name];
if (existing === undefined) files[name] = file;
else if (Array.isArray(existing)) existing.push(file);
else files[name] = [existing, file];
}
/** Lazily loads the optional `busboy` peer dependency, or null when it is not installed. */
function loadBusboy(): ((cfg: unknown) => NodeJS.WritableStream & NodeJS.EventEmitter) | null {
try {
// `busboy` is an OPTIONAL peer dependency: only multipart uploads need it, so it is
// lazy-required and its absence surfaces as a clear 501 rather than a load-time crash.
return nodeRequire('busboy');
} catch {
return null;
}
}
/**
* Parses a raw multipart/form-data buffer into decoded fields and files, using `busboy`.
* @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> {
const busboy = loadBusboy();
if (!busboy) {
throw new HttpError(501, 'multipart/form-data requires the optional "busboy" peer dependency — run `npm i busboy`');
}
return new Promise<MultipartBody>((resolve, reject) => {
const body: MultipartBody = { fields: {}, files: {} };
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;
// 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, fieldSize },
});
} catch {
return fail();
}
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[] = [];
stream.on('data', (chunk: Buffer) => chunks.push(chunk));
stream.on('close', () => {
const data = Buffer.concat(chunks);
const filename = info.filename ?? '';
if (filename === '' && data.length === 0) return; // unselected file input
addFile(body.files, name, {
filename,
contentType: info.mimeType || 'application/octet-stream',
data,
size: data.length,
});
});
});
bb.on('partsLimit', fail); // more parts than maxParts → treat as malformed (matches prior behavior)
bb.on('error', fail);
bb.on('close', () => resolve(body));
Readable.from(raw).pipe(bb);
});
}