1
-// This module's upload pipeline is plain async/await. The Effect caller
2
-// resolves the platform FileSystem and Path services and the Composio client
3
-// and passes the instances in as parameters; `runToPromise` below is the
4
-// single point where an effect (a FileSystem read, the presigned-URL request)
5
-// is run to completion inside this promise pipeline.
1
+// The upload pipeline runs in the caller's fiber: the FileSystem read and the
2
+// presigned-URL request are effects of their own, and the core SDK's
3
+// promise-based helpers (the path and URL guards, the body reader, the upload
4
+// transport) are wrapped at each call. The caller resolves the platform
5
+// services and the Composio client and passes the instances in as parameters.
6
import type * as FileSystem from 'effect/FileSystem';
7
import type * as Path from 'effect/Path';
8
-import type { ComposioClientShape } from '@composio/client/effect';
9
-import { Cause, Data, Effect, Exit, Predicate } from 'effect';
8
+import type { ComposioClientShape, ComposioError } from '@composio/client/effect';
9
+import { Data, Effect, Predicate } from 'effect';
10
import { guessToolkitFromToolSlug } from 'src/utils/toolkit-from-tool-slug';
11
12
type JsonSchema = Record<string, unknown>;
15
// a file input, so they are imported on that path rather than at module scope:
16
// `@composio/core`'s root entry evaluates the whole SDK, which every command
17
// would otherwise pay for at startup. The module registry memoizes the import.
18
-const loadCoreFileUploadHelpers = () => import('@composio/core');
19
-const loadSsrfSafeFetch = () => import('@composio/core/utils/ssrf-guard');
18
+const loadCoreFileUploadHelpers = Effect.promise(() => import('@composio/core'));
19
+const loadSsrfSafeFetch = Effect.promise(() => import('@composio/core/utils/ssrf-guard'));
20
21
export class ToolFileUploadError extends Data.TaggedError('services/ToolFileUploadError')<{
22
readonly cause?: unknown;
25
readonly status?: number;
26
}> {}
27
28
+/**
29
+ * Every failure of {@link uploadToolInputFiles}: reading or uploading a file,
30
+ * or the presigned-URL request, as the client's own error.
31
+ */
32
+export type ToolInputUploadError = ToolFileUploadError | ComposioError;
33
+
34
+// What a thrown value from the core SDK's helpers, or a platform error of the
35
+// local read, becomes: the file could not be read as a source. An error this
36
+// module raised itself passes through.
37
+const asSourceReadError = (cause: unknown): ToolFileUploadError =>
38
+ cause instanceof ToolFileUploadError
39
+ ? cause
40
+ : new ToolFileUploadError({
41
+ cause,
42
+ message: cause instanceof Error ? cause.message : String(cause),
43
+ reason: 'source-read',
44
+ });
45
+
46
+// Releases a body this module does not read, ignoring a cancellation failure.
47
+const discardBody = (response: Response) =>
48
+ Effect.ignore(Effect.tryPromise(() => response.body?.cancel() ?? Promise.resolve()));
49
+
50
const isFileLike = (value: unknown): value is File =>
51
typeof File !== 'undefined' && value instanceof File;
52
171
});
172
};
173
152
-const readFileFromUrl = async (path: Path.Path, url: string) => {
153
- const { ssrfSafeFetch } = await loadSsrfSafeFetch();
154
- const response = await ssrfSafeFetch(url);
155
- if (!response.ok) {
156
- await response.body?.cancel().catch(() => undefined);
157
- throw new ToolFileUploadError({
158
- message: `Failed to fetch file: ${response.statusText}`,
159
- reason: 'source-fetch',
160
- status: response.status,
174
+const readFileFromUrl = (path: Path.Path, url: string) =>
175
+ Effect.gen(function* () {
176
+ const { ssrfSafeFetch } = yield* loadSsrfSafeFetch;
177
+ const response = yield* Effect.tryPromise({
178
+ try: signal => ssrfSafeFetch(url, { signal }),
179
+ catch: asSourceReadError,
180
});
162
- }
163
-
164
- // Same cap as the core SDK's URL uploads: a remote server must not be able
165
- // to exhaust memory with an oversized or never-ending body. Copied into a
166
- // fresh ArrayBuffer-backed view for the same reason as readFileFromDisk.
167
- const { readResponseBodyWithLimit } = await loadCoreFileUploadHelpers();
168
- const bytes = new Uint8Array(await readResponseBodyWithLimit(response));
169
- const parsedUrl = new URL(url);
170
- const fileName = path.basename(parsedUrl.pathname) || `file-${Date.now()}`;
171
-
172
- return {
173
- bytes,
174
- fileName,
175
- mimeType: response.headers.get('content-type') || 'application/octet-stream',
176
- };
177
-};
181
+ if (!response.ok) {
182
+ yield* discardBody(response);
183
+ return yield* new ToolFileUploadError({
184
+ message: `Failed to fetch file: ${response.statusText}`,
185
+ reason: 'source-fetch',
186
+ status: response.status,
187
+ });
188
+ }
189
179
-// Runs an effect to completion for the plain-async pipeline above. A failure
180
-// rejects with the underlying error (the platform error of a read, the
181
-// client's error of a request), not a FiberFailure wrapper, matching this
182
-// module's throw-based error contract.
183
-const runToPromise = <A, E>(effect: Effect.Effect<A, E>): Promise<A> =>
184
- Effect.runPromiseExit(effect).then(exit =>
185
- Exit.match(exit, {
186
- onFailure: cause => Promise.reject<A>(Cause.squash(cause)),
187
- onSuccess: value => Promise.resolve(value),
188
- })
189
- );
190
+ // Same cap as the core SDK's URL uploads: a remote server must not be able
191
+ // to exhaust memory with an oversized or never-ending body. Copied into a
192
+ // fresh ArrayBuffer-backed view for the same reason as readFileFromDisk.
193
+ const { readResponseBodyWithLimit } = yield* loadCoreFileUploadHelpers;
194
+ const body = yield* Effect.tryPromise({
195
+ try: () => readResponseBodyWithLimit(response),
196
+ catch: asSourceReadError,
197
+ });
198
+ const bytes = new Uint8Array(body);
199
+ const parsedUrl = new URL(url);
200
+ const fileName = path.basename(parsedUrl.pathname) || `file-${Date.now()}`;
201
191
-const readLocalFileBytes = (fs: FileSystem.FileSystem, filePath: string): Promise<Uint8Array> =>
192
- runToPromise(fs.readFile(filePath));
193
-
194
-const readFileFromDisk = async (fs: FileSystem.FileSystem, path: Path.Path, filePath: string) => {
195
- // Enforce the sensitive-path denylist at the lowest-level local read, so any
196
- // caller of this reader (not just `uploadToolInputFiles`) is protected. This
197
- // is the single canonical guard shared with `@composio/core`; without it the
198
- // CLI's upload path would silently exfiltrate ~/.ssh/id_rsa, ~/.aws/credentials,
199
- // .env files, etc. (issue #3746 / GHSA-hp3h-89pf-5q58). URLs and File objects
200
- // are intentionally not path-checked, matching the core SDK.
201
- //
202
- // The CLI intentionally exposes NO opt-out for this guard (unlike the core/Python
203
- // SDKs' `sensitiveFileUploadProtection` flag): the primary attack vector is an
204
- // agent that has been prompt-injected into supplying its own tool arguments, so a
205
- // `--force`/env override would hand that attacker a trivial bypass. Pass a
206
- // CLI-appropriate remediation so the error does not advertise an SDK-only opt-out.
207
- const { assertSafeFileUploadPath } = await loadCoreFileUploadHelpers();
208
- assertSafeFileUploadPath(filePath, {
209
- remediation:
210
- 'The Composio CLI always enforces this denylist and has no opt-out. To upload this ' +
211
- 'file, copy it to a location outside sensitive directories (e.g. ~/.ssh, ~/.aws) and ' +
212
- 'pass the copy instead.',
202
+ return {
203
+ bytes,
204
+ fileName,
205
+ mimeType: response.headers.get('content-type') || 'application/octet-stream',
206
+ };
207
});
214
- return {
215
- // Copy into a fresh ArrayBuffer-backed view: `fetch` bodies reject the
216
- // wider ArrayBufferLike-backed Uint8Array that FileSystem returns.
217
- bytes: new Uint8Array(await readLocalFileBytes(fs, filePath)),
218
- fileName: path.basename(filePath),
219
- mimeType: 'application/octet-stream',
220
- };
221
-};
208
223
-const readUploadSource = async (
224
- fs: FileSystem.FileSystem,
225
- path: Path.Path,
226
- file: string | File
227
-) => {
228
- if (isFileLike(file)) {
209
+const readFileFromDisk = (fs: FileSystem.FileSystem, path: Path.Path, filePath: string) =>
210
+ Effect.gen(function* () {
211
+ // Enforce the sensitive-path denylist at the lowest-level local read, so any
212
+ // caller of this reader (not just `uploadToolInputFiles`) is protected. This
213
+ // is the single canonical guard shared with `@composio/core`; without it the
214
+ // CLI's upload path would silently exfiltrate ~/.ssh/id_rsa, ~/.aws/credentials,
215
+ // .env files, etc. (issue #3746 / GHSA-hp3h-89pf-5q58). URLs and File objects
216
+ // are intentionally not path-checked, matching the core SDK.
217
+ //
218
+ // The CLI intentionally exposes NO opt-out for this guard (unlike the core/Python
219
+ // SDKs' `sensitiveFileUploadProtection` flag): the primary attack vector is an
220
+ // agent that has been prompt-injected into supplying its own tool arguments, so a
221
+ // `--force`/env override would hand that attacker a trivial bypass. Pass a
222
+ // CLI-appropriate remediation so the error does not advertise an SDK-only opt-out.
223
+ const { assertSafeFileUploadPath } = yield* loadCoreFileUploadHelpers;
224
+ yield* Effect.try({
225
+ try: () =>
226
+ assertSafeFileUploadPath(filePath, {
227
+ remediation:
228
+ 'The Composio CLI always enforces this denylist and has no opt-out. To upload this ' +
229
+ 'file, copy it to a location outside sensitive directories (e.g. ~/.ssh, ~/.aws) and ' +
230
+ 'pass the copy instead.',
231
+ }),
232
+ catch: asSourceReadError,
233
+ });
234
+ const bytes = yield* fs.readFile(filePath).pipe(Effect.mapError(asSourceReadError));
235
return {
230
- bytes: new Uint8Array(await file.arrayBuffer()),
231
- fileName: file.name || `file-${Date.now()}`,
232
- mimeType: file.type || 'application/octet-stream',
236
+ // Copy into a fresh ArrayBuffer-backed view: `fetch` bodies reject the
237
+ // wider ArrayBufferLike-backed Uint8Array that FileSystem returns.
238
+ bytes: new Uint8Array(bytes),
239
+ fileName: path.basename(filePath),
240
+ mimeType: 'application/octet-stream',
241
};
242
+ });
243
+
244
+const readUploadSource = (fs: FileSystem.FileSystem, path: Path.Path, file: string | File) => {
245
+ if (isFileLike(file)) {
246
+ return Effect.tryPromise({ try: () => file.arrayBuffer(), catch: asSourceReadError }).pipe(
247
+ Effect.map(buffer => ({
248
+ bytes: new Uint8Array(buffer),
249
+ fileName: file.name || `file-${Date.now()}`,
250
+ mimeType: file.type || 'application/octet-stream',
251
+ }))
252
+ );
253
}
254
255
if (typeof file === 'string' && /^https?:\/\//i.test(file)) {
260
return readFileFromDisk(fs, path, file);
261
}
262
244
- throw new ToolFileUploadError({
245
- message: 'Unsupported upload source',
246
- reason: 'unsupported-source',
247
- });
263
+ return Effect.fail(
264
+ new ToolFileUploadError({
265
+ message: 'Unsupported upload source',
266
+ reason: 'unsupported-source',
267
+ })
268
+ );
269
};
270
250
-const uploadFile = async (params: {
271
+const uploadFile = (params: {
272
readonly fs: FileSystem.FileSystem;
273
readonly path: Path.Path;
274
readonly file: string | File;
275
readonly toolSlug: string;
276
readonly toolkitSlug: string;
277
readonly client: ComposioClientShape;
257
-}) => {
258
- const fileData = await readUploadSource(params.fs, params.path, params.file);
259
- // eslint-disable-next-line no-restricted-imports -- MD5 for the presigned-upload checksum is not available in Web Crypto
260
- const { createHash } = await import('node:crypto');
261
- const md5 = createHash('md5').update(fileData.bytes).digest('hex');
262
- const presigned = await runToPromise(
263
- params.client.files.createPresignedURL(
278
+}) =>
279
+ Effect.gen(function* () {
280
+ const fileData = yield* readUploadSource(params.fs, params.path, params.file);
281
+ // eslint-disable-next-line no-restricted-imports -- MD5 for the presigned-upload checksum is not available in Web Crypto
282
+ const { createHash } = yield* Effect.promise(() => import('node:crypto'));
283
+ const md5 = createHash('md5').update(fileData.bytes).digest('hex');
284
+ const presigned = yield* params.client.files.createPresignedURL(
285
{
286
filename: fileData.fileName,
287
mimetype: fileData.mimeType,
290
toolkit_slug: params.toolkitSlug,
291
},
292
{ decode: false }
272
- )
273
- );
274
-
275
- const { ssrfSafeFetch } = await loadSsrfSafeFetch();
276
- const uploadResponse = await ssrfSafeFetch(presigned.new_presigned_url, {
277
- method: 'PUT',
278
- body: fileData.bytes,
279
- headers: {
280
- 'Content-Type': fileData.mimeType,
281
- 'Content-Length': fileData.bytes.byteLength.toString(),
282
- },
283
- });
293
+ );
294
285
- if (!uploadResponse.ok) {
286
- await uploadResponse.body?.cancel().catch(() => undefined);
287
- throw new ToolFileUploadError({
288
- message: `Failed to upload file to S3: ${uploadResponse.statusText}`,
289
- reason: 'upload',
290
- status: uploadResponse.status,
295
+ const { ssrfSafeFetch } = yield* loadSsrfSafeFetch;
296
+ const uploadResponse = yield* Effect.tryPromise({
297
+ try: signal =>
298
+ ssrfSafeFetch(presigned.new_presigned_url, {
299
+ method: 'PUT',
300
+ body: fileData.bytes,
301
+ headers: {
302
+ 'Content-Type': fileData.mimeType,
303
+ 'Content-Length': fileData.bytes.byteLength.toString(),
304
+ },
305
+ signal,
306
+ }),
307
+ catch: asSourceReadError,
308
});
292
- }
293
- await uploadResponse.body?.cancel().catch(() => undefined);
309
295
- return {
296
- name: fileData.fileName,
297
- mimetype: fileData.mimeType,
298
- s3key: presigned.key,
299
- };
300
-};
310
+ yield* discardBody(uploadResponse);
311
+ if (!uploadResponse.ok) {
312
+ return yield* new ToolFileUploadError({
313
+ message: `Failed to upload file to S3: ${uploadResponse.statusText}`,
314
+ reason: 'upload',
315
+ status: uploadResponse.status,
316
+ });
317
+ }
318
302
-const hydrateFileUploads = async (
319
+ return {
320
+ name: fileData.fileName,
321
+ mimetype: fileData.mimeType,
322
+ s3key: presigned.key,
323
+ };
324
+ });
325
+
326
+const hydrateFileUploads = (
327
value: unknown,
328
schema: JsonSchema | undefined,
329
ctx: {
333
readonly toolkitSlug: string;
334
readonly client: ComposioClientShape;
335
}
312
-): Promise<unknown> => {
313
- if (schema?.file_uploadable === true) {
314
- if (typeof value !== 'string' && !isFileLike(value)) {
315
- return value;
316
- }
336
+): Effect.Effect<unknown, ToolInputUploadError> =>
337
+ Effect.gen(function* () {
338
+ if (schema?.file_uploadable === true) {
339
+ if (typeof value !== 'string' && !isFileLike(value)) {
340
+ return value;
341
+ }
342
318
- return uploadFile({
319
- fs: ctx.fs,
320
- path: ctx.path,
321
- file: value,
322
- toolSlug: ctx.toolSlug,
323
- toolkitSlug: ctx.toolkitSlug,
324
- client: ctx.client,
325
- });
326
- }
343
+ return yield* uploadFile({
344
+ fs: ctx.fs,
345
+ path: ctx.path,
346
+ file: value,
347
+ toolSlug: ctx.toolSlug,
348
+ toolkitSlug: ctx.toolkitSlug,
349
+ client: ctx.client,
350
+ });
351
+ }
352
328
- const uploadableVariants = getSchemaVariants(schema).filter(schemaHasFileUploadable);
329
- if (uploadableVariants.length > 0) {
330
- let nextValue = value;
331
- for (const variant of uploadableVariants) {
332
- nextValue = await hydrateFileUploads(nextValue, variant, ctx);
353
+ const uploadableVariants = getSchemaVariants(schema).filter(schemaHasFileUploadable);
354
+ if (uploadableVariants.length > 0) {
355
+ let nextValue = value;
356
+ for (const variant of uploadableVariants) {
357
+ nextValue = yield* hydrateFileUploads(nextValue, variant, ctx);
358
+ }
359
+ return nextValue;
360
}
334
- return nextValue;
335
- }
361
337
- if (isSchemaRecord(schema?.properties) && Predicate.isObject(value)) {
338
- const properties = schema.properties;
339
- const entries = await Promise.all(
340
- Object.entries(value).map(async ([key, entryValue]) => [
341
- key,
342
- await hydrateFileUploads(
343
- entryValue,
344
- isSchemaRecord(properties[key]) ? properties[key] : undefined,
345
- ctx
362
+ if (isSchemaRecord(schema?.properties) && Predicate.isObject(value)) {
363
+ const properties = schema.properties;
364
+ const entries = yield* Effect.all(
365
+ Object.entries(value).map(([key, entryValue]) =>
366
+ hydrateFileUploads(
367
+ entryValue,
368
+ isSchemaRecord(properties[key]) ? properties[key] : undefined,
369
+ ctx
370
+ ).pipe(Effect.map(hydrated => [key, hydrated] as const))
371
),
347
- ])
348
- );
349
- return Object.fromEntries(entries);
350
- }
372
+ { concurrency: 'unbounded' }
373
+ );
374
+ return Object.fromEntries(entries);
375
+ }
376
352
- if (schema?.type === 'array' && Array.isArray(value) && schema.items) {
353
- const itemSchema = Array.isArray(schema.items)
354
- ? schema.items.find(isSchemaRecord)
355
- : isSchemaRecord(schema.items)
356
- ? schema.items
357
- : undefined;
377
+ if (schema?.type === 'array' && Array.isArray(value) && schema.items) {
378
+ const itemSchema = Array.isArray(schema.items)
379
+ ? schema.items.find(isSchemaRecord)
380
+ : isSchemaRecord(schema.items)
381
+ ? schema.items
382
+ : undefined;
383
359
- return Promise.all(value.map(item => hydrateFileUploads(item, itemSchema, ctx)));
360
- }
384
+ return yield* Effect.all(
385
+ value.map(item => hydrateFileUploads(item, itemSchema, ctx)),
386
+ { concurrency: 'unbounded' }
387
+ );
388
+ }
389
362
- return value;
363
-};
390
+ return value;
391
+ });
392
365
-export const uploadToolInputFiles = async (params: {
393
+export const uploadToolInputFiles = (params: {
394
readonly fs: FileSystem.FileSystem;
395
readonly path: Path.Path;
396
readonly toolSlug: string;
398
readonly inputSchema: JsonSchema;
399
readonly client: ComposioClientShape;
400
readonly toolkitSlug?: string;
373
-}): Promise<Record<string, unknown>> => {
374
- if (!schemaHasFileUploadable(params.inputSchema)) {
375
- return params.arguments_;
376
- }
401
+}): Effect.Effect<Record<string, unknown>, ToolInputUploadError> =>
402
+ Effect.gen(function* () {
403
+ if (!schemaHasFileUploadable(params.inputSchema)) {
404
+ return params.arguments_;
405
+ }
406
378
- const hydrated = await hydrateFileUploads(params.arguments_, params.inputSchema, {
379
- fs: params.fs,
380
- path: params.path,
381
- toolSlug: params.toolSlug,
382
- toolkitSlug: params.toolkitSlug ?? guessToolkitFromToolSlug(params.toolSlug) ?? 'unknown',
383
- client: params.client,
384
- });
407
+ const hydrated = yield* hydrateFileUploads(params.arguments_, params.inputSchema, {
408
+ fs: params.fs,
409
+ path: params.path,
410
+ toolSlug: params.toolSlug,
411
+ toolkitSlug: params.toolkitSlug ?? guessToolkitFromToolSlug(params.toolSlug) ?? 'unknown',
412
+ client: params.client,
413
+ });
414
386
- return Predicate.isObject(hydrated) ? hydrated : params.arguments_;
387
-};
415
+ return Predicate.isObject(hydrated) ? hydrated : params.arguments_;
416
+ });