(input: {
method: string;
contentLength: string | null;
maxBytes?: number;
})
| 28 | * this global cap. |
| 29 | */ |
| 30 | export const enforceBodyLimit = (input: { |
| 31 | method: string; |
| 32 | contentLength: string | null; |
| 33 | maxBytes?: number; |
| 34 | }): void => { |
| 35 | if (!BODIED_METHODS.has(input.method)) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | const cap = input.maxBytes ?? MAX_BODY_SIZE_BYTES; |
| 40 | |
| 41 | if (input.contentLength === null) { |
| 42 | throw ApiErrors.validation( |
| 43 | "Content-Length header is required for requests with a body", |
| 44 | "body" |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | const size = Number.parseInt(input.contentLength, 10); |
| 49 | |
| 50 | if (Number.isNaN(size)) { |
| 51 | throw ApiErrors.validation( |
| 52 | "Content-Length header must be a number", |
| 53 | "body" |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | if (size > cap) { |
| 58 | throw ApiErrors.validation("Request body exceeds 1 MB limit", "body"); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | export const bodyLimit = new Elysia().onParse(({ request }) => { |
| 63 | enforceBodyLimit({ |
no test coverage detected