The fetch adapter read every body through request.arrayBuffer() and compared the length afterwards, so Deno, Bun and the edge buffered an oversized upload in full before refusing it. readFetchBytes now reads request.body chunk by chunk, stops at the chunk that crosses the limit and cancels the reader, so no further chunk is pulled. A content-length that already declares more than the limit is refused before anything is read, on both adapters. On Node the request used to wait for its first chunk, which a client that sends only headers never delivers. A malformed or absent header is ignored and the running count enforces the limit. The response is the same 413 through onError, and the route-over-server precedence for maxBodyBytes and maxParts is untouched; the new tests pin it. The over-read is proven with a counting stream rather than a status code, which is the same 413 either way. The stream uses a high-water mark of 0: the default of 1 pulls a chunk at construction, before any reader exists, which would make "nothing was read" unprovable. Socket-level tests on Deno, Bun and workerd use a 1000-byte route limit so every refused body fits in the socket buffer. With a multi-megabyte body the client is still writing when the 413 lands and sometimes sees ECONNRESET instead, which is the transport racing the answer. The Bun socket test also disables keep-alive: Bun's own fetch client reuses a socket after a response that arrived before the body was read, and its next request on it comes back 400 or 431. Node's fetch against the same Bun server does not. Closes #105 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
48 lines
1.9 KiB
TypeScript
48 lines
1.9 KiB
TypeScript
import type http from 'http';
|
|
import { HttpError } from '../signals';
|
|
import { declaresOverLimit } from './body';
|
|
|
|
/** Reads the full request body, enforcing the max-bytes ceiling (throws 413 if exceeded). */
|
|
export async function readBody(req: http.IncomingMessage, maxBodyBytes: number): Promise<Buffer | undefined> {
|
|
// A declared length over the limit is refused without waiting for the first chunk, which could
|
|
// otherwise sit until the request timeout. The fetch adapter makes the same check.
|
|
if (declaresOverLimit(req.headers['content-length'], maxBodyBytes)) throw new HttpError(413, 'Payload Too Large');
|
|
|
|
const chunks: Buffer[] = [];
|
|
let total = 0;
|
|
|
|
for await (const chunk of req) {
|
|
total += (chunk as Buffer).length;
|
|
if (total > maxBodyBytes) throw new HttpError(413, 'Payload Too Large');
|
|
chunks.push(chunk as Buffer);
|
|
}
|
|
|
|
return chunks.length === 0 ? undefined : Buffer.concat(chunks);
|
|
}
|
|
|
|
/** Returns the first comma-separated token of a header value (handles array-valued headers). */
|
|
function firstToken(value: string | string[] | undefined): string | undefined {
|
|
const first = Array.isArray(value) ? value[0] : value;
|
|
return first?.split(',')[0].trim();
|
|
}
|
|
|
|
/** Determines whether the request arrived over HTTPS, honoring `x-forwarded-proto` when proxy is trusted. */
|
|
export function deriveSecure(req: http.IncomingMessage, trustProxy: boolean): boolean {
|
|
if (trustProxy) {
|
|
const proto = firstToken(req.headers['x-forwarded-proto']);
|
|
if (proto) return proto === 'https';
|
|
}
|
|
|
|
return (req.socket as any).encrypted === true;
|
|
}
|
|
|
|
/** Resolves the client IP, honoring `x-forwarded-for` when proxy is trusted. */
|
|
export function deriveIp(req: http.IncomingMessage, trustProxy: boolean): string {
|
|
if (trustProxy) {
|
|
const forwarded = firstToken(req.headers['x-forwarded-for']);
|
|
if (forwarded) return forwarded;
|
|
}
|
|
|
|
return req.socket.remoteAddress ?? '';
|
|
}
|