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

next
ts 267 lines 9.72 KB
Raw
1 /**
2 * Behavior matrix for the file upload / download gating introduced with
3 * `fileUploadDirs` and `fileDownloadDir`. Exercises the interaction between:
4 *
5 * - `dangerouslyAllowAutoUploadDownloadFiles` (off | on)
6 * - Caller (automatic via `tools.execute` | manual via `composio.files.*`)
7 * - Direction (upload | download)
8 * - `fileUploadDirs` shape (`undefined`, `false`, `[]`, `string[]`)
9 *
10 * Runtime filesystem operations and HTTP are mocked; we only assert what the
11 * SDK tries to do and what errors it raises.
12 */
13
14 import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
15 import * as os from 'node:os';
16 import * as path from 'node:path';
17 import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
18 import ComposioClient from '@composio/client';
19
20 import { Tools } from '../../src/models/Tools';
21 import { Files } from '../../src/models/Files.node';
22 import * as fileUtilsModule from '../../src/utils/fileUtils.node';
23 import { mockClient } from '../utils/mocks/client.mock';
24 import { MockProvider } from '../utils/mocks/provider.mock';
25 import {
26 ComposioFileUploadPathNotAllowedError,
27 ComposioFileNotFoundError,
28 } from '../../src/errors/FileModifierErrors';
29 import { getDefaultUploadDir } from '../../src/utils/fileDirs';
30
31 // Mock network-bound helpers so we can assert inputs without hitting the API.
32 vi.mock('../../src/utils/fileUtils.node', async importOriginal => {
33 const actual = await importOriginal<typeof import('../../src/utils/fileUtils.node')>();
34 return {
35 ...actual,
36 downloadFileFromS3: vi.fn(),
37 };
38 });
39
40 const mkTemp = () => mkdtempSync(path.join(os.tmpdir(), 'composio-matrix-'));
41
42 const makeTools = (opts: {
43 dangerouslyAllowAutoUploadDownloadFiles?: boolean;
44 fileUploadDirs?: string[] | false;
45 fileDownloadDir?: string;
46 }) => {
47 return new Tools(mockClient as unknown as ComposioClient, {
48 provider: new MockProvider(),
49 ...opts,
50 });
51 };
52
53 describe('File handling matrix — Tools constructor resolves allowlist correctly', () => {
54 afterEach(() => vi.clearAllMocks());
55
56 describe('dangerouslyAllowAutoUploadDownloadFiles = false (default)', () => {
57 it('leaves fileUploadAllowlist undefined (auto-upload code path is never run)', () => {
58 const t = makeTools({});
59 // `fileUploadAllowlist` lives on the private options the class forwards
60 // to FileToolModifier. When the flag is off we leave it undefined so the
61 // modifier isn't even constructed.
62 expect(
63 (t as unknown as { fileUploadPathOptions: { fileUploadAllowlist?: string[] } })
64 .fileUploadPathOptions.fileUploadAllowlist
65 ).toBeUndefined();
66 });
67
68 it('ignores fileUploadDirs entirely (including `false`)', () => {
69 const t = makeTools({ fileUploadDirs: false });
70 expect(
71 (t as unknown as { fileUploadPathOptions: { fileUploadAllowlist?: string[] } })
72 .fileUploadPathOptions.fileUploadAllowlist
73 ).toBeUndefined();
74 });
75 });
76
77 describe('dangerouslyAllowAutoUploadDownloadFiles = true', () => {
78 it('defaults fileUploadAllowlist to [~/.composio/temp] when fileUploadDirs is unset', () => {
79 const t = makeTools({ dangerouslyAllowAutoUploadDownloadFiles: true });
80 const opts = (t as unknown as { fileUploadPathOptions: { fileUploadAllowlist?: string[] } })
81 .fileUploadPathOptions;
82 expect(opts.fileUploadAllowlist).toEqual([getDefaultUploadDir()]);
83 });
84
85 it('resolves to [] when fileUploadDirs is `false` (explicit "no local paths")', () => {
86 const t = makeTools({
87 dangerouslyAllowAutoUploadDownloadFiles: true,
88 fileUploadDirs: false,
89 });
90 const opts = (t as unknown as { fileUploadPathOptions: { fileUploadAllowlist?: string[] } })
91 .fileUploadPathOptions;
92 expect(opts.fileUploadAllowlist).toEqual([]);
93 });
94
95 it('resolves to [] when fileUploadDirs is `[]` (alias for `false`)', () => {
96 const t = makeTools({
97 dangerouslyAllowAutoUploadDownloadFiles: true,
98 fileUploadDirs: [],
99 });
100 const opts = (t as unknown as { fileUploadPathOptions: { fileUploadAllowlist?: string[] } })
101 .fileUploadPathOptions;
102 expect(opts.fileUploadAllowlist).toEqual([]);
103 });
104
105 it('uses user-provided dirs verbatim when fileUploadDirs is a non-empty array', () => {
106 const dir = mkTemp();
107 try {
108 const t = makeTools({
109 dangerouslyAllowAutoUploadDownloadFiles: true,
110 fileUploadDirs: [dir],
111 });
112 const opts = (t as unknown as { fileUploadPathOptions: { fileUploadAllowlist?: string[] } })
113 .fileUploadPathOptions;
114 expect(opts.fileUploadAllowlist).toEqual([path.resolve(dir)]);
115 } finally {
116 rmSync(dir, { recursive: true, force: true });
117 }
118 });
119 });
120 });
121
122 describe('File handling matrix — manual Files API bypasses the allowlist', () => {
123 const mkClient = () =>
124 ({
125 baseURL: 'https://api.composio.dev',
126 apiKey: 'test',
127 }) as unknown as ComposioClient;
128
129 beforeEach(() => vi.clearAllMocks());
130
131 it('composio.files.upload() accepts a local path that is NOT in any allowlist', async () => {
132 const tmp = mkTemp();
133 try {
134 const file = path.join(tmp, 'a.txt');
135 writeFileSync(file, 'x');
136
137 // Stub the actual S3 call so the manual upload can complete.
138 const stubbed = vi.spyOn(fileUtilsModule, 'getFileDataAfterUploadingToS3');
139 stubbed.mockResolvedValueOnce({ name: 'a.txt', mimetype: 'text/plain', s3key: 'k' });
140
141 const files = new Files(mkClient());
142 const result = await files.upload({
143 file,
144 toolSlug: 'T',
145 toolkitSlug: 'tk',
146 });
147
148 expect(result.s3key).toBe('k');
149
150 // The call must NOT include a `fileUploadAllowlist` — that's what keeps
151 // manual upload unconstrained.
152 const callArg = stubbed.mock.calls[0]?.[1] as { fileUploadAllowlist?: unknown };
153 expect(callArg.fileUploadAllowlist).toBeUndefined();
154 } finally {
155 rmSync(tmp, { recursive: true, force: true });
156 }
157 });
158
159 it('composio.files.download() works regardless of `dangerouslyAllowAutoUploadDownloadFiles`', async () => {
160 const downloadMock = vi.spyOn(fileUtilsModule, 'downloadFileFromS3');
161 downloadMock.mockResolvedValueOnce({
162 name: 'x.txt',
163 mimeType: 'text/plain',
164 s3Url: 'https://s3.example/x',
165 filePath: '/tmp/fake/x.txt',
166 });
167
168 const files = new Files(mkClient(), { fileDownloadDir: '/var/app/dl' });
169 const res = await files.download({
170 toolSlug: 'T',
171 s3Url: 'https://s3.example/x',
172 mimeType: 'text/plain',
173 });
174
175 expect(res.filePath).toBe('/tmp/fake/x.txt');
176 // The manual download path forwards the configured `fileDownloadDir`.
177 expect(downloadMock.mock.calls[0]?.[0]).toMatchObject({
178 fileDownloadDir: '/var/app/dl',
179 });
180 });
181 });
182
183 describe('File handling matrix — auto upload honors the resolved allowlist', () => {
184 const createPresignedURLSpy = vi.spyOn(mockClient.files, 'createPresignedURL');
185
186 beforeEach(() => {
187 vi.clearAllMocks();
188 createPresignedURLSpy.mockRejectedValue(new Error('stubbed-presigned-url-request'));
189 });
190
191 const callUpload = async (file: string, fileUploadAllowlist: string[] | undefined) => {
192 return fileUtilsModule.getFileDataAfterUploadingToS3(file, {
193 toolSlug: 'T',
194 toolkitSlug: 'tk',
195 client: mockClient as unknown as ComposioClient,
196 fileUploadAllowlist,
197 });
198 };
199
200 it('rejects local paths outside the allowlist with ComposioFileUploadPathNotAllowedError', async () => {
201 const allowed = mkTemp();
202 const outside = mkTemp();
203 try {
204 const file = path.join(outside, 'leak.txt');
205 writeFileSync(file, 'x');
206 await expect(callUpload(file, [allowed])).rejects.toBeInstanceOf(
207 ComposioFileUploadPathNotAllowedError
208 );
209 } finally {
210 rmSync(allowed, { recursive: true, force: true });
211 rmSync(outside, { recursive: true, force: true });
212 }
213 });
214
215 it('rejects ALL local paths when allowlist is [] (the `false` / fail-closed case)', async () => {
216 const tmp = mkTemp();
217 try {
218 const file = path.join(tmp, 'a.txt');
219 writeFileSync(file, 'x');
220 await expect(callUpload(file, [])).rejects.toBeInstanceOf(
221 ComposioFileUploadPathNotAllowedError
222 );
223 } finally {
224 rmSync(tmp, { recursive: true, force: true });
225 }
226 });
227
228 it('allows URLs even when allowlist is [] (URLs are never path-checked)', async () => {
229 // Stub global fetch so `readFile` (URL branch) doesn't hit the network.
230 const fetchSpy = vi
231 .spyOn(globalThis, 'fetch')
232 .mockRejectedValueOnce(new Error('stubbed-network'));
233
234 // Passes the allowlist gate (because the URL isn't a local path), then
235 // fails at the network layer. Any non-allowlist error proves the gate
236 // didn't reject the URL.
237 const http = 'https://example.com/file.pdf';
238 await expect(callUpload(http, [])).rejects.toThrowError('stubbed-network');
239 expect(fetchSpy).toHaveBeenCalledTimes(1);
240 });
241
242 it('raises ComposioFileNotFoundError when the file is missing under auto-upload', async () => {
243 const allowed = mkTemp();
244 try {
245 const missing = path.join(allowed, 'nope.txt');
246 await expect(callUpload(missing, [allowed])).rejects.toBeInstanceOf(
247 ComposioFileNotFoundError
248 );
249 } finally {
250 rmSync(allowed, { recursive: true, force: true });
251 }
252 });
253
254 it('does NOT throw an allowlist error when the file is inside an allowed dir', async () => {
255 const allowed = mkTemp();
256 try {
257 const file = path.join(allowed, 'ok.txt');
258 writeFileSync(file, 'x');
259 await expect(callUpload(file, [allowed])).rejects.toThrowError(
260 'stubbed-presigned-url-request'
261 );
262 expect(createPresignedURLSpy).toHaveBeenCalledTimes(1);
263 } finally {
264 rmSync(allowed, { recursive: true, force: true });
265 }
266 });
267 });