Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** Maximum size accepted for files fetched from user-supplied URLs (100 MiB). */ |
| 2 | export const MAX_URL_UPLOAD_SIZE_BYTES = 100 * 1024 * 1024; |
| 3 | |
| 4 | const sizeLimitError = (actualSize: number, maxBytes: number): Error => |
| 5 | new Error(`File size (${actualSize} bytes) exceeds maximum allowed size (${maxBytes} bytes)`); |
| 6 | |
| 7 | /** |
| 8 | * Read a fetch response without allowing an untrusted server to exhaust memory. |
| 9 | * |
| 10 | * `Content-Length` is checked as an early rejection, but the streamed byte |
| 11 | * count is authoritative because the header can be absent or dishonest. |
| 12 | */ |
| 13 | export const readResponseBodyWithLimit = async ( |
| 14 | response: Response, |
| 15 | maxBytes: number = MAX_URL_UPLOAD_SIZE_BYTES |
| 16 | ): Promise<Uint8Array> => { |
| 17 | if (!Number.isSafeInteger(maxBytes) || maxBytes < 0) { |
| 18 | throw new RangeError('maxBytes must be a non-negative safe integer'); |
| 19 | } |
| 20 | |
| 21 | const contentLength = response.headers.get('content-length')?.trim(); |
| 22 | if (contentLength && /^\d+$/.test(contentLength)) { |
| 23 | const declaredSize = Number(contentLength); |
| 24 | if (!Number.isSafeInteger(declaredSize) || declaredSize > maxBytes) { |
| 25 | const error = sizeLimitError(declaredSize, maxBytes); |
| 26 | await response.body?.cancel(error).catch(() => undefined); |
| 27 | throw error; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | if (!response.body) { |
| 32 | return new Uint8Array(); |
| 33 | } |
| 34 | |
| 35 | const reader = response.body.getReader(); |
| 36 | const chunks: Uint8Array[] = []; |
| 37 | let totalBytes = 0; |
| 38 | |
| 39 | try { |
| 40 | while (true) { |
| 41 | const { done, value } = await reader.read(); |
| 42 | if (done) break; |
| 43 | if (!value || value.byteLength === 0) continue; |
| 44 | |
| 45 | totalBytes += value.byteLength; |
| 46 | if (totalBytes > maxBytes) { |
| 47 | await reader.cancel(sizeLimitError(totalBytes, maxBytes)); |
| 48 | throw sizeLimitError(totalBytes, maxBytes); |
| 49 | } |
| 50 | chunks.push(value); |
| 51 | } |
| 52 | } finally { |
| 53 | reader.releaseLock(); |
| 54 | } |
| 55 | |
| 56 | const body = new Uint8Array(totalBytes); |
| 57 | let offset = 0; |
| 58 | for (const chunk of chunks) { |
| 59 | body.set(chunk, offset); |
| 60 | offset += chunk.byteLength; |
| 61 | } |
| 62 | return body; |
| 63 | }; |