Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(core): cap automatic S3 download size at 100 MiB
jkomyno8a56383b24d2
diff --git a/.changeset/bound-download-size.md b/.changeset/bound-download-size.mdnew file mode 100644index 000000000..769d13155--- /dev/null+++ b/.changeset/bound-download-size.md@@ -0,0 +1,5 @@+---+'@composio/core': patch+---++Fix: automatic S3 file downloads are now capped at 100 MiB (configurable per call) to prevent memory exhaustion from oversized or streaming responses.diff --git a/ts/packages/core/src/utils/fileUtils.node.ts b/ts/packages/core/src/utils/fileUtils.node.tsindex c659d7c49..234a2959e 100644--- a/ts/packages/core/src/utils/fileUtils.node.ts+++ b/ts/packages/core/src/utils/fileUtils.node.ts@@ -340,6 +340,7 @@ export const downloadFileFromS3 = async ({mimeType,fileDownloadDir,signal,+ maxDownloadBytes,}: {toolSlug: string;s3Url: string;@@ -350,6 +351,11 @@ export const downloadFileFromS3 = async ({*/fileDownloadDir?: string;signal?: AbortSignal;+ /**+ * Maximum number of bytes to read from the response before rejecting.+ * Defaults to {@link MAX_URL_UPLOAD_SIZE_BYTES} (100 MiB).+ */+ maxDownloadBytes?: number;}): Promise<FileDownloadData> => {// SSRF guard: `s3Url` is a field of the tool-execution response, so it is no// more trusted than a user-supplied URL — and the bytes it returns are@@ -358,11 +364,13 @@ export const downloadFileFromS3 = async ({if (!response.ok) {throw new Error(`Failed to download file: ${response.statusText}`);}- const data = await response.arrayBuffer();+ // The response is attacker-influencable and streamed to disk, so the body is+ // read through the shared size guard rather than buffered wholesale.+ const data = await readResponseBodyWithLimit(response, maxDownloadBytes);const extension = getExtensionFromMimeType(mimeType);const fileName = generateTimestampedFilename(extension, `${toolSlug}_`);- const filePath = saveFile(fileName, new Uint8Array(data), {+ const filePath = saveFile(fileName, data, {isTempFile: fileDownloadDir === undefined,outputDir: fileDownloadDir,});diff --git a/ts/packages/core/test/utils/fileUtils.test.ts b/ts/packages/core/test/utils/fileUtils.test.tsindex e8445d1e8..a1e406db6 100644--- a/ts/packages/core/test/utils/fileUtils.test.ts+++ b/ts/packages/core/test/utils/fileUtils.test.ts@@ -1,5 +1,6 @@import { describe, it, expect, vi, beforeEach, afterEach, Mock } from 'vitest';import * as path from 'node:path';+import * as fs from 'fs';import { getFileDataAfterUploadingToS3, downloadFileFromS3 } from '../../src/utils/fileUtils.node';import ComposioClient from '@composio/client';import { ComposioSensitiveFilePathBlockedError } from '../../src/errors/FileModifierErrors';@@ -65,6 +66,10 @@ const fetchedFileResponse = (contentType: string = 'application/pdf') =>headers: { 'content-type': contentType },});+/** A small, well-formed S3 download response (no `content-length` header). */+const downloadedFileResponse = (body: Uint8Array = new Uint8Array(10)) =>+ new Response(body, { status: 200 });+describe('fileUtils', () => {let mockClient: ComposioClient;@@ -314,10 +319,7 @@ describe('fileUtils', () => {describe('downloadFileFromS3', () => {it('should generate filename with tool slug prefix', async () => {- mockFetch.mockResolvedValue({- ok: true,- arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),- });+ mockFetch.mockResolvedValue(downloadedFileResponse());const result = await downloadFileFromS3({toolSlug: 'github',@@ -329,10 +331,7 @@ describe('fileUtils', () => {});it('should handle MIME types with + in downloadFileFromS3', async () => {- mockFetch.mockResolvedValue({- ok: true,- arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),- });+ mockFetch.mockResolvedValue(downloadedFileResponse());const result = await downloadFileFromS3({toolSlug: 'api-tool',@@ -364,10 +363,7 @@ describe('fileUtils', () => {// If a future change started reflecting the s3Url path or query into// the local filename, an attacker-controlled CDN response could write// outside the download dir or stomp on existing files.- mockFetch.mockResolvedValue({- ok: true,- arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),- });+ mockFetch.mockResolvedValue(downloadedFileResponse());const result = await downloadFileFromS3({toolSlug: 'github',@@ -390,10 +386,7 @@ describe('fileUtils', () => {// could in theory smuggle path components into the saved filename.// `saveFile` defends against that by running the assembled filename// through `path.basename` before joining with the download dir.- mockFetch.mockResolvedValue({- ok: true,- arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),- });+ mockFetch.mockResolvedValue(downloadedFileResponse());const result = await downloadFileFromS3({toolSlug: 'AAA/../etc/passwd',@@ -409,6 +402,39 @@ describe('fileUtils', () => {expect(path.dirname(result.filePath as string)).not.toContain('etc');expect(path.basename(result.filePath as string)).toBe('passwd_1640995200000abc12345.txt');});++ it('rejects a stream that exceeds the download limit and writes nothing', async () => {+ // `s3Url` is an API-response field, so the body behind it is untrusted:+ // without a cap, a dishonest (or absent) Content-Length lets the server+ // stream unbounded bytes into the host process's heap and onto disk.+ mockFetch.mockResolvedValue(downloadedFileResponse(new Uint8Array(2048)));++ await expect(+ downloadFileFromS3({+ toolSlug: 'github',+ s3Url: 'https://s3.example.com/huge.bin',+ mimeType: 'text/plain',+ maxDownloadBytes: 1024,+ })+ ).rejects.toThrow(/exceeds maximum allowed size/);++ expect(fs.writeFileSync).not.toHaveBeenCalled();+ });++ it('downloads a body within the limit', async () => {+ const body = new Uint8Array(512).fill(7);+ mockFetch.mockResolvedValue(downloadedFileResponse(body));++ const result = await downloadFileFromS3({+ toolSlug: 'github',+ s3Url: 'https://s3.example.com/small.bin',+ mimeType: 'text/plain',+ maxDownloadBytes: 1024,+ });++ expect(result.filePath).toBeDefined();+ expect(fs.writeFileSync).toHaveBeenCalledWith(result.filePath, body);+ });});describe('File object handling', () => {