@devries-cameron20 / composio-1 / commits / 8a56383b2
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(core): cap automatic S3 download size at 100 MiB

`downloadFileFromS3` buffered the whole response with `arrayBuffer()`. The `s3Url` it fetches is a tool-execution response field — the same untrusted input the SSRF guard already defends against — so an oversized or endlessly streaming body could exhaust the host process's heap. Route the body through the existing `readResponseBodyWithLimit` guard, which pre-checks `Content-Length` and counts streamed bytes (the header can be absent or dishonest). The 100 MiB default matches the upload-from-URL sibling in the same module; `maxDownloadBytes` overrides it per call. Claude-Session: https://claude.ai/code/session_01K1hH9PMmd6KPKdkACX553z

jkomyno committed Aug 28, 2026 at 09:50 UTC 8a56383b24d2c4de601adbe31aef1b81a7822e9d
3 files changed +57 -18
.changeset/bound-download-size.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Fix: automatic S3 file downloads are now capped at 100 MiB (configurable per call) to prevent memory exhaustion from oversized or streaming responses.
ts/packages/core/src/utils/fileUtils.node.ts
+10 -2
@@ -340,6 +340,7 @@ export const downloadFileFromS3 = async ({
340 mimeType,
341 fileDownloadDir,
342 signal,
343 + maxDownloadBytes,
344 }: {
345 toolSlug: string;
346 s3Url: string;
@@ -350,6 +351,11 @@ export const downloadFileFromS3 = async ({
351 */
352 fileDownloadDir?: string;
353 signal?: AbortSignal;
354 + /**
355 + * Maximum number of bytes to read from the response before rejecting.
356 + * Defaults to {@link MAX_URL_UPLOAD_SIZE_BYTES} (100 MiB).
357 + */
358 + maxDownloadBytes?: number;
359 }): Promise<FileDownloadData> => {
360 // SSRF guard: `s3Url` is a field of the tool-execution response, so it is no
361 // more trusted than a user-supplied URL — and the bytes it returns are
@@ -358,11 +364,13 @@ export const downloadFileFromS3 = async ({
364 if (!response.ok) {
365 throw new Error(`Failed to download file: ${response.statusText}`);
366 }
361 - const data = await response.arrayBuffer();
367 + // The response is attacker-influencable and streamed to disk, so the body is
368 + // read through the shared size guard rather than buffered wholesale.
369 + const data = await readResponseBodyWithLimit(response, maxDownloadBytes);
370
371 const extension = getExtensionFromMimeType(mimeType);
372 const fileName = generateTimestampedFilename(extension, `${toolSlug}_`);
365 - const filePath = saveFile(fileName, new Uint8Array(data), {
373 + const filePath = saveFile(fileName, data, {
374 isTempFile: fileDownloadDir === undefined,
375 outputDir: fileDownloadDir,
376 });
ts/packages/core/test/utils/fileUtils.test.ts
+42 -16
@@ -1,5 +1,6 @@
1 import { describe, it, expect, vi, beforeEach, afterEach, Mock } from 'vitest';
2 import * as path from 'node:path';
3 +import * as fs from 'fs';
4 import { getFileDataAfterUploadingToS3, downloadFileFromS3 } from '../../src/utils/fileUtils.node';
5 import ComposioClient from '@composio/client';
6 import { ComposioSensitiveFilePathBlockedError } from '../../src/errors/FileModifierErrors';
@@ -65,6 +66,10 @@ const fetchedFileResponse = (contentType: string = 'application/pdf') =>
66 headers: { 'content-type': contentType },
67 });
68
69 +/** A small, well-formed S3 download response (no `content-length` header). */
70 +const downloadedFileResponse = (body: Uint8Array = new Uint8Array(10)) =>
71 + new Response(body, { status: 200 });
72 +
73 describe('fileUtils', () => {
74 let mockClient: ComposioClient;
75
@@ -314,10 +319,7 @@ describe('fileUtils', () => {
319
320 describe('downloadFileFromS3', () => {
321 it('should generate filename with tool slug prefix', async () => {
317 - mockFetch.mockResolvedValue({
318 - ok: true,
319 - arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
320 - });
322 + mockFetch.mockResolvedValue(downloadedFileResponse());
323
324 const result = await downloadFileFromS3({
325 toolSlug: 'github',
@@ -329,10 +331,7 @@ describe('fileUtils', () => {
331 });
332
333 it('should handle MIME types with + in downloadFileFromS3', async () => {
332 - mockFetch.mockResolvedValue({
333 - ok: true,
334 - arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
335 - });
334 + mockFetch.mockResolvedValue(downloadedFileResponse());
335
336 const result = await downloadFileFromS3({
337 toolSlug: 'api-tool',
@@ -364,10 +363,7 @@ describe('fileUtils', () => {
363 // If a future change started reflecting the s3Url path or query into
364 // the local filename, an attacker-controlled CDN response could write
365 // outside the download dir or stomp on existing files.
367 - mockFetch.mockResolvedValue({
368 - ok: true,
369 - arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
370 - });
366 + mockFetch.mockResolvedValue(downloadedFileResponse());
367
368 const result = await downloadFileFromS3({
369 toolSlug: 'github',
@@ -390,10 +386,7 @@ describe('fileUtils', () => {
386 // could in theory smuggle path components into the saved filename.
387 // `saveFile` defends against that by running the assembled filename
388 // through `path.basename` before joining with the download dir.
393 - mockFetch.mockResolvedValue({
394 - ok: true,
395 - arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
396 - });
389 + mockFetch.mockResolvedValue(downloadedFileResponse());
390
391 const result = await downloadFileFromS3({
392 toolSlug: 'AAA/../etc/passwd',
@@ -409,6 +402,39 @@ describe('fileUtils', () => {
402 expect(path.dirname(result.filePath as string)).not.toContain('etc');
403 expect(path.basename(result.filePath as string)).toBe('passwd_1640995200000abc12345.txt');
404 });
405 +
406 + it('rejects a stream that exceeds the download limit and writes nothing', async () => {
407 + // `s3Url` is an API-response field, so the body behind it is untrusted:
408 + // without a cap, a dishonest (or absent) Content-Length lets the server
409 + // stream unbounded bytes into the host process's heap and onto disk.
410 + mockFetch.mockResolvedValue(downloadedFileResponse(new Uint8Array(2048)));
411 +
412 + await expect(
413 + downloadFileFromS3({
414 + toolSlug: 'github',
415 + s3Url: 'https://s3.example.com/huge.bin',
416 + mimeType: 'text/plain',
417 + maxDownloadBytes: 1024,
418 + })
419 + ).rejects.toThrow(/exceeds maximum allowed size/);
420 +
421 + expect(fs.writeFileSync).not.toHaveBeenCalled();
422 + });
423 +
424 + it('downloads a body within the limit', async () => {
425 + const body = new Uint8Array(512).fill(7);
426 + mockFetch.mockResolvedValue(downloadedFileResponse(body));
427 +
428 + const result = await downloadFileFromS3({
429 + toolSlug: 'github',
430 + s3Url: 'https://s3.example.com/small.bin',
431 + mimeType: 'text/plain',
432 + maxDownloadBytes: 1024,
433 + });
434 +
435 + expect(result.filePath).toBeDefined();
436 + expect(fs.writeFileSync).toHaveBeenCalledWith(result.filePath, body);
437 + });
438 });
439
440 describe('File object handling', () => {