@composio/core 0.14.0 → 0.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/{BaseProvider-lCxnjFKQ.d.mts → BaseProvider-Dc5yKZhk.d.mts} +81 -82
  2. package/dist/{CustomTool-CmokRT9q.d.mts → CustomTool-4nVuwLzO.d.mts} +1 -2
  3. package/dist/{CustomTool-C_5NvBsX.mjs → CustomTool-l-j5HtEf.mjs} +1 -1
  4. package/dist/FileModifierErrors-B8V8_3VE.mjs +97 -0
  5. package/dist/{SsrfErrors-BQRozkeU.mjs → SsrfErrors-DNJt57GL.mjs} +5 -98
  6. package/dist/{Telemetry-llfvticE.mjs → Telemetry-CF0rNHu2.mjs} +2 -2
  7. package/dist/{cancellation-W65QRyj2.mjs → cancellation-BbJ-UQ__.mjs} +2 -2
  8. package/dist/{composio-6C8kHtqc.d.mts → composio-ChU3eD9F.d.mts} +260 -257
  9. package/dist/{customTool.types-kFhNYuzh.d.mts → customTool.types-CX2-LbfI.d.mts} +142 -109
  10. package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +2 -0
  11. package/dist/{errors-B165yJUQ.mjs → errors-BcEUHZbq.mjs} +2 -1
  12. package/dist/experimental/index.d.mts +2 -2
  13. package/dist/experimental/index.mjs +1 -1
  14. package/dist/{fileUtils.node-iPFi2o4i.mjs → fileUtils.node-DOkW4PBg.mjs} +5 -153
  15. package/dist/index.d.mts +1585 -753
  16. package/dist/index.mjs +14 -10
  17. package/dist/models/Files.node.d.mts +2 -11
  18. package/dist/models/Files.node.mjs +2 -2
  19. package/dist/models/Files.workerd.d.mts +0 -1
  20. package/dist/platform/node.d.mts +0 -1
  21. package/dist/platform/workerd.d.mts +0 -1
  22. package/dist/platform/workerd.mjs +27 -3
  23. package/dist/{sensitiveFileUploadPaths-B1OLsd8F.mjs → sensitiveFileUploadPaths-C43dHP7S.mjs} +52 -2
  24. package/dist/{types-yCrveqAj.d.mts → types-D5N60gUD.d.mts} +2 -3
  25. package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +1 -2
  26. package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +1 -2
  27. package/dist/utils/modifiers/FileToolModifier.node.d.mts +1 -2
  28. package/dist/utils/modifiers/FileToolModifier.node.mjs +5 -4
  29. package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -2
  30. package/dist/utils/ssrfGuard.node.d.mts +22 -0
  31. package/dist/utils/ssrfGuard.node.mjs +152 -0
  32. package/dist/utils/ssrfGuard.workerd.d.mts +9 -0
  33. package/dist/utils/ssrfGuard.workerd.mjs +17 -0
  34. package/package.json +14 -9
  35. package/src/errors/SsrfErrors.ts +4 -3
  36. package/src/models/ToolRouterSessionFileMount.ts +7 -3
  37. package/src/platform/workerd.ts +30 -3
  38. package/src/utils/fileUtils.node.ts +2 -2
  39. package/src/utils/readResponseBody.ts +63 -0
  40. package/src/utils/ssrfGuard.node.ts +6 -6
  41. package/src/utils/ssrfGuard.workerd.ts +19 -0
@@ -1,5 +1,32 @@
1
1
  import type { Platform, Uint8ArrayEncoding } from './types';
2
2
 
3
+ const SLASH_CHAR_CODE = 47; // '/'
4
+
5
+ /**
6
+ * Trailing-slash trim without a regex. The equivalent pattern backtracks
7
+ * quadratically on long runs of slashes (CodeQL js/polynomial-redos), so this
8
+ * walks the string from the end instead. Returns the input unchanged when
9
+ * there is nothing to trim, so the common case allocates nothing.
10
+ */
11
+ const trimTrailingSlashes = (value: string): string => {
12
+ let end = value.length;
13
+ while (end > 0 && value.charCodeAt(end - 1) === SLASH_CHAR_CODE) end--;
14
+ return end === value.length ? value : value.slice(0, end);
15
+ };
16
+
17
+ /**
18
+ * Leading-and-trailing slash trim without a regex, matching the semantics of
19
+ * the previous leading/trailing slash-trim replace for every input, including
20
+ * a string that is entirely slashes.
21
+ */
22
+ const trimSurroundingSlashes = (value: string): string => {
23
+ let start = 0;
24
+ let end = value.length;
25
+ while (start < end && value.charCodeAt(start) === SLASH_CHAR_CODE) start++;
26
+ while (end > start && value.charCodeAt(end - 1) === SLASH_CHAR_CODE) end--;
27
+ return start === 0 && end === value.length ? value : value.slice(start, end);
28
+ };
29
+
3
30
  /**
4
31
  * Cloudflare Workers / Edge runtime platform implementation.
5
32
  * Provides stub implementations for file system operations that are unavailable in edge runtimes.
@@ -18,9 +45,9 @@ export const platform: Platform = {
18
45
  return paths
19
46
  .map((segment, index) => {
20
47
  if (index === 0) {
21
- return segment.replace(/\/+$/, '');
48
+ return trimTrailingSlashes(segment);
22
49
  }
23
- return segment.replace(/^\/+|\/+$/g, '');
50
+ return trimSurroundingSlashes(segment);
24
51
  })
25
52
  .filter(Boolean)
26
53
  .join('/');
@@ -40,7 +67,7 @@ export const platform: Platform = {
40
67
 
41
68
  basename(filePath: string): string {
42
69
  // Simple basename extraction without Node.js path module
43
- const segments = filePath.replace(/\/+$/, '').split('/');
70
+ const segments = trimTrailingSlashes(filePath).split('/');
44
71
  return segments[segments.length - 1] || '';
45
72
  },
46
73
 
@@ -10,6 +10,7 @@ import type { FileDownloadData, FileUploadData } from '../types/files.types';
10
10
  import { assertSafeFileUploadPath } from './sensitiveFileUploadPaths';
11
11
  import { assertPathInsideUploadDirs } from './uploadDirAllowlist.node';
12
12
  import { ssrfSafeFetch } from './ssrfGuard.node';
13
+ import { readResponseBodyWithLimit } from './readResponseBody';
13
14
 
14
15
  /**
15
16
  * Options for {@link getFileDataAfterUploadingToS3} (S3 presigned upload from local path, URL, or File).
@@ -177,8 +178,7 @@ const readFileContentFromURL = async (
177
178
  if (!response.ok) {
178
179
  throw new Error(`Failed to fetch file: ${response.statusText}`);
179
180
  }
180
- const arrayBuffer = await response.arrayBuffer();
181
- const content = new Uint8Array(arrayBuffer);
181
+ const content = await readResponseBodyWithLimit(response);
182
182
  const mimeType = response.headers.get('content-type') || 'application/octet-stream';
183
183
 
184
184
  // Extract clean filename from URL, removing query parameters
@@ -0,0 +1,63 @@
1
+ /** Maximum size accepted for files fetched from user-supplied URLs (100 MiB). */
2
+ export const MAX_URL_UPLOAD_SIZE_BYTES = 100 * 1024 * 1024;
3
+
4
+ const sizeLimitError = (actualSize: number, maxBytes: number): Error =>
5
+ new Error(`File size (${actualSize} bytes) exceeds maximum allowed size (${maxBytes} bytes)`);
6
+
7
+ /**
8
+ * Read a fetch response without allowing an untrusted server to exhaust memory.
9
+ *
10
+ * `Content-Length` is checked as an early rejection, but the streamed byte
11
+ * count is authoritative because the header can be absent or dishonest.
12
+ */
13
+ export const readResponseBodyWithLimit = async (
14
+ response: Response,
15
+ maxBytes: number = MAX_URL_UPLOAD_SIZE_BYTES
16
+ ): Promise<Uint8Array> => {
17
+ if (!Number.isSafeInteger(maxBytes) || maxBytes < 0) {
18
+ throw new RangeError('maxBytes must be a non-negative safe integer');
19
+ }
20
+
21
+ const contentLength = response.headers.get('content-length')?.trim();
22
+ if (contentLength && /^\d+$/.test(contentLength)) {
23
+ const declaredSize = Number(contentLength);
24
+ if (!Number.isSafeInteger(declaredSize) || declaredSize > maxBytes) {
25
+ const error = sizeLimitError(declaredSize, maxBytes);
26
+ await response.body?.cancel(error).catch(() => undefined);
27
+ throw error;
28
+ }
29
+ }
30
+
31
+ if (!response.body) {
32
+ return new Uint8Array();
33
+ }
34
+
35
+ const reader = response.body.getReader();
36
+ const chunks: Uint8Array[] = [];
37
+ let totalBytes = 0;
38
+
39
+ try {
40
+ while (true) {
41
+ const { done, value } = await reader.read();
42
+ if (done) break;
43
+ if (!value || value.byteLength === 0) continue;
44
+
45
+ totalBytes += value.byteLength;
46
+ if (totalBytes > maxBytes) {
47
+ await reader.cancel(sizeLimitError(totalBytes, maxBytes));
48
+ throw sizeLimitError(totalBytes, maxBytes);
49
+ }
50
+ chunks.push(value);
51
+ }
52
+ } finally {
53
+ reader.releaseLock();
54
+ }
55
+
56
+ const body = new Uint8Array(totalBytes);
57
+ let offset = 0;
58
+ for (const chunk of chunks) {
59
+ body.set(chunk, offset);
60
+ offset += chunk.byteLength;
61
+ }
62
+ return body;
63
+ };
@@ -5,12 +5,12 @@ import { ComposioBlockedInternalUrlError } from '../errors/SsrfErrors';
5
5
  /**
6
6
  * SSRF guard for user-supplied URL file inputs.
7
7
  *
8
- * `composio.files.upload(url)` and automatic file upload during tool execution
9
- * fetch arbitrary user-provided URLs. Without a guard, a caller (or a tool
10
- * argument produced by an LLM) can point the SDK at internal infrastructure —
11
- * loopback, RFC1918 ranges, link-local cloud-metadata endpoints
12
- * (`169.254.169.254`), or a public URL that 3xx-redirects into internal space —
13
- * turning the SDK into a server-side request forgery probe.
8
+ * `composio.files.upload(url)`, Tool Router session file uploads, and automatic
9
+ * file upload during tool execution fetch arbitrary user-provided URLs. Without
10
+ * a guard, a caller (or a tool argument produced by an LLM) can point the SDK at
11
+ * internal infrastructure — loopback, RFC1918 ranges, link-local cloud-metadata
12
+ * endpoints (`169.254.169.254`), or a public URL that 3xx-redirects into internal
13
+ * space — turning the SDK into a server-side request forgery probe.
14
14
  *
15
15
  * This module validates the *resolved* address (not just the hostname string,
16
16
  * which defeats decimal/octal/hex IP obfuscation) before every fetch, and
@@ -0,0 +1,19 @@
1
+ import { ComposioBlockedInternalUrlError } from '../errors/SsrfErrors';
2
+
3
+ /**
4
+ * Edge runtimes do not expose the DNS resolution APIs needed to prove that a
5
+ * hostname is publicly routable before connecting. Fail closed instead of
6
+ * falling back to an unguarded fetch.
7
+ */
8
+ export const ssrfSafeFetch = async (rawUrl: string): Promise<Response> => {
9
+ throw new ComposioBlockedInternalUrlError(
10
+ 'URL file uploads are not supported in edge runtimes because the destination cannot be safely validated',
11
+ {
12
+ url: rawUrl,
13
+ possibleFixes: [
14
+ 'Fetch the public URL yourself and pass a File or ArrayBuffer instead',
15
+ 'Run the URL upload in a Node.js or Bun runtime',
16
+ ],
17
+ }
18
+ );
19
+ };