@composio/core 0.14.0 → 0.15.0
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.
- package/dist/{BaseProvider-lCxnjFKQ.d.mts → BaseProvider-BN2-Nktx.d.mts} +224 -180
- package/dist/{CustomTool-C_5NvBsX.mjs → CustomTool-l-j5HtEf.mjs} +1 -1
- package/dist/{CustomTool-CmokRT9q.d.mts → CustomTool-zjeDpZ91.d.mts} +1 -2
- package/dist/FileModifierErrors-B8V8_3VE.mjs +97 -0
- package/dist/{SsrfErrors-BQRozkeU.mjs → SsrfErrors-DNJt57GL.mjs} +5 -98
- package/dist/{Telemetry-llfvticE.mjs → Telemetry-DuGJSwUt.mjs} +3 -3
- package/dist/{cancellation-W65QRyj2.mjs → cancellation-BbJ-UQ__.mjs} +2 -2
- package/dist/{composio-6C8kHtqc.d.mts → composio-Bf8hnBBq.d.mts} +24 -21
- package/dist/{customTool.types-kFhNYuzh.d.mts → customTool.types-Bhom9a2R.d.mts} +73 -40
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +2 -0
- package/dist/{errors-B165yJUQ.mjs → errors-BcEUHZbq.mjs} +2 -1
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-iPFi2o4i.mjs → fileUtils.node-BQ0fWge3.mjs} +9 -154
- package/dist/index.d.mts +1585 -752
- package/dist/index.mjs +39 -20
- package/dist/models/Files.node.d.mts +2 -11
- package/dist/models/Files.node.mjs +2 -2
- package/dist/models/Files.workerd.d.mts +0 -1
- package/dist/platform/node.d.mts +1 -2
- package/dist/platform/node.mjs +24 -0
- package/dist/platform/workerd.d.mts +1 -2
- package/dist/platform/workerd.mjs +30 -3
- package/dist/{sensitiveFileUploadPaths-B1OLsd8F.mjs → sensitiveFileUploadPaths-BVEt0peg.mjs} +64 -9
- package/dist/{types-yCrveqAj.d.mts → types-CePuCfD-.d.mts} +2 -3
- package/dist/{types-DY1r-kN9.d.mts → types-D5A2wzBl.d.mts} +8 -0
- package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +1 -2
- package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +1 -2
- package/dist/utils/modifiers/FileToolModifier.node.d.mts +1 -2
- package/dist/utils/modifiers/FileToolModifier.node.mjs +5 -4
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -2
- package/dist/utils/ssrfGuard.node.d.mts +22 -0
- package/dist/utils/ssrfGuard.node.mjs +153 -0
- package/dist/utils/ssrfGuard.workerd.d.mts +9 -0
- package/dist/utils/ssrfGuard.workerd.mjs +17 -0
- package/package.json +17 -11
- package/src/errors/SsrfErrors.ts +4 -3
- package/src/models/ToolRouterSessionFileMount.ts +10 -3
- package/src/platform/node.ts +40 -0
- package/src/platform/types.ts +9 -0
- package/src/platform/workerd.ts +35 -3
- package/src/telemetry/redact.ts +6 -1
- package/src/types/tool.types.ts +52 -8
- package/src/utils/fileUtils.node.ts +5 -2
- package/src/utils/modifiers/FileToolModifier.node.ts +3 -3
- package/src/utils/modifiers/FileToolModifier.utils.neutral.ts +6 -4
- package/src/utils/readResponseBody.ts +63 -0
- package/src/utils/sdk.ts +2 -2
- package/src/utils/sensitiveFileUploadPaths.ts +12 -9
- package/src/utils/ssrfGuard.node.ts +12 -8
- package/src/utils/ssrfGuard.workerd.ts +19 -0
- package/src/utils/version.ts +21 -20
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { t as ComposioBlockedInternalUrlError } from "../SsrfErrors-DNJt57GL.mjs";
|
|
2
|
+
import { lookup } from "node:dns/promises";
|
|
3
|
+
import { isIP } from "node:net";
|
|
4
|
+
|
|
5
|
+
//#region src/utils/ssrfGuard.node.ts
|
|
6
|
+
/**
|
|
7
|
+
* SSRF guard for user-supplied URL file inputs.
|
|
8
|
+
*
|
|
9
|
+
* `composio.files.upload(url)`, Tool Router session file uploads, and automatic
|
|
10
|
+
* file upload during tool execution fetch arbitrary user-provided URLs. Without
|
|
11
|
+
* a guard, a caller (or a tool argument produced by an LLM) can point the SDK at
|
|
12
|
+
* internal infrastructure — loopback, RFC1918 ranges, link-local cloud-metadata
|
|
13
|
+
* endpoints (`169.254.169.254`), or a public URL that 3xx-redirects into internal
|
|
14
|
+
* space — turning the SDK into a server-side request forgery probe.
|
|
15
|
+
*
|
|
16
|
+
* This module validates the *resolved* address (not just the hostname string,
|
|
17
|
+
* which defeats decimal/octal/hex IP obfuscation) before every fetch, and
|
|
18
|
+
* follows redirects manually so each hop is re-validated.
|
|
19
|
+
*
|
|
20
|
+
* Known residual: a DNS name could resolve to a public address here and rebind
|
|
21
|
+
* to an internal one before the underlying `fetch` connects (a TOCTOU / DNS
|
|
22
|
+
* rebinding window). Closing that fully requires pinning the validated IP at
|
|
23
|
+
* connect time (a custom dispatcher), which is out of scope for this guard.
|
|
24
|
+
*/
|
|
25
|
+
const MAX_REDIRECTS = 5;
|
|
26
|
+
const ipv4ToLong = (ip) => {
|
|
27
|
+
const [a, b, c, d] = ip.split(".").map(Number);
|
|
28
|
+
return (a << 24 >>> 0 | b << 16 | c << 8 | d) >>> 0;
|
|
29
|
+
};
|
|
30
|
+
const IPV4_BLOCKED_CIDRS = [
|
|
31
|
+
["0.0.0.0", 8],
|
|
32
|
+
["10.0.0.0", 8],
|
|
33
|
+
["100.64.0.0", 10],
|
|
34
|
+
["127.0.0.0", 8],
|
|
35
|
+
["169.254.0.0", 16],
|
|
36
|
+
["172.16.0.0", 12],
|
|
37
|
+
["192.0.0.0", 24],
|
|
38
|
+
["192.0.2.0", 24],
|
|
39
|
+
["192.168.0.0", 16],
|
|
40
|
+
["198.18.0.0", 15],
|
|
41
|
+
["198.51.100.0", 24],
|
|
42
|
+
["203.0.113.0", 24],
|
|
43
|
+
["224.0.0.0", 4],
|
|
44
|
+
["240.0.0.0", 4]
|
|
45
|
+
];
|
|
46
|
+
const isBlockedIpv4Long = (long) => IPV4_BLOCKED_CIDRS.some(([base, bits]) => {
|
|
47
|
+
const mask = bits === 0 ? 0 : 4294967295 << 32 - bits >>> 0;
|
|
48
|
+
return (long & mask) === (ipv4ToLong(base) & mask);
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* Expand an IPv6 string into its 8 16-bit groups, converting any trailing
|
|
52
|
+
* dotted-quad (e.g. `::ffff:1.2.3.4`) into hex groups first. Returns `null` when
|
|
53
|
+
* the input cannot be parsed.
|
|
54
|
+
*/
|
|
55
|
+
const expandIpv6 = (ip) => {
|
|
56
|
+
let addr = ip;
|
|
57
|
+
const v4Match = addr.match(/^(.*:)(\d+\.\d+\.\d+\.\d+)$/);
|
|
58
|
+
if (v4Match) {
|
|
59
|
+
const long = ipv4ToLong(v4Match[2]);
|
|
60
|
+
addr = `${v4Match[1]}${(long >>> 16 & 65535).toString(16)}:${(long & 65535).toString(16)}`;
|
|
61
|
+
}
|
|
62
|
+
const halves = addr.split("::");
|
|
63
|
+
if (halves.length > 2) return null;
|
|
64
|
+
const head = halves[0] ? halves[0].split(":") : [];
|
|
65
|
+
const tail = halves.length === 2 ? halves[1] ? halves[1].split(":") : [] : [];
|
|
66
|
+
let groups;
|
|
67
|
+
if (halves.length === 2) {
|
|
68
|
+
const missing = 8 - head.length - tail.length;
|
|
69
|
+
if (missing < 0) return null;
|
|
70
|
+
groups = [
|
|
71
|
+
...head,
|
|
72
|
+
...Array(missing).fill("0"),
|
|
73
|
+
...tail
|
|
74
|
+
];
|
|
75
|
+
} else groups = head;
|
|
76
|
+
if (groups.length !== 8) return null;
|
|
77
|
+
return groups.map((g) => parseInt(g || "0", 16));
|
|
78
|
+
};
|
|
79
|
+
const isBlockedIpv6 = (ip) => {
|
|
80
|
+
const h = expandIpv6(ip);
|
|
81
|
+
if (!h) return true;
|
|
82
|
+
if (h.every((x) => x === 0)) return true;
|
|
83
|
+
if (h.slice(0, 7).every((x) => x === 0) && h[7] === 1) return true;
|
|
84
|
+
const isMappedOrCompat = h[0] === 0 && h[1] === 0 && h[2] === 0 && h[3] === 0 && h[4] === 0 && (h[5] === 65535 || h[5] === 0);
|
|
85
|
+
const isNat64 = h[0] === 100 && h[1] === 65435 && h[2] === 0 && h[3] === 0 && h[4] === 0 && h[5] === 0;
|
|
86
|
+
if (isMappedOrCompat || isNat64) return isBlockedIpv4Long((h[6] << 16 >>> 0 | h[7]) >>> 0);
|
|
87
|
+
if ((h[0] & 65024) === 64512) return true;
|
|
88
|
+
if ((h[0] & 65472) === 65152) return true;
|
|
89
|
+
if ((h[0] & 65280) === 65280) return true;
|
|
90
|
+
return false;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* True when `ip` is a private, loopback, link-local, or otherwise
|
|
94
|
+
* non-publicly-routable address. Fails closed (returns `true`) for anything that
|
|
95
|
+
* is not a valid IPv4/IPv6 literal.
|
|
96
|
+
*/
|
|
97
|
+
const isBlockedIp = (ip) => {
|
|
98
|
+
const family = isIP(ip);
|
|
99
|
+
if (family === 4) return isBlockedIpv4Long(ipv4ToLong(ip));
|
|
100
|
+
if (family === 6) return isBlockedIpv6(ip);
|
|
101
|
+
return true;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Validate a single URL: it must be http(s), its host must resolve, and every
|
|
105
|
+
* resolved address must be publicly routable. Throws
|
|
106
|
+
* {@link ComposioBlockedInternalUrlError} otherwise.
|
|
107
|
+
*/
|
|
108
|
+
const assertSafeFetchTarget = async (rawUrl) => {
|
|
109
|
+
let url;
|
|
110
|
+
try {
|
|
111
|
+
url = new URL(rawUrl);
|
|
112
|
+
} catch {
|
|
113
|
+
throw new ComposioBlockedInternalUrlError("Refusing to fetch a malformed URL", { url: rawUrl });
|
|
114
|
+
}
|
|
115
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") throw new ComposioBlockedInternalUrlError(`Refusing to fetch a non-http(s) URL (scheme "${url.protocol}")`, { url: rawUrl });
|
|
116
|
+
const host = url.hostname.replace(/^\[|\]$/g, "");
|
|
117
|
+
let resolved;
|
|
118
|
+
try {
|
|
119
|
+
resolved = await lookup(host, {
|
|
120
|
+
all: true,
|
|
121
|
+
verbatim: true
|
|
122
|
+
});
|
|
123
|
+
} catch {
|
|
124
|
+
throw new ComposioBlockedInternalUrlError(`Could not resolve host "${host}"`, { url: rawUrl });
|
|
125
|
+
}
|
|
126
|
+
for (const { address } of resolved) if (isBlockedIp(address)) throw new ComposioBlockedInternalUrlError(`Refusing to fetch "${host}" — it resolves to a private, loopback, or link-local address`, {
|
|
127
|
+
url: rawUrl,
|
|
128
|
+
resolvedIp: address
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Drop-in replacement for `fetch` that blocks SSRF. Validates the target before
|
|
133
|
+
* connecting and re-validates every redirect hop (redirects are followed
|
|
134
|
+
* manually up to {@link MAX_REDIRECTS}). Intermediate redirect bodies are
|
|
135
|
+
* cancelled; non-redirect responses are returned unchanged.
|
|
136
|
+
*/
|
|
137
|
+
const ssrfSafeFetch = async (rawUrl, init = {}, maxRedirects = MAX_REDIRECTS) => {
|
|
138
|
+
let currentUrl = rawUrl;
|
|
139
|
+
for (let hop = 0; hop <= maxRedirects; hop++) {
|
|
140
|
+
await assertSafeFetchTarget(currentUrl);
|
|
141
|
+
const response = await fetch(currentUrl, {
|
|
142
|
+
...init,
|
|
143
|
+
redirect: "manual"
|
|
144
|
+
});
|
|
145
|
+
if (!(response.status >= 300 && response.status < 400 && response.headers.has("location"))) return response;
|
|
146
|
+
await response.body?.cancel().catch(() => void 0);
|
|
147
|
+
currentUrl = new URL(response.headers.get("location"), currentUrl).toString();
|
|
148
|
+
}
|
|
149
|
+
throw new ComposioBlockedInternalUrlError(`Refusing to fetch: too many redirects (max ${maxRedirects})`, { url: rawUrl });
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
//#endregion
|
|
153
|
+
export { assertSafeFetchTarget, isBlockedIp, ssrfSafeFetch };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/utils/ssrfGuard.workerd.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Edge runtimes do not expose the DNS resolution APIs needed to prove that a
|
|
4
|
+
* hostname is publicly routable before connecting. Fail closed instead of
|
|
5
|
+
* falling back to an unguarded fetch.
|
|
6
|
+
*/
|
|
7
|
+
declare const ssrfSafeFetch: (rawUrl: string) => Promise<Response>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { ssrfSafeFetch };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { t as ComposioBlockedInternalUrlError } from "../SsrfErrors-DNJt57GL.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/ssrfGuard.workerd.ts
|
|
4
|
+
/**
|
|
5
|
+
* Edge runtimes do not expose the DNS resolution APIs needed to prove that a
|
|
6
|
+
* hostname is publicly routable before connecting. Fail closed instead of
|
|
7
|
+
* falling back to an unguarded fetch.
|
|
8
|
+
*/
|
|
9
|
+
const ssrfSafeFetch = async (rawUrl) => {
|
|
10
|
+
throw new ComposioBlockedInternalUrlError("URL file uploads are not supported in edge runtimes because the destination cannot be safely validated", {
|
|
11
|
+
url: rawUrl,
|
|
12
|
+
possibleFixes: ["Fetch the public URL yourself and pass a File or ArrayBuffer instead", "Run the URL upload in a Node.js or Bun runtime"]
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { ssrfSafeFetch };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composio/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.15.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "dist/index.mjs",
|
|
7
7
|
"publishConfig": {
|
|
@@ -31,6 +31,12 @@
|
|
|
31
31
|
"edge-light": "./dist/utils/config-defaults/ConfigDefaults.workerd.mjs",
|
|
32
32
|
"node": "./dist/utils/config-defaults/ConfigDefaults.node.mjs",
|
|
33
33
|
"default": "./dist/utils/config-defaults/ConfigDefaults.node.mjs"
|
|
34
|
+
},
|
|
35
|
+
"#ssrf_guard": {
|
|
36
|
+
"workerd": "./dist/utils/ssrfGuard.workerd.mjs",
|
|
37
|
+
"edge-light": "./dist/utils/ssrfGuard.workerd.mjs",
|
|
38
|
+
"node": "./dist/utils/ssrfGuard.node.mjs",
|
|
39
|
+
"default": "./dist/utils/ssrfGuard.node.mjs"
|
|
34
40
|
}
|
|
35
41
|
},
|
|
36
42
|
"exports": {
|
|
@@ -84,11 +90,11 @@
|
|
|
84
90
|
"license": "ISC",
|
|
85
91
|
"devDependencies": {
|
|
86
92
|
"@type-challenges/utils": "^0.1.1",
|
|
87
|
-
"@types/semver": "^7.
|
|
88
|
-
"tsdown": "^0.22.
|
|
89
|
-
"tsx": "^4.23.
|
|
93
|
+
"@types/semver": "^7.8.0",
|
|
94
|
+
"tsdown": "^0.22.14",
|
|
95
|
+
"tsx": "^4.23.5",
|
|
90
96
|
"typedoc": "^0.28.20",
|
|
91
|
-
"typescript": "
|
|
97
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
92
98
|
"vitest": "^4.1.10",
|
|
93
99
|
"zod": "^4.4.3"
|
|
94
100
|
},
|
|
@@ -96,14 +102,15 @@
|
|
|
96
102
|
"zod": ">=3.25.76 <5"
|
|
97
103
|
},
|
|
98
104
|
"dependencies": {
|
|
99
|
-
"@composio/client": "0.1.0-alpha.
|
|
105
|
+
"@composio/client": "0.1.0-alpha.76",
|
|
100
106
|
"@types/json-schema": "^7.0.15",
|
|
101
|
-
"
|
|
107
|
+
"is-fs-case-sensitive": "^2.0.0",
|
|
108
|
+
"openai": "^7.2.0",
|
|
102
109
|
"picocolors": "^1.1.1",
|
|
103
|
-
"pusher-js": "^8.
|
|
110
|
+
"pusher-js": "^8.6.0",
|
|
104
111
|
"semver": "^7.8.5",
|
|
105
112
|
"zod-to-json-schema": "^3.25.2",
|
|
106
|
-
"@composio/json-schema-to-zod": "0.2.
|
|
113
|
+
"@composio/json-schema-to-zod": "0.2.2"
|
|
107
114
|
},
|
|
108
115
|
"gitHead": "4fae6e54d5c150fba955cc5fa314281da5a1e064",
|
|
109
116
|
"repository": {
|
|
@@ -119,8 +126,7 @@
|
|
|
119
126
|
"clean": "git clean -xdf node_modules",
|
|
120
127
|
"build": "pnpm exec tsdown",
|
|
121
128
|
"test": "vitest run",
|
|
122
|
-
"typecheck": "
|
|
123
|
-
"typecheck:tsc": "tsc --noEmit --skipLibCheck && tsc --noEmit --skipLibCheck -p tsconfig.type-tests.json",
|
|
129
|
+
"typecheck": "tsc --noEmit --skipLibCheck && tsc --noEmit --skipLibCheck -p tsconfig.type-tests.json",
|
|
124
130
|
"generate:docs": "tsx scripts/generate-docs.ts"
|
|
125
131
|
},
|
|
126
132
|
"types": "dist/index.d.mts"
|
package/src/errors/SsrfErrors.ts
CHANGED
|
@@ -14,9 +14,10 @@ export interface ComposioBlockedInternalUrlErrorOptions extends Omit<ComposioErr
|
|
|
14
14
|
/**
|
|
15
15
|
* Thrown when a URL file input resolves to a private, loopback, link-local, or
|
|
16
16
|
* otherwise internal address, or points at a non-http(s) scheme. Guards against
|
|
17
|
-
* turning
|
|
18
|
-
* execution) into a server-side request forgery
|
|
19
|
-
* infrastructure such as cloud metadata endpoints
|
|
17
|
+
* turning URL-based file uploads (including Tool Router session files and
|
|
18
|
+
* automatic upload during tool execution) into a server-side request forgery
|
|
19
|
+
* (SSRF) probe of internal infrastructure such as cloud metadata endpoints
|
|
20
|
+
* (`169.254.169.254`).
|
|
20
21
|
*/
|
|
21
22
|
export class ComposioBlockedInternalUrlError extends ComposioError {
|
|
22
23
|
constructor(
|
|
@@ -18,7 +18,9 @@ import {
|
|
|
18
18
|
import { RemoteFile } from './RemoteFile';
|
|
19
19
|
import { ValidationError } from '../errors';
|
|
20
20
|
import { platform } from '#platform';
|
|
21
|
+
import { ssrfSafeFetch } from '#ssrf_guard';
|
|
21
22
|
import { getExtensionFromMimeType } from '../utils/mime';
|
|
23
|
+
import { readResponseBodyWithLimit } from '../utils/readResponseBody';
|
|
22
24
|
import { getRandomShortId } from '../utils/uuid';
|
|
23
25
|
|
|
24
26
|
/**
|
|
@@ -110,11 +112,14 @@ export class ToolRouterSessionFilesMount {
|
|
|
110
112
|
): Promise<{ fileToUpload: File; remotePath: string; mimetype: string }> {
|
|
111
113
|
if (typeof input === 'string') {
|
|
112
114
|
if (input.startsWith('http://') || input.startsWith('https://')) {
|
|
113
|
-
const response = await
|
|
115
|
+
const response = await ssrfSafeFetch(input);
|
|
114
116
|
if (!response.ok) {
|
|
117
|
+
// The error path never reads the body, so release it explicitly (mirrors
|
|
118
|
+
// `readResponseBodyWithLimit`) instead of leaving it to the garbage collector.
|
|
119
|
+
await response.body?.cancel().catch(() => undefined);
|
|
115
120
|
throw new Error(`Failed to fetch file from URL: ${response.statusText}`);
|
|
116
121
|
}
|
|
117
|
-
const
|
|
122
|
+
const content = await readResponseBodyWithLimit(response);
|
|
118
123
|
const rawContentType = response.headers.get('content-type') || 'application/octet-stream';
|
|
119
124
|
const mimeType = rawContentType.split(';')[0].trim();
|
|
120
125
|
const url = new URL(input);
|
|
@@ -125,7 +130,7 @@ export class ToolRouterSessionFilesMount {
|
|
|
125
130
|
const extension = getExtensionFromMimeType(mimeType);
|
|
126
131
|
filename = `${getRandomShortId()}.${extension}`;
|
|
127
132
|
}
|
|
128
|
-
const file = new File([
|
|
133
|
+
const file = new File([content as BlobPart], filename, { type: mimeType });
|
|
129
134
|
return {
|
|
130
135
|
fileToUpload: file,
|
|
131
136
|
remotePath: options.remotePath ?? filename,
|
|
@@ -178,6 +183,8 @@ export class ToolRouterSessionFilesMount {
|
|
|
178
183
|
*
|
|
179
184
|
* Accepts a file path (local or URL), a native File object, or a raw buffer.
|
|
180
185
|
* The file is stored in the virtual filesystem associated with the tool router session.
|
|
186
|
+
* URL inputs require a Node.js or Bun runtime so the destination can be DNS-validated;
|
|
187
|
+
* edge runtimes must fetch the file themselves and pass a File or ArrayBuffer.
|
|
181
188
|
*
|
|
182
189
|
* @param input - File path (string), native File, or raw buffer (ArrayBuffer | Uint8Array).
|
|
183
190
|
* @param options - Optional configuration. When passing a buffer, remotePath is required.
|
package/src/platform/node.ts
CHANGED
|
@@ -1,8 +1,37 @@
|
|
|
1
1
|
import * as fs from 'node:fs';
|
|
2
2
|
import * as os from 'node:os';
|
|
3
3
|
import * as path from 'node:path';
|
|
4
|
+
import { isFsCaseSensitive } from 'is-fs-case-sensitive';
|
|
4
5
|
import type { Platform, Uint8ArrayEncoding } from './types';
|
|
5
6
|
|
|
7
|
+
const findNearestExistingDirectory = (filePath: string): string => {
|
|
8
|
+
let candidate = path.resolve(filePath);
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
if (!fs.statSync(candidate).isDirectory()) {
|
|
12
|
+
candidate = path.dirname(candidate);
|
|
13
|
+
}
|
|
14
|
+
} catch {
|
|
15
|
+
candidate = path.dirname(candidate);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
while (true) {
|
|
19
|
+
try {
|
|
20
|
+
if (fs.statSync(candidate).isDirectory()) {
|
|
21
|
+
return candidate;
|
|
22
|
+
}
|
|
23
|
+
} catch {
|
|
24
|
+
// Walk toward the filesystem root until an existing directory is found.
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const parent = path.dirname(candidate);
|
|
28
|
+
if (parent === candidate) {
|
|
29
|
+
return candidate;
|
|
30
|
+
}
|
|
31
|
+
candidate = parent;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
6
35
|
/**
|
|
7
36
|
* Node.js platform implementation.
|
|
8
37
|
* Provides full file system and OS operations using Node.js built-in modules.
|
|
@@ -42,6 +71,17 @@ export const platform = {
|
|
|
42
71
|
return fs.realpathSync(filePath);
|
|
43
72
|
},
|
|
44
73
|
|
|
74
|
+
isFileSystemCaseSensitive(filePath: string): boolean {
|
|
75
|
+
try {
|
|
76
|
+
return isFsCaseSensitive(findNearestExistingDirectory(filePath));
|
|
77
|
+
} catch {
|
|
78
|
+
// Detection must not weaken this security guard. If the target mount
|
|
79
|
+
// cannot be inspected, compare case-insensitively and accept possible
|
|
80
|
+
// false positives instead of allowing a casing bypass.
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
|
|
45
85
|
mkdirSync(dirPath: string): void {
|
|
46
86
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
47
87
|
},
|
package/src/platform/types.ts
CHANGED
|
@@ -52,6 +52,15 @@ export interface Platform {
|
|
|
52
52
|
*/
|
|
53
53
|
realpathSync(filePath: string): string;
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Returns whether paths on the filesystem containing `filePath` are
|
|
57
|
+
* case-sensitive. Node checks the nearest existing directory so the result
|
|
58
|
+
* follows the target mount rather than the operating-system default.
|
|
59
|
+
* Runtimes without a filesystem return true to preserve exact path matching.
|
|
60
|
+
* @param filePath - A file or directory on the filesystem to inspect.
|
|
61
|
+
*/
|
|
62
|
+
isFileSystemCaseSensitive?(filePath: string): boolean;
|
|
63
|
+
|
|
55
64
|
/**
|
|
56
65
|
* Creates a directory at the given path, including parent directories if needed.
|
|
57
66
|
* @param dirPath - The directory path to create.
|
package/src/platform/workerd.ts
CHANGED
|
@@ -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
|
|
48
|
+
return trimTrailingSlashes(segment);
|
|
22
49
|
}
|
|
23
|
-
return segment
|
|
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
|
|
70
|
+
const segments = trimTrailingSlashes(filePath).split('/');
|
|
44
71
|
return segments[segments.length - 1] || '';
|
|
45
72
|
},
|
|
46
73
|
|
|
@@ -55,6 +82,11 @@ export const platform: Platform = {
|
|
|
55
82
|
return filePath;
|
|
56
83
|
},
|
|
57
84
|
|
|
85
|
+
isFileSystemCaseSensitive(_filePath: string): boolean {
|
|
86
|
+
// Preserve exact matching when there is no filesystem to inspect.
|
|
87
|
+
return true;
|
|
88
|
+
},
|
|
89
|
+
|
|
58
90
|
mkdirSync(_dirPath: string): void {
|
|
59
91
|
// No-op in edge runtimes - directories cannot be created
|
|
60
92
|
},
|
package/src/telemetry/redact.ts
CHANGED
|
@@ -20,8 +20,13 @@ const REDACTION_RULES: ReadonlyArray<readonly [RegExp, string]> = [
|
|
|
20
20
|
// Authorization scheme + credential: `Bearer <token>`, `Basic <token>`.
|
|
21
21
|
[/\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]+/gi, `$1 ${REDACTED}`],
|
|
22
22
|
// Secret-ish `key: value` / `key=value` / `key: "value"` / `key: 'value'` pairs.
|
|
23
|
+
// The separator group allows a quote directly after the key name so JSON is
|
|
24
|
+
// covered: in `{"api_key": "secret"}` the key's own closing quote sits
|
|
25
|
+
// between the name and the colon.
|
|
26
|
+
// Leading lookbehind (not \b) so env-style names like COMPOSIO_API_KEY still
|
|
27
|
+
// match: underscore is a word char, so \b can't fire between COMPOSIO_ and API.
|
|
23
28
|
[
|
|
24
|
-
|
|
29
|
+
/(?<![A-Za-z0-9])(authorization|api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd)\b(["']?\s*[:=]+\s*)(["']?)([^\s"',}&]+)\3/gi,
|
|
25
30
|
`$1$2$3${REDACTED}$3`,
|
|
26
31
|
],
|
|
27
32
|
];
|
package/src/types/tool.types.ts
CHANGED
|
@@ -26,10 +26,55 @@ const JSONSchemaType = z.enum([
|
|
|
26
26
|
'array',
|
|
27
27
|
'null',
|
|
28
28
|
]);
|
|
29
|
+
type JSONSchemaTypeName = z.infer<typeof JSONSchemaType>;
|
|
29
30
|
|
|
30
31
|
// JSON Schema property definition
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
export interface JSONSchemaProperty {
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
type?: JSONSchemaTypeName | JSONSchemaTypeName[];
|
|
35
|
+
description?: string;
|
|
36
|
+
anyOf?: JSONSchemaProperty[];
|
|
37
|
+
oneOf?: JSONSchemaProperty[];
|
|
38
|
+
allOf?: JSONSchemaProperty[];
|
|
39
|
+
not?: JSONSchemaProperty;
|
|
40
|
+
title?: string;
|
|
41
|
+
default?: unknown;
|
|
42
|
+
nullable?: boolean;
|
|
43
|
+
properties?: Record<string, JSONSchemaProperty>;
|
|
44
|
+
required?: string[];
|
|
45
|
+
file_uploadable?: boolean;
|
|
46
|
+
file_downloadable?: boolean;
|
|
47
|
+
items?: JSONSchemaProperty | JSONSchemaProperty[];
|
|
48
|
+
enum?: unknown[];
|
|
49
|
+
const?: unknown;
|
|
50
|
+
minimum?: number;
|
|
51
|
+
maximum?: number;
|
|
52
|
+
exclusiveMinimum?: number;
|
|
53
|
+
exclusiveMaximum?: number;
|
|
54
|
+
multipleOf?: number;
|
|
55
|
+
minLength?: number;
|
|
56
|
+
maxLength?: number;
|
|
57
|
+
pattern?: string;
|
|
58
|
+
format?: string;
|
|
59
|
+
minItems?: number;
|
|
60
|
+
maxItems?: number;
|
|
61
|
+
uniqueItems?: boolean;
|
|
62
|
+
minProperties?: number;
|
|
63
|
+
maxProperties?: number;
|
|
64
|
+
patternProperties?: Record<string, JSONSchemaProperty>;
|
|
65
|
+
additionalProperties?: boolean | JSONSchemaProperty;
|
|
66
|
+
examples?: unknown[];
|
|
67
|
+
readOnly?: boolean;
|
|
68
|
+
writeOnly?: boolean;
|
|
69
|
+
if?: JSONSchemaProperty;
|
|
70
|
+
then?: JSONSchemaProperty;
|
|
71
|
+
else?: JSONSchemaProperty;
|
|
72
|
+
$ref?: string;
|
|
73
|
+
definitions?: Record<string, JSONSchemaProperty>;
|
|
74
|
+
$defs?: Record<string, JSONSchemaProperty>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const JSONSchemaPropertySchema: z.ZodType<JSONSchemaProperty> = z.object({
|
|
33
78
|
type: z.union([JSONSchemaType, z.array(JSONSchemaType)]).optional(),
|
|
34
79
|
description: z.string().optional(),
|
|
35
80
|
anyOf: z.lazy(() => z.array(JSONSchemaPropertySchema)).optional(),
|
|
@@ -37,7 +82,7 @@ export const JSONSchemaPropertySchema: z.ZodType<any> = z.object({
|
|
|
37
82
|
allOf: z.lazy(() => z.array(JSONSchemaPropertySchema)).optional(),
|
|
38
83
|
not: z.lazy(() => JSONSchemaPropertySchema).optional(),
|
|
39
84
|
title: z.string().optional(),
|
|
40
|
-
default: z.
|
|
85
|
+
default: z.unknown().optional(),
|
|
41
86
|
nullable: z.boolean().optional(),
|
|
42
87
|
properties: z.lazy(() => z.record(z.string(), JSONSchemaPropertySchema)).optional(),
|
|
43
88
|
required: z.array(z.string()).optional(),
|
|
@@ -46,8 +91,8 @@ export const JSONSchemaPropertySchema: z.ZodType<any> = z.object({
|
|
|
46
91
|
items: z
|
|
47
92
|
.lazy(() => z.union([JSONSchemaPropertySchema, z.array(JSONSchemaPropertySchema)]))
|
|
48
93
|
.optional(),
|
|
49
|
-
enum: z.array(z.
|
|
50
|
-
const: z.
|
|
94
|
+
enum: z.array(z.unknown()).optional(),
|
|
95
|
+
const: z.unknown().optional(),
|
|
51
96
|
minimum: z.number().optional(),
|
|
52
97
|
maximum: z.number().optional(),
|
|
53
98
|
exclusiveMinimum: z.number().optional(),
|
|
@@ -64,7 +109,7 @@ export const JSONSchemaPropertySchema: z.ZodType<any> = z.object({
|
|
|
64
109
|
maxProperties: z.number().optional(),
|
|
65
110
|
patternProperties: z.lazy(() => z.record(z.string(), JSONSchemaPropertySchema)).optional(),
|
|
66
111
|
additionalProperties: z.union([z.boolean(), z.lazy(() => JSONSchemaPropertySchema)]).optional(),
|
|
67
|
-
examples: z.array(z.
|
|
112
|
+
examples: z.array(z.unknown()).optional(),
|
|
68
113
|
readOnly: z.boolean().optional(),
|
|
69
114
|
writeOnly: z.boolean().optional(),
|
|
70
115
|
if: z.lazy(() => JSONSchemaPropertySchema).optional(),
|
|
@@ -84,7 +129,6 @@ export const JSONSchemaPropertySchema: z.ZodType<any> = z.object({
|
|
|
84
129
|
)
|
|
85
130
|
.optional(),
|
|
86
131
|
});
|
|
87
|
-
export type JSONSchemaProperty = z.infer<typeof JSONSchemaPropertySchema>;
|
|
88
132
|
|
|
89
133
|
// Schema for parameters (input/output)
|
|
90
134
|
const ParametersSchema = z.preprocess(
|
|
@@ -98,7 +142,7 @@ const ParametersSchema = z.preprocess(
|
|
|
98
142
|
properties: z.record(z.string(), JSONSchemaPropertySchema),
|
|
99
143
|
required: z.array(z.string()).optional(),
|
|
100
144
|
title: z.string().optional(),
|
|
101
|
-
default: z.
|
|
145
|
+
default: z.unknown().optional(),
|
|
102
146
|
nullable: z.boolean().optional(),
|
|
103
147
|
description: z.string().optional(),
|
|
104
148
|
additionalProperties: z.boolean().default(false).optional(),
|
|
@@ -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).
|
|
@@ -175,10 +176,12 @@ const readFileContentFromURL = async (
|
|
|
175
176
|
// redirect into them. See ssrfGuard.node.ts.
|
|
176
177
|
const response = await ssrfSafeFetch(path, { signal });
|
|
177
178
|
if (!response.ok) {
|
|
179
|
+
// The error path never reads the body, so release it explicitly (mirrors
|
|
180
|
+
// `readResponseBodyWithLimit`) instead of leaving it to the garbage collector.
|
|
181
|
+
await response.body?.cancel().catch(() => undefined);
|
|
178
182
|
throw new Error(`Failed to fetch file: ${response.statusText}`);
|
|
179
183
|
}
|
|
180
|
-
const
|
|
181
|
-
const content = new Uint8Array(arrayBuffer);
|
|
184
|
+
const content = await readResponseBodyWithLimit(response);
|
|
182
185
|
const mimeType = response.headers.get('content-type') || 'application/octet-stream';
|
|
183
186
|
|
|
184
187
|
// Extract clean filename from URL, removing query parameters
|
|
@@ -39,9 +39,9 @@ import { withCancellation } from '../cancellation';
|
|
|
39
39
|
* whose schema omits the `$defs` target working (see
|
|
40
40
|
* https://github.com/ComposioHQ/composio/issues/3307) instead of throwing.
|
|
41
41
|
*/
|
|
42
|
-
const resolveFileSchema = (
|
|
43
|
-
schema:
|
|
44
|
-
):
|
|
42
|
+
const resolveFileSchema = <Schema extends JSONSchemaProperty>(
|
|
43
|
+
schema: Schema | undefined
|
|
44
|
+
): Schema | undefined =>
|
|
45
45
|
schema ? dereferenceJsonSchema(schema, { onUnresolved: 'sentinel' }) : schema;
|
|
46
46
|
|
|
47
47
|
const getSchemaVariants = (schema: JSONSchemaProperty | undefined): JSONSchemaProperty[] => [
|
|
@@ -59,10 +59,12 @@ const transformSchema = (property: JSONSchemaProperty): JSONSchemaProperty => {
|
|
|
59
59
|
* @param properties - The properties of the tool schema.
|
|
60
60
|
* @returns The transformed properties.
|
|
61
61
|
*/
|
|
62
|
-
export const transformProperties = (
|
|
63
|
-
|
|
62
|
+
export const transformProperties = (
|
|
63
|
+
properties: Record<string, JSONSchemaProperty>
|
|
64
|
+
): Record<string, JSONSchemaProperty> => {
|
|
65
|
+
const newProperties: Record<string, JSONSchemaProperty> = {};
|
|
64
66
|
|
|
65
|
-
for (const [key, property] of Object.entries(properties)
|
|
67
|
+
for (const [key, property] of Object.entries(properties)) {
|
|
66
68
|
newProperties[key] = transformSchema(property);
|
|
67
69
|
}
|
|
68
70
|
|
|
@@ -81,7 +83,7 @@ export const schemaHasFileProperty = (
|
|
|
81
83
|
|
|
82
84
|
// Check nested properties
|
|
83
85
|
if (schema.properties) {
|
|
84
|
-
for (const prop of Object.values(schema.properties)
|
|
86
|
+
for (const prop of Object.values(schema.properties)) {
|
|
85
87
|
if (schemaHasFileProperty(prop, property)) return true;
|
|
86
88
|
}
|
|
87
89
|
}
|