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 | import { afterEach, describe, expect, it, vi } from '@effect/vitest'; |
| 2 | import * as os from 'node:os'; |
| 3 | import * as path from 'node:path'; |
| 4 | import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 5 | import * as FileSystem from 'effect/FileSystem'; |
| 6 | import * as Path from 'effect/Path'; |
| 7 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 8 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 9 | import { Effect, Fiber, Layer } from 'effect'; |
| 10 | import { HttpClient } from 'effect/http'; |
| 11 | import { Auth, BadRequestError, make } from '@composio/client/effect'; |
| 12 | import { |
| 13 | ComposioBlockedInternalUrlError, |
| 14 | ComposioSensitiveFilePathBlockedError, |
| 15 | MAX_URL_UPLOAD_SIZE_BYTES, |
| 16 | } from '@composio/core'; |
| 17 | import { |
| 18 | readRunStagedFiles, |
| 19 | findFileUploadablePaths, |
| 20 | findLocalPathArguments, |
| 21 | schemaHasFileUploadable, |
| 22 | ToolFileUploadError, |
| 23 | uploadToolInputFiles, |
| 24 | } from 'src/services/tool-file-uploads'; |
| 25 | import { stubComposioClient } from 'test/__utils__/services/stub-composio-client'; |
| 26 | |
| 27 | // Schema with a single `file_uploadable` attachment field — mirrors tools like |
| 28 | // GMAIL_SEND_EMAIL whose attachment is uploaded from a local path. |
| 29 | const inputSchema = { |
| 30 | type: 'object', |
| 31 | properties: { |
| 32 | attachment: { file_uploadable: true, type: 'string' }, |
| 33 | }, |
| 34 | }; |
| 35 | |
| 36 | describe('file argument paths', () => { |
| 37 | const schema = { |
| 38 | type: 'object', |
| 39 | properties: { |
| 40 | operations: { |
| 41 | type: 'array', |
| 42 | items: { |
| 43 | oneOf: [ |
| 44 | { |
| 45 | properties: { type: { const: 'add_image' }, image: { file_uploadable: true } }, |
| 46 | required: ['type'], |
| 47 | }, |
| 48 | { |
| 49 | properties: { type: { const: 'replace_image' }, image: { file_uploadable: true } }, |
| 50 | required: ['type'], |
| 51 | }, |
| 52 | { |
| 53 | properties: { type: { const: 'set_text' }, text: { type: 'string' } }, |
| 54 | required: ['type'], |
| 55 | }, |
| 56 | ], |
| 57 | }, |
| 58 | }, |
| 59 | }, |
| 60 | }; |
| 61 | |
| 62 | it('binds the selected union branch to the existing array index', () => { |
| 63 | expect( |
| 64 | findFileUploadablePaths(schema, [], { |
| 65 | operations: [{ type: 'set_text', text: 'keep' }, { type: 'add_image' }], |
| 66 | }) |
| 67 | ).toEqual([['operations', 1, 'image']]); |
| 68 | }); |
| 69 | |
| 70 | it('does not invent array entries or choose between multiple image operations', () => { |
| 71 | expect(findFileUploadablePaths(schema, [], { operations: [] })).toEqual([]); |
| 72 | expect(findFileUploadablePaths(schema, [], { operations: [{ type: 'set_text' }] })).toEqual([]); |
| 73 | expect( |
| 74 | findFileUploadablePaths(schema, [], { |
| 75 | operations: [{ type: 'add_image' }, { type: 'replace_image' }], |
| 76 | }) |
| 77 | ).toEqual([ |
| 78 | ['operations', 0, 'image'], |
| 79 | ['operations', 1, 'image'], |
| 80 | ]); |
| 81 | }); |
| 82 | |
| 83 | it('preserves tuple indices, including preceding boolean schemas', () => { |
| 84 | expect( |
| 85 | findFileUploadablePaths( |
| 86 | { type: 'array', items: [true, { file_uploadable: true }] }, |
| 87 | [], |
| 88 | ['keep', undefined] |
| 89 | ) |
| 90 | ).toEqual([[1]]); |
| 91 | }); |
| 92 | |
| 93 | const optionalModeSchema = (required: ReadonlyArray<string>) => ({ |
| 94 | type: 'object', |
| 95 | allOf: [ |
| 96 | { |
| 97 | properties: { mode: { const: 'upload' }, file: { file_uploadable: true } }, |
| 98 | required, |
| 99 | }, |
| 100 | ], |
| 101 | }); |
| 102 | |
| 103 | it('keeps a branch whose optional const property the caller omitted', () => { |
| 104 | expect(findFileUploadablePaths(optionalModeSchema(['file']), [], {})).toEqual([['file']]); |
| 105 | }); |
| 106 | |
| 107 | it('drops a branch whose const property is required and omitted, or set to another value', () => { |
| 108 | expect(findFileUploadablePaths(optionalModeSchema(['mode', 'file']), [], {})).toEqual([]); |
| 109 | expect(findFileUploadablePaths(optionalModeSchema(['file']), [], { mode: 'link' })).toEqual([]); |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | // The client over a transport that answers the presigned-URL request with |
| 114 | // `presigned`, and records each request it receives. |
| 115 | const makeClient = ( |
| 116 | presigned: { readonly new_presigned_url: string; readonly key: string } = { |
| 117 | new_presigned_url: 'https://1.1.1.1/put', |
| 118 | key: 's3key-123', |
| 119 | } |
| 120 | ) => stubComposioClient(() => Response.json(presigned)); |
| 121 | |
| 122 | describe('readRunStagedFiles', () => { |
| 123 | it.effect('loads original upload names and MIME types from the manifest', () => { |
| 124 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-labels-')); |
| 125 | const file = path.join(root, 'files.json'); |
| 126 | writeFileSync( |
| 127 | file, |
| 128 | JSON.stringify({ |
| 129 | '/tmp/file-1': { name: 'deck', mimetype: 'application/pdf' }, |
| 130 | '/tmp/file-2': { name: 'notes.txt' }, |
| 131 | }) |
| 132 | ); |
| 133 | return Effect.gen(function* () { |
| 134 | const fsApi = yield* FileSystem.FileSystem; |
| 135 | expect(yield* readRunStagedFiles(fsApi, file)).toEqual( |
| 136 | new Map([ |
| 137 | ['/tmp/file-1', { name: 'deck', mimetype: 'application/pdf' }], |
| 138 | ['/tmp/file-2', { name: 'notes.txt' }], |
| 139 | ]) |
| 140 | ); |
| 141 | }).pipe( |
| 142 | Effect.provide(BunFileSystem.layer), |
| 143 | Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true }))) |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | it.effect.each(['not json', '["a"]', '{"/a":1}', '{"/a":{"mimetype":"text/plain"}}', undefined])( |
| 148 | 'rejects unreadable or invalid metadata (%s) instead of dropping labels', |
| 149 | content => { |
| 150 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-labels-')); |
| 151 | const file = path.join(root, 'files.json'); |
| 152 | if (content !== undefined) writeFileSync(file, content); |
| 153 | return Effect.gen(function* () { |
| 154 | const fsApi = yield* FileSystem.FileSystem; |
| 155 | const failure = yield* Effect.flip(readRunStagedFiles(fsApi, file)); |
| 156 | expect(failure).toBeInstanceOf(ToolFileUploadError); |
| 157 | expect(failure.reason).toBe('source-read'); |
| 158 | }).pipe( |
| 159 | Effect.provide(BunFileSystem.layer), |
| 160 | Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true }))) |
| 161 | ); |
| 162 | } |
| 163 | ); |
| 164 | }); |
| 165 | |
| 166 | describe('uploadToolInputFiles — sensitive-path guard (issue #3746)', () => { |
| 167 | afterEach(() => { |
| 168 | vi.unstubAllGlobals(); |
| 169 | vi.restoreAllMocks(); |
| 170 | }); |
| 171 | |
| 172 | it('ignores malformed composition branches without hiding valid schemas', () => { |
| 173 | expect( |
| 174 | schemaHasFileUploadable({ |
| 175 | anyOf: ['not-a-schema', null, { file_uploadable: true }], |
| 176 | }) |
| 177 | ).toBe(true); |
| 178 | expect(schemaHasFileUploadable({ anyOf: ['not-a-schema', null] })).toBe(false); |
| 179 | }); |
| 180 | |
| 181 | it.effect('refuses to read/upload a path under a sensitive directory (~/.ssh/id_rsa)', () => |
| 182 | Effect.gen(function* () { |
| 183 | const fsApi = yield* FileSystem.FileSystem; |
| 184 | const pathApi = yield* Path.Path; |
| 185 | const { client, requests } = makeClient(); |
| 186 | |
| 187 | const failure = yield* Effect.flip( |
| 188 | uploadToolInputFiles({ |
| 189 | fs: fsApi, |
| 190 | path: pathApi, |
| 191 | toolSlug: 'GMAIL_SEND_EMAIL', |
| 192 | arguments_: { attachment: path.join(os.homedir(), '.ssh', 'id_rsa') }, |
| 193 | inputSchema, |
| 194 | client, |
| 195 | }) |
| 196 | ); |
| 197 | |
| 198 | expect(failure).toBeInstanceOf(ToolFileUploadError); |
| 199 | expect(failure).toMatchObject({ reason: 'source-read' }); |
| 200 | expect(failure.cause).toBeInstanceOf(ComposioSensitiveFilePathBlockedError); |
| 201 | // The guard must fire BEFORE any network round-trip: no presigned URL, no upload. |
| 202 | expect(requests).toEqual([]); |
| 203 | }).pipe(Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer))) |
| 204 | ); |
| 205 | |
| 206 | it.effect( |
| 207 | 'surfaces CLI-appropriate remediation, not the SDK-only opt-out, in the block error', |
| 208 | () => |
| 209 | Effect.gen(function* () { |
| 210 | const fsApi = yield* FileSystem.FileSystem; |
| 211 | const pathApi = yield* Path.Path; |
| 212 | const { client } = makeClient(); |
| 213 | |
| 214 | // The CLI has no `sensitiveFileUploadProtection` opt-out (by design), so the |
| 215 | // error must not point users at that non-existent knob (issue #3763 review #2/#3). |
| 216 | const failure = yield* Effect.flip( |
| 217 | uploadToolInputFiles({ |
| 218 | fs: fsApi, |
| 219 | path: pathApi, |
| 220 | toolSlug: 'GMAIL_SEND_EMAIL', |
| 221 | arguments_: { attachment: path.join(os.homedir(), '.ssh', 'id_rsa') }, |
| 222 | inputSchema, |
| 223 | client, |
| 224 | }) |
| 225 | ); |
| 226 | |
| 227 | expect(failure.message).toMatch(/has no opt-out/i); |
| 228 | expect(failure.message).not.toMatch(/sensitiveFileUploadProtection/); |
| 229 | }).pipe(Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer))) |
| 230 | ); |
| 231 | |
| 232 | it.effect('refuses credential-like basenames (.env) even outside a sensitive directory', () => |
| 233 | Effect.gen(function* () { |
| 234 | const fsApi = yield* FileSystem.FileSystem; |
| 235 | const pathApi = yield* Path.Path; |
| 236 | const { client, requests } = makeClient(); |
| 237 | |
| 238 | const failure = yield* Effect.flip( |
| 239 | uploadToolInputFiles({ |
| 240 | fs: fsApi, |
| 241 | path: pathApi, |
| 242 | toolSlug: 'GMAIL_SEND_EMAIL', |
| 243 | arguments_: { attachment: path.join(os.tmpdir(), '.env') }, |
| 244 | inputSchema, |
| 245 | client, |
| 246 | }) |
| 247 | ); |
| 248 | |
| 249 | expect(failure.cause).toBeInstanceOf(ComposioSensitiveFilePathBlockedError); |
| 250 | expect(requests).toEqual([]); |
| 251 | }).pipe(Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer))) |
| 252 | ); |
| 253 | |
| 254 | it.effect('requests one presigned URL for a normal local file and uploads to it', () => { |
| 255 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-upload-')); |
| 256 | const file = path.join(root, 'document.pdf'); |
| 257 | writeFileSync(file, 'hello'); |
| 258 | |
| 259 | const { client, requests } = makeClient(); |
| 260 | const uploadFetch = vi.fn(async () => ({ ok: true, statusText: 'OK' })); |
| 261 | vi.stubGlobal('fetch', uploadFetch); |
| 262 | |
| 263 | return Effect.gen(function* () { |
| 264 | const fsApi = yield* FileSystem.FileSystem; |
| 265 | const pathApi = yield* Path.Path; |
| 266 | const result = yield* uploadToolInputFiles({ |
| 267 | fs: fsApi, |
| 268 | path: pathApi, |
| 269 | toolSlug: 'GMAIL_SEND_EMAIL', |
| 270 | arguments_: { attachment: file }, |
| 271 | inputSchema, |
| 272 | client, |
| 273 | }); |
| 274 | |
| 275 | expect(requests).toEqual([ |
| 276 | { |
| 277 | method: 'POST', |
| 278 | path: '/api/v3.1/files/upload/request', |
| 279 | body: { |
| 280 | filename: 'document.pdf', |
| 281 | mimetype: 'application/pdf', |
| 282 | md5: '5d41402abc4b2a76b9719d911017c592', |
| 283 | tool_slug: 'GMAIL_SEND_EMAIL', |
| 284 | toolkit_slug: 'gmail', |
| 285 | }, |
| 286 | }, |
| 287 | ]); |
| 288 | expect(uploadFetch).toHaveBeenCalledTimes(1); |
| 289 | expect(uploadFetch).toHaveBeenCalledWith( |
| 290 | 'https://1.1.1.1/put', |
| 291 | expect.objectContaining({ method: 'PUT' }) |
| 292 | ); |
| 293 | expect(result.attachment).toMatchObject({ s3key: 's3key-123', name: 'document.pdf' }); |
| 294 | }).pipe( |
| 295 | Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)), |
| 296 | Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true }))) |
| 297 | ); |
| 298 | }); |
| 299 | }); |
| 300 | |
| 301 | describe('uploadToolInputFiles — MIME type of a local file (issue #4984)', () => { |
| 302 | afterEach(() => { |
| 303 | vi.unstubAllGlobals(); |
| 304 | vi.restoreAllMocks(); |
| 305 | }); |
| 306 | |
| 307 | const PPTX = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; |
| 308 | const canvaSchema = { |
| 309 | type: 'object', |
| 310 | properties: { |
| 311 | file: { file_uploadable: true, type: 'object' }, |
| 312 | title: { type: 'string' }, |
| 313 | mime_type: { type: 'string' }, |
| 314 | }, |
| 315 | }; |
| 316 | |
| 317 | it.effect.each([ |
| 318 | // A file the caller did not stage takes the type of its extension. |
| 319 | { onDisk: 'deck.pptx', staged: undefined, name: 'deck.pptx', mimetype: PPTX }, |
| 320 | { onDisk: 'Deck.PPTX', staged: undefined, name: 'Deck.PPTX', mimetype: PPTX }, |
| 321 | { onDisk: 'picture.png', staged: undefined, name: 'picture.png', mimetype: 'image/png' }, |
| 322 | { onDisk: 'picture.JPG', staged: undefined, name: 'picture.JPG', mimetype: 'image/jpeg' }, |
| 323 | { |
| 324 | onDisk: 'document.pdf', |
| 325 | staged: undefined, |
| 326 | name: 'document.pdf', |
| 327 | mimetype: 'application/pdf', |
| 328 | }, |
| 329 | { |
| 330 | onDisk: 'document.docx', |
| 331 | staged: undefined, |
| 332 | name: 'document.docx', |
| 333 | mimetype: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 334 | }, |
| 335 | { |
| 336 | onDisk: 'unknown.extension-dogfood', |
| 337 | staged: undefined, |
| 338 | name: 'unknown.extension-dogfood', |
| 339 | mimetype: 'application/octet-stream', |
| 340 | }, |
| 341 | { |
| 342 | onDisk: 'no-extension', |
| 343 | staged: undefined, |
| 344 | name: 'no-extension', |
| 345 | mimetype: 'application/octet-stream', |
| 346 | }, |
| 347 | // What its caller staged it under (a `composio run` File's own name and type) |
| 348 | // wins over the path's. |
| 349 | { onDisk: 'file-a1', staged: { name: 'deck', mimetype: PPTX }, name: 'deck', mimetype: PPTX }, |
| 350 | { |
| 351 | onDisk: 'file-a1.pdf', |
| 352 | staged: { name: 'deck.pdf', mimetype: PPTX }, |
| 353 | name: 'deck.pdf', |
| 354 | mimetype: PPTX, |
| 355 | }, |
| 356 | { onDisk: 'file-a1.pptx', staged: { name: 'deck.pptx' }, name: 'deck.pptx', mimetype: PPTX }, |
| 357 | // Names the denylist refuses on a path are only labels here. |
| 358 | { |
| 359 | onDisk: 'file-a1.example', |
| 360 | staged: { name: '.env.example', mimetype: 'text/plain' }, |
| 361 | name: '.env.example', |
| 362 | mimetype: 'text/plain', |
| 363 | }, |
| 364 | { |
| 365 | onDisk: 'file-a1', |
| 366 | staged: { name: 'id_rsa' }, |
| 367 | name: 'id_rsa', |
| 368 | mimetype: 'application/octet-stream', |
| 369 | }, |
| 370 | ])( |
| 371 | '[Given] $onDisk staged as $staged [Then] the presign request, the PUT and the tool input carry $name and $mimetype', |
| 372 | ({ onDisk, staged, name, mimetype }) => { |
| 373 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-upload-')); |
| 374 | const file = path.join(root, onDisk); |
| 375 | writeFileSync(file, 'PK fake pptx'); |
| 376 | |
| 377 | const { client, requests } = makeClient(); |
| 378 | const uploadFetch = vi.fn(async (_url: string, _init: RequestInit) => ({ |
| 379 | ok: true, |
| 380 | statusText: 'OK', |
| 381 | })); |
| 382 | vi.stubGlobal('fetch', uploadFetch); |
| 383 | |
| 384 | return Effect.gen(function* () { |
| 385 | const fsApi = yield* FileSystem.FileSystem; |
| 386 | const pathApi = yield* Path.Path; |
| 387 | const result = yield* uploadToolInputFiles({ |
| 388 | fs: fsApi, |
| 389 | path: pathApi, |
| 390 | toolSlug: 'CANVA_CREATE_DESIGN_IMPORT_JOB', |
| 391 | arguments_: { file, title: 'Deck', mime_type: PPTX }, |
| 392 | inputSchema: canvaSchema, |
| 393 | client, |
| 394 | stagedFiles: new Map(staged === undefined ? [] : [[file, staged]]), |
| 395 | }); |
| 396 | |
| 397 | expect(requests).toMatchObject([ |
| 398 | { body: { filename: name, mimetype, toolkit_slug: 'canva' } }, |
| 399 | ]); |
| 400 | expect(uploadFetch.mock.calls[0]?.[1]?.headers).toMatchObject({ |
| 401 | 'Content-Type': mimetype, |
| 402 | }); |
| 403 | expect(result).toEqual({ |
| 404 | file: { name, mimetype, s3key: 's3key-123' }, |
| 405 | title: 'Deck', |
| 406 | mime_type: PPTX, |
| 407 | }); |
| 408 | }).pipe( |
| 409 | Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)), |
| 410 | Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true }))) |
| 411 | ); |
| 412 | } |
| 413 | ); |
| 414 | |
| 415 | it.effect( |
| 416 | '[Given] a sensitive path staged under a harmless name [Then] it is still refused', |
| 417 | () => |
| 418 | Effect.gen(function* () { |
| 419 | const fsApi = yield* FileSystem.FileSystem; |
| 420 | const pathApi = yield* Path.Path; |
| 421 | const { client, requests } = makeClient(); |
| 422 | const envFile = path.join(os.homedir(), '.env'); |
| 423 | |
| 424 | // The staged-file labels rename an upload; they never exempt a path from the denylist. |
| 425 | const failure = yield* Effect.flip( |
| 426 | uploadToolInputFiles({ |
| 427 | fs: fsApi, |
| 428 | path: pathApi, |
| 429 | toolSlug: 'CANVA_CREATE_DESIGN_IMPORT_JOB', |
| 430 | arguments_: { file: envFile, title: 'Deck' }, |
| 431 | inputSchema: canvaSchema, |
| 432 | client, |
| 433 | stagedFiles: new Map([[envFile, { name: 'deck.pptx', mimetype: PPTX }]]), |
| 434 | }) |
| 435 | ); |
| 436 | |
| 437 | expect(failure.cause).toBeInstanceOf(ComposioSensitiveFilePathBlockedError); |
| 438 | expect(requests).toEqual([]); |
| 439 | }).pipe(Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer))) |
| 440 | ); |
| 441 | }); |
| 442 | |
| 443 | describe('uploadToolInputFiles — presigned-URL request', () => { |
| 444 | afterEach(() => { |
| 445 | vi.unstubAllGlobals(); |
| 446 | vi.restoreAllMocks(); |
| 447 | }); |
| 448 | |
| 449 | it.effect('fails with the API failure, worded as before, when the request is refused', () => { |
| 450 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-upload-')); |
| 451 | const file = path.join(root, 'document.pdf'); |
| 452 | writeFileSync(file, 'hello'); |
| 453 | const body = { |
| 454 | error: { message: 'Unsupported mimetype', code: 1803, slug: 'File_Invalid', status: 400 }, |
| 455 | }; |
| 456 | const { client, requests } = stubComposioClient(() => Response.json(body, { status: 400 })); |
| 457 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 458 | |
| 459 | return Effect.gen(function* () { |
| 460 | const fsApi = yield* FileSystem.FileSystem; |
| 461 | const pathApi = yield* Path.Path; |
| 462 | |
| 463 | const failure = yield* Effect.flip( |
| 464 | uploadToolInputFiles({ |
| 465 | fs: fsApi, |
| 466 | path: pathApi, |
| 467 | toolSlug: 'GMAIL_SEND_EMAIL', |
| 468 | arguments_: { attachment: file }, |
| 469 | inputSchema, |
| 470 | client, |
| 471 | }) |
| 472 | ); |
| 473 | |
| 474 | expect(failure).toBeInstanceOf(BadRequestError); |
| 475 | expect(failure).toMatchObject({ |
| 476 | message: `400 ${JSON.stringify(body)}`, |
| 477 | status: 400, |
| 478 | details: body.error, |
| 479 | }); |
| 480 | expect(requests).toHaveLength(1); |
| 481 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 482 | }).pipe( |
| 483 | Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)), |
| 484 | Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true }))) |
| 485 | ); |
| 486 | }); |
| 487 | |
| 488 | it.effect('interrupting the caller interrupts the request', () => { |
| 489 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-upload-')); |
| 490 | const file = path.join(root, 'document.pdf'); |
| 491 | writeFileSync(file, 'hello'); |
| 492 | // A transport that never answers, reporting when the request reaches it |
| 493 | // and when it is interrupted. |
| 494 | const reached = Promise.withResolvers<void>(); |
| 495 | let interrupted = false; |
| 496 | const client = make( |
| 497 | { |
| 498 | auth: Auth.project('test'), |
| 499 | baseURL: 'https://stub.invalid', |
| 500 | }, |
| 501 | HttpClient.make(() => |
| 502 | Effect.sync(() => reached.resolve()).pipe( |
| 503 | Effect.andThen(Effect.never), |
| 504 | Effect.onInterrupt(() => |
| 505 | Effect.sync(() => { |
| 506 | interrupted = true; |
| 507 | }) |
| 508 | ) |
| 509 | ) |
| 510 | ) |
| 511 | ); |
| 512 | |
| 513 | return Effect.gen(function* () { |
| 514 | const fsApi = yield* FileSystem.FileSystem; |
| 515 | const pathApi = yield* Path.Path; |
| 516 | |
| 517 | const fiber = yield* Effect.forkChild( |
| 518 | uploadToolInputFiles({ |
| 519 | fs: fsApi, |
| 520 | path: pathApi, |
| 521 | toolSlug: 'GMAIL_SEND_EMAIL', |
| 522 | arguments_: { attachment: file }, |
| 523 | inputSchema, |
| 524 | client, |
| 525 | }) |
| 526 | ); |
| 527 | yield* Effect.promise(() => reached.promise); |
| 528 | yield* Fiber.interrupt(fiber); |
| 529 | |
| 530 | expect(interrupted).toBe(true); |
| 531 | }).pipe( |
| 532 | Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)), |
| 533 | Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true }))) |
| 534 | ); |
| 535 | }); |
| 536 | }); |
| 537 | |
| 538 | describe('uploadToolInputFiles — URL SSRF boundary', () => { |
| 539 | afterEach(() => { |
| 540 | vi.unstubAllGlobals(); |
| 541 | vi.restoreAllMocks(); |
| 542 | }); |
| 543 | |
| 544 | it.effect('rejects an unsafe source before requesting an upload destination', () => |
| 545 | Effect.gen(function* () { |
| 546 | const fsApi = yield* FileSystem.FileSystem; |
| 547 | const pathApi = yield* Path.Path; |
| 548 | const { client, requests } = makeClient(); |
| 549 | const sourceUrl = 'http://169.254.169.254/latest/meta-data/credentials'; |
| 550 | |
| 551 | const failure = yield* Effect.flip( |
| 552 | uploadToolInputFiles({ |
| 553 | fs: fsApi, |
| 554 | path: pathApi, |
| 555 | toolSlug: 'GMAIL_SEND_EMAIL', |
| 556 | arguments_: { attachment: sourceUrl }, |
| 557 | inputSchema, |
| 558 | client, |
| 559 | }) |
| 560 | ); |
| 561 | |
| 562 | expect(failure.cause).toBeInstanceOf(ComposioBlockedInternalUrlError); |
| 563 | expect(requests).toEqual([]); |
| 564 | }).pipe(Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer))) |
| 565 | ); |
| 566 | |
| 567 | it.effect('rejects an unsafe presigned destination before sending file bytes', () => { |
| 568 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-upload-')); |
| 569 | const file = path.join(root, 'document.pdf'); |
| 570 | writeFileSync(file, 'hello'); |
| 571 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 572 | const { client, requests } = makeClient({ |
| 573 | new_presigned_url: 'http://127.0.0.1:8080/upload', |
| 574 | key: 's3key-123', |
| 575 | }); |
| 576 | |
| 577 | return Effect.gen(function* () { |
| 578 | const fsApi = yield* FileSystem.FileSystem; |
| 579 | const pathApi = yield* Path.Path; |
| 580 | |
| 581 | const failure = yield* Effect.flip( |
| 582 | uploadToolInputFiles({ |
| 583 | fs: fsApi, |
| 584 | path: pathApi, |
| 585 | toolSlug: 'GMAIL_SEND_EMAIL', |
| 586 | arguments_: { attachment: file }, |
| 587 | inputSchema, |
| 588 | client, |
| 589 | }) |
| 590 | ); |
| 591 | |
| 592 | expect(failure.cause).toBeInstanceOf(ComposioBlockedInternalUrlError); |
| 593 | expect(requests).toHaveLength(1); |
| 594 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 595 | }).pipe( |
| 596 | Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)), |
| 597 | Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true }))) |
| 598 | ); |
| 599 | }); |
| 600 | }); |
| 601 | |
| 602 | describe('uploadToolInputFiles — URL download size cap', () => { |
| 603 | afterEach(() => { |
| 604 | vi.unstubAllGlobals(); |
| 605 | vi.restoreAllMocks(); |
| 606 | }); |
| 607 | |
| 608 | it.effect('rejects a remote file that declares a body above the cap before buffering it', () => { |
| 609 | const { client, requests } = makeClient(); |
| 610 | const oversized = String(MAX_URL_UPLOAD_SIZE_BYTES + 1); |
| 611 | vi.stubGlobal( |
| 612 | 'fetch', |
| 613 | vi.fn( |
| 614 | async () => |
| 615 | new Response('not the real body', { |
| 616 | status: 200, |
| 617 | headers: { 'content-length': oversized, 'content-type': 'application/octet-stream' }, |
| 618 | }) |
| 619 | ) |
| 620 | ); |
| 621 | |
| 622 | return Effect.gen(function* () { |
| 623 | const fsApi = yield* FileSystem.FileSystem; |
| 624 | const pathApi = yield* Path.Path; |
| 625 | |
| 626 | const failure = yield* Effect.flip( |
| 627 | uploadToolInputFiles({ |
| 628 | fs: fsApi, |
| 629 | path: pathApi, |
| 630 | toolSlug: 'GMAIL_SEND_EMAIL', |
| 631 | arguments_: { attachment: 'https://1.1.1.1/large.bin' }, |
| 632 | inputSchema, |
| 633 | client, |
| 634 | }) |
| 635 | ); |
| 636 | |
| 637 | expect(failure.message).toMatch(/exceeds maximum allowed size/); |
| 638 | expect(requests).toEqual([]); |
| 639 | }).pipe(Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer))); |
| 640 | }); |
| 641 | }); |
| 642 | |
| 643 | // TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD as the API serves it (issue #4965): |
| 644 | // `file` is described as a local file but is not `file_uploadable`. |
| 645 | const trelloAttachmentSchema = { |
| 646 | type: 'object', |
| 647 | required: ['idCard'], |
| 648 | properties: { |
| 649 | idCard: { type: 'string', description: 'ID or shortLink of the Trello card.' }, |
| 650 | url: { |
| 651 | type: 'string', |
| 652 | description: |
| 653 | "URL of an external resource to attach if not uploading a `file`. Must start with 'http://' or 'https://'.", |
| 654 | }, |
| 655 | file: { |
| 656 | type: 'string', |
| 657 | title: 'File', |
| 658 | description: 'Path to the local file to attach if not linking a `url`.', |
| 659 | }, |
| 660 | }, |
| 661 | }; |
| 662 | |
| 663 | describe('uploadToolInputFiles — local file for an input that is not file_uploadable (issue #4965)', () => { |
| 664 | const withTempFile = <A, E, R>( |
| 665 | use: (paths: { readonly root: string; readonly file: string }) => Effect.Effect<A, E, R> |
| 666 | ) => { |
| 667 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-upload-')); |
| 668 | const file = path.join(root, 'photo.jpg'); |
| 669 | writeFileSync(file, 'jpeg bytes'); |
| 670 | return use({ root, file }).pipe( |
| 671 | Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)), |
| 672 | Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true }))) |
| 673 | ); |
| 674 | }; |
| 675 | |
| 676 | const run = (arguments_: Record<string, unknown>, schema: Record<string, unknown>) => |
| 677 | Effect.gen(function* () { |
| 678 | const { client, requests } = makeClient(); |
| 679 | const result = yield* uploadToolInputFiles({ |
| 680 | fs: yield* FileSystem.FileSystem, |
| 681 | path: yield* Path.Path, |
| 682 | toolSlug: 'TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD', |
| 683 | arguments_, |
| 684 | inputSchema: schema, |
| 685 | client, |
| 686 | }).pipe(Effect.result); |
| 687 | return { result, requests }; |
| 688 | }); |
| 689 | |
| 690 | it.effect('fails before anything is sent when the argument names a local file', () => |
| 691 | withTempFile(({ file }) => |
| 692 | Effect.gen(function* () { |
| 693 | const { result, requests } = yield* run({ idCard: 'card', file }, trelloAttachmentSchema); |
| 694 | |
| 695 | const failure = yield* Effect.flip(Effect.fromResult(result)); |
| 696 | expect(failure).toBeInstanceOf(ToolFileUploadError); |
| 697 | expect(failure).toMatchObject({ reason: 'not-uploadable' }); |
| 698 | expect(failure.message).toContain(`"file" (${file})`); |
| 699 | expect(failure.message).toContain('Nothing was executed'); |
| 700 | expect(requests).toEqual([]); |
| 701 | }) |
| 702 | ) |
| 703 | ); |
| 704 | |
| 705 | it.effect('names every offending argument, including ones nested in arrays', () => |
| 706 | withTempFile(({ file }) => |
| 707 | Effect.gen(function* () { |
| 708 | const { result } = yield* run( |
| 709 | { files: [{ file_path: file }, { content: 'inline' }] }, |
| 710 | { |
| 711 | type: 'object', |
| 712 | properties: { |
| 713 | files: { |
| 714 | type: 'array', |
| 715 | items: { |
| 716 | type: 'object', |
| 717 | properties: { |
| 718 | file_path: { |
| 719 | type: 'string', |
| 720 | description: 'Local filesystem path to the file.', |
| 721 | }, |
| 722 | content: { type: 'string' }, |
| 723 | }, |
| 724 | }, |
| 725 | }, |
| 726 | }, |
| 727 | } |
| 728 | ); |
| 729 | |
| 730 | const failure = yield* Effect.flip(Effect.fromResult(result)); |
| 731 | expect(failure.message).toContain(`"files.0.file_path" (${file})`); |
| 732 | }) |
| 733 | ) |
| 734 | ); |
| 735 | |
| 736 | it.effect('passes a path that does not exist here through, as it may be remote', () => |
| 737 | withTempFile(({ root }) => |
| 738 | Effect.gen(function* () { |
| 739 | const remote = path.join(root, 'missing', 'report.pdf'); |
| 740 | const { result, requests } = yield* run( |
| 741 | { idCard: 'card', file: remote }, |
| 742 | trelloAttachmentSchema |
| 743 | ); |
| 744 | |
| 745 | expect(yield* Effect.fromResult(result)).toEqual({ idCard: 'card', file: remote }); |
| 746 | expect(requests).toEqual([]); |
| 747 | }) |
| 748 | ) |
| 749 | ); |
| 750 | |
| 751 | it.effect.each([ |
| 752 | { name: 'a directory', value: (root: string) => root }, |
| 753 | { name: 'a URL', value: () => 'https://example.com/photo.jpg' }, |
| 754 | { name: 'a bare word', value: () => 'photo' }, |
| 755 | ])('passes $name through', ({ value }) => |
| 756 | withTempFile(({ root }) => |
| 757 | Effect.gen(function* () { |
| 758 | const file = value(root); |
| 759 | const { result } = yield* run({ idCard: 'card', file }, trelloAttachmentSchema); |
| 760 | expect(yield* Effect.fromResult(result)).toEqual({ idCard: 'card', file }); |
| 761 | }) |
| 762 | ) |
| 763 | ); |
| 764 | |
| 765 | it.effect('leaves an existing file alone when the input is a repository path', () => |
| 766 | withTempFile(({ file }) => |
| 767 | Effect.gen(function* () { |
| 768 | const { result } = yield* run( |
| 769 | { path: file }, |
| 770 | { |
| 771 | type: 'object', |
| 772 | properties: { |
| 773 | path: { |
| 774 | type: 'string', |
| 775 | description: 'The full path to the file in the repository, including the filename.', |
| 776 | }, |
| 777 | }, |
| 778 | } |
| 779 | ); |
| 780 | |
| 781 | expect(yield* Effect.fromResult(result)).toEqual({ path: file }); |
| 782 | }) |
| 783 | ) |
| 784 | ); |
| 785 | |
| 786 | it('reads the description of an optional input from its non-null variant', () => { |
| 787 | expect( |
| 788 | findLocalPathArguments( |
| 789 | { file: '/tmp/photo.jpg' }, |
| 790 | { |
| 791 | type: 'object', |
| 792 | properties: { |
| 793 | file: { |
| 794 | anyOf: [ |
| 795 | { type: 'string', description: 'Local file path of the video to upload.' }, |
| 796 | { type: 'null' }, |
| 797 | ], |
| 798 | }, |
| 799 | }, |
| 800 | } |
| 801 | ) |
| 802 | ).toEqual([{ argumentPath: ['file'], value: '/tmp/photo.jpg' }]); |
| 803 | }); |
| 804 | |
| 805 | it('skips an input that also accepts an upload', () => { |
| 806 | expect( |
| 807 | findLocalPathArguments( |
| 808 | { file: '/tmp/photo.jpg' }, |
| 809 | { |
| 810 | type: 'object', |
| 811 | properties: { |
| 812 | file: { |
| 813 | description: 'Path to the local file to upload.', |
| 814 | anyOf: [{ file_uploadable: true }, { type: 'string' }], |
| 815 | }, |
| 816 | }, |
| 817 | } |
| 818 | ) |
| 819 | ).toEqual([]); |
| 820 | }); |
| 821 | |
| 822 | const localPathDescription = { type: 'string', description: 'Path to the local file to attach.' }; |
| 823 | |
| 824 | it.each([ |
| 825 | { |
| 826 | name: 'allOf splits the description and the upload marker across branches', |
| 827 | schema: { |
| 828 | type: 'object', |
| 829 | allOf: [ |
| 830 | { properties: { file: localPathDescription } }, |
| 831 | { properties: { file: { file_uploadable: true } } }, |
| 832 | ], |
| 833 | }, |
| 834 | }, |
| 835 | { |
| 836 | name: 'anyOf offers an uploadable branch beside the path branch', |
| 837 | schema: { |
| 838 | type: 'object', |
| 839 | properties: { file: { anyOf: [localPathDescription, { file_uploadable: true }] } }, |
| 840 | }, |
| 841 | }, |
| 842 | { |
| 843 | name: 'oneOf nests the uploadable branch one level down', |
| 844 | schema: { |
| 845 | type: 'object', |
| 846 | properties: { |
| 847 | file: { oneOf: [localPathDescription, { allOf: [{ file_uploadable: true }] }] }, |
| 848 | }, |
| 849 | }, |
| 850 | }, |
| 851 | ])('never flags an input the upload code uploads: $name', ({ schema }) => { |
| 852 | expect(findLocalPathArguments({ file: '/tmp/photo.jpg' }, schema)).toEqual([]); |
| 853 | }); |
| 854 | |
| 855 | it.effect('uploads, not refuses, a local file for an allOf-split uploadable input', () => |
| 856 | withTempFile(({ file }) => |
| 857 | Effect.gen(function* () { |
| 858 | vi.stubGlobal( |
| 859 | 'fetch', |
| 860 | vi.fn(async () => ({ ok: true, statusText: 'OK' })) |
| 861 | ); |
| 862 | const { result, requests } = yield* run( |
| 863 | { file }, |
| 864 | { |
| 865 | type: 'object', |
| 866 | allOf: [ |
| 867 | { properties: { file: localPathDescription } }, |
| 868 | { properties: { file: { file_uploadable: true } } }, |
| 869 | ], |
| 870 | } |
| 871 | ); |
| 872 | |
| 873 | expect(yield* Effect.fromResult(result)).toMatchObject({ |
| 874 | file: { name: 'photo.jpg', s3key: 's3key-123' }, |
| 875 | }); |
| 876 | expect(requests).toHaveLength(1); |
| 877 | }).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllGlobals()))) |
| 878 | ) |
| 879 | ); |
| 880 | |
| 881 | it.effect('uploads an input once when several branches mark it uploadable', () => |
| 882 | withTempFile(({ file }) => |
| 883 | Effect.gen(function* () { |
| 884 | vi.stubGlobal( |
| 885 | 'fetch', |
| 886 | vi.fn(async () => ({ ok: true, statusText: 'OK' })) |
| 887 | ); |
| 888 | const { result, requests } = yield* run( |
| 889 | { file, note: 'keep' }, |
| 890 | { |
| 891 | type: 'object', |
| 892 | anyOf: [ |
| 893 | { properties: { file: { file_uploadable: true } } }, |
| 894 | { properties: { file: { file_uploadable: true }, note: { type: 'string' } } }, |
| 895 | ], |
| 896 | } |
| 897 | ); |
| 898 | |
| 899 | expect(yield* Effect.fromResult(result)).toEqual({ |
| 900 | file: { name: 'photo.jpg', mimetype: 'image/jpeg', s3key: 's3key-123' }, |
| 901 | note: 'keep', |
| 902 | }); |
| 903 | expect(requests).toHaveLength(1); |
| 904 | }).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllGlobals()))) |
| 905 | ) |
| 906 | ); |
| 907 | |
| 908 | it('flags only the tuple elements the upload code does not upload', () => { |
| 909 | // `hydrateFileUploads` reads each tuple element against its own `items` schema. |
| 910 | expect( |
| 911 | findLocalPathArguments( |
| 912 | { files: ['/tmp/photo.jpg', '/tmp/other.jpg'] }, |
| 913 | { |
| 914 | type: 'object', |
| 915 | properties: { |
| 916 | files: { |
| 917 | type: 'array', |
| 918 | items: [localPathDescription, { ...localPathDescription, file_uploadable: true }], |
| 919 | }, |
| 920 | }, |
| 921 | } |
| 922 | ) |
| 923 | ).toEqual([{ argumentPath: ['files', 0], value: '/tmp/photo.jpg' }]); |
| 924 | }); |
| 925 | |
| 926 | it('reads descriptions in nested composition branches', () => { |
| 927 | expect( |
| 928 | findLocalPathArguments( |
| 929 | { file: '/tmp/photo.jpg' }, |
| 930 | { |
| 931 | type: 'object', |
| 932 | properties: { |
| 933 | file: { anyOf: [{ type: 'null' }, { allOf: [localPathDescription] }] }, |
| 934 | }, |
| 935 | } |
| 936 | ) |
| 937 | ).toEqual([{ argumentPath: ['file'], value: '/tmp/photo.jpg' }]); |
| 938 | }); |
| 939 | }); |
| 940 | |
| 941 | describe('multiple PowerPoint images', () => { |
| 942 | afterEach(() => { |
| 943 | vi.unstubAllGlobals(); |
| 944 | vi.restoreAllMocks(); |
| 945 | }); |
| 946 | |
| 947 | it.effect('keeps each nested image reference paired with its uploaded bytes', () => { |
| 948 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-multiple-images-')); |
| 949 | const first = path.join(root, 'first.png'); |
| 950 | const second = path.join(root, 'second.png'); |
| 951 | writeFileSync(first, 'first image bytes'); |
| 952 | writeFileSync(second, 'second image bytes'); |
| 953 | const { client, requests } = stubComposioClient(request => { |
| 954 | const { filename } = request.body as { filename: string }; |
| 955 | return Response.json({ |
| 956 | new_presigned_url: `https://1.1.1.1/${filename}`, |
| 957 | key: `uploads/${filename}`, |
| 958 | }); |
| 959 | }); |
| 960 | const uploaded = new Map<string, string>(); |
| 961 | vi.stubGlobal('fetch', async (url: string, init: RequestInit) => { |
| 962 | uploaded.set(url, Buffer.from(init.body as Uint8Array).toString()); |
| 963 | return new Response(null, { status: 200 }); |
| 964 | }); |
| 965 | const schema = { |
| 966 | type: 'object', |
| 967 | properties: { |
| 968 | slides: { |
| 969 | type: 'array', |
| 970 | items: { |
| 971 | type: 'object', |
| 972 | properties: { |
| 973 | blocks: { |
| 974 | type: 'array', |
| 975 | items: { |
| 976 | oneOf: [ |
| 977 | { properties: { type: { const: 'text' }, text: { type: 'string' } } }, |
| 978 | { properties: { type: { const: 'image' }, image: { file_uploadable: true } } }, |
| 979 | ], |
| 980 | }, |
| 981 | }, |
| 982 | }, |
| 983 | }, |
| 984 | }, |
| 985 | }, |
| 986 | }; |
| 987 | const arguments_ = { |
| 988 | filename: 'two-images.pptx', |
| 989 | slides: [ |
| 990 | { |
| 991 | blocks: [ |
| 992 | { type: 'text', text: 'Keep this' }, |
| 993 | { type: 'image', image: first }, |
| 994 | ], |
| 995 | }, |
| 996 | { blocks: [{ type: 'image', image: second }] }, |
| 997 | ], |
| 998 | }; |
| 999 | return Effect.gen(function* () { |
| 1000 | const fsApi = yield* FileSystem.FileSystem; |
| 1001 | const pathApi = yield* Path.Path; |
| 1002 | const result = yield* uploadToolInputFiles({ |
| 1003 | fs: fsApi, |
| 1004 | path: pathApi, |
| 1005 | toolSlug: 'MICROSOFT_POWERPOINT_CREATE_PRESENTATION', |
| 1006 | arguments_, |
| 1007 | inputSchema: schema, |
| 1008 | client, |
| 1009 | }); |
| 1010 | expect(requests).toHaveLength(2); |
| 1011 | expect(uploaded).toEqual( |
| 1012 | new Map([ |
| 1013 | ['https://1.1.1.1/first.png', 'first image bytes'], |
| 1014 | ['https://1.1.1.1/second.png', 'second image bytes'], |
| 1015 | ]) |
| 1016 | ); |
| 1017 | expect(result).toEqual({ |
| 1018 | filename: 'two-images.pptx', |
| 1019 | slides: [ |
| 1020 | { |
| 1021 | blocks: [ |
| 1022 | { type: 'text', text: 'Keep this' }, |
| 1023 | { |
| 1024 | type: 'image', |
| 1025 | image: { |
| 1026 | name: 'first.png', |
| 1027 | mimetype: 'image/png', |
| 1028 | s3key: 'uploads/first.png', |
| 1029 | }, |
| 1030 | }, |
| 1031 | ], |
| 1032 | }, |
| 1033 | { |
| 1034 | blocks: [ |
| 1035 | { |
| 1036 | type: 'image', |
| 1037 | image: { |
| 1038 | name: 'second.png', |
| 1039 | mimetype: 'image/png', |
| 1040 | s3key: 'uploads/second.png', |
| 1041 | }, |
| 1042 | }, |
| 1043 | ], |
| 1044 | }, |
| 1045 | ], |
| 1046 | }); |
| 1047 | expect(arguments_.slides[0]?.blocks[1]?.image).toBe(first); |
| 1048 | expect(arguments_.slides[1]?.blocks[0]?.image).toBe(second); |
| 1049 | }).pipe( |
| 1050 | Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)), |
| 1051 | Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true }))) |
| 1052 | ); |
| 1053 | }); |
| 1054 | }); |