fix(http): enforce maxBodyBytes while reading fetch request bodies
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>
This commit is contained in:
12 files changed
+559
-26
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createApp, Provider, Route, Get, Post, Module, needs, body, ctx } from '../src/index';
|
||||
import { countingStream } from './helpers/counting-stream';
|
||||
|
||||
@Provider({ provides: 'store' })
|
||||
class Store {
|
||||
@@ -145,3 +146,104 @@ describe('app.fetch on a mesh app', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// #105: the limit is enforced while the body is read, not after it has been buffered. A status code
|
||||
// cannot tell the two apart — both answer 413 — so the counting stream is what these assert on.
|
||||
@Route('/limits')
|
||||
class LimitsCtl {
|
||||
@Post('/echo') echo(@body() b: any) {
|
||||
return { got: b };
|
||||
}
|
||||
@Post('/small', { maxBodyBytes: 10 }) small(@body() b: any) {
|
||||
return { got: b };
|
||||
}
|
||||
@Post('/large', { maxBodyBytes: 2_000_000 }) large(@body() b: any) {
|
||||
return { length: String(b).length };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [LimitsCtl] })
|
||||
class LimitsModule {}
|
||||
|
||||
const post = (path: string, body: RequestInit['body'], headers: Record<string, string> = {}) =>
|
||||
new Request(`http://x${path}`, { method: 'POST', body, headers, duplex: 'half' } as RequestInit);
|
||||
|
||||
describe('app.fetch bounded body reads (#105)', () => {
|
||||
const limitsApp = createApp({ modules: [LimitsModule] }); // server limit: the 1 MB default
|
||||
|
||||
it('T1: a declared content-length over the limit is refused without reading the body', async () => {
|
||||
const body = countingStream(10, 300_000);
|
||||
|
||||
const res = await limitsApp.fetch(post('/limits/echo', body.stream, { 'content-length': '2000000' }));
|
||||
|
||||
expect(res.status).toBe(413);
|
||||
expect(body.pulled).toBe(0);
|
||||
expect(body.cancelled).toBe(true);
|
||||
});
|
||||
|
||||
it('T3: a streamed body stops at the chunk that crosses the limit, and the reader is cancelled', async () => {
|
||||
const body = countingStream(10, 300_000); // 3 MB, no content-length
|
||||
|
||||
const res = await limitsApp.fetch(post('/limits/echo', body.stream));
|
||||
|
||||
expect(res.status).toBe(413);
|
||||
// 4 read, the 4th crossing 1 MB, plus at most one the stream queued ahead of the reader.
|
||||
expect(body.pulled).toBeLessThanOrEqual(5);
|
||||
expect(body.cancelled).toBe(true);
|
||||
});
|
||||
|
||||
it('T4: a body of exactly the limit is accepted', async () => {
|
||||
const json = JSON.stringify({ s: 'x'.repeat(1_000_000 - 8) });
|
||||
expect(Buffer.byteLength(json)).toBe(1_000_000);
|
||||
|
||||
const res = await limitsApp.fetch(post('/limits/echo', json, { 'content-type': 'application/json' }));
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(((await res.json()) as any).got.s).toHaveLength(1_000_000 - 8);
|
||||
});
|
||||
|
||||
it('T5: one byte over the limit is refused', async () => {
|
||||
const json = JSON.stringify({ s: 'x'.repeat(1_000_000 - 7) });
|
||||
|
||||
const res = await limitsApp.fetch(post('/limits/echo', json, { 'content-type': 'application/json' }));
|
||||
|
||||
expect(res.status).toBe(413);
|
||||
});
|
||||
|
||||
it('T5: one byte over the limit is refused when streamed without a content-length', async () => {
|
||||
const body = countingStream(1, 1_000_001);
|
||||
|
||||
const res = await limitsApp.fetch(post('/limits/echo', body.stream));
|
||||
|
||||
expect(res.status).toBe(413);
|
||||
});
|
||||
|
||||
it('T8: a route limit below the server limit wins', async () => {
|
||||
const res = await limitsApp.fetch(post('/limits/small', 'x'.repeat(11), { 'content-type': 'text/plain' }));
|
||||
|
||||
expect(res.status).toBe(413);
|
||||
});
|
||||
|
||||
it('T9: a route limit above the server limit wins', async () => {
|
||||
const res = await limitsApp.fetch(
|
||||
post('/limits/large', 'x'.repeat(1_500_000), { 'content-type': 'text/plain' }),
|
||||
);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.json()).toEqual({ length: 1_500_000 });
|
||||
});
|
||||
|
||||
it('a malformed content-length is ignored, and the stream count enforces the limit', async () => {
|
||||
const body = countingStream(1, 20);
|
||||
|
||||
const res = await limitsApp.fetch(post('/limits/small', body.stream, { 'content-length': 'nope' }));
|
||||
|
||||
expect(res.status).toBe(413);
|
||||
});
|
||||
|
||||
it('T10: a bodiless request is answered without reading anything', async () => {
|
||||
const res = await limitsApp.fetch(new Request('http://x/limits/echo', { method: 'POST' }));
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.json()).toEqual({});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user