@composio/core 0.17.0 → 0.18.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.
- package/dist/{CustomTool-l-j5HtEf.mjs → CustomTool-Bz0f52QR.mjs} +16 -6
- package/dist/{CustomTool-Uip-fhPw.d.mts → CustomTool-VRfWzMix.d.mts} +1 -1
- package/dist/{FileModifierErrors-B8V8_3VE.mjs → FileModifierErrors-CwB-Inm9.mjs} +1 -1
- package/dist/FileToolModifier.utils.neutral-D_YkHwZP.mjs +188 -0
- package/dist/{SsrfErrors-DNJt57GL.mjs → SsrfErrors-xhdVE4yw.mjs} +123 -3
- package/dist/{Telemetry-DuGJSwUt.mjs → Telemetry-Dw9W5RSp.mjs} +28 -44
- package/dist/cancellation-P5YUlcfz.mjs +704 -0
- package/dist/{composio-B27X6i6A.d.mts → composio-Cb3d1jFe.d.mts} +250 -244
- package/dist/{customTool.types-B2Ava_j5.d.mts → customTool.types-B6ej_8XZ.d.mts} +146 -144
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +1 -1
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +4 -3
- package/dist/{errors-BcEUHZbq.mjs → errors-Ue9wMrPK.mjs} +5 -2
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-B1Wn848p.mjs → fileUtils.node-KE5Fa-af.mjs} +7 -7
- package/dist/index.d.mts +108 -8
- package/dist/index.mjs +109 -55
- package/dist/models/Files.node.mjs +2 -2
- package/dist/{sensitiveFileUploadPaths-BVEt0peg.mjs → sensitiveFileUploadPaths-QN7w4n8v.mjs} +24 -7
- package/dist/ssrfGuard.node-BLX6b-Rf.mjs +384 -0
- package/dist/{types-DdZJuMm2.d.mts → types-CGdgK442.d.mts} +2 -2
- package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +1 -1
- package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.mjs +59 -103
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.workerd.mjs +1 -1
- package/dist/utils/ssrfGuard.node.d.mts +14 -5
- package/dist/utils/ssrfGuard.node.mjs +1 -161
- package/dist/utils/ssrfGuard.workerd.d.mts +1 -1
- package/dist/utils/ssrfGuard.workerd.mjs +2 -2
- package/package.json +25 -3
- package/src/errors/ToolErrors.ts +1 -0
- package/src/errors/ValidationErrors.ts +2 -0
- package/src/index.ts +6 -0
- package/src/models/CustomTool.ts +41 -6
- package/src/models/RemoteFile.ts +54 -36
- package/src/models/ToolRouterSession.ts +45 -12
- package/src/models/Tools.ts +102 -39
- package/src/models/Triggers.ts +15 -0
- package/src/services/telemetry/TelemetryService.ts +27 -12
- package/src/telemetry/redact.ts +133 -6
- package/src/utils/fileUtils.node.ts +10 -2
- package/src/utils/jsonSchema.ts +505 -27
- package/src/utils/logger.ts +6 -6
- package/src/utils/modifiers/FileToolModifier.node.ts +85 -181
- package/src/utils/modifiers/FileToolModifier.utils.neutral.ts +199 -0
- package/src/utils/pinnedDispatcher.node.ts +214 -0
- package/src/utils/sdk.ts +1 -1
- package/src/utils/sensitiveFileUploadPaths.ts +36 -9
- package/src/utils/ssrfGuard.node.ts +101 -11
- package/src/utils/ssrfGuard.workerd.ts +5 -1
- package/dist/FileToolModifier.utils.neutral-B5rT711M.mjs +0 -72
- package/dist/cancellation-NVtGf3St.mjs +0 -364
|
@@ -1,163 +1,3 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import { lookup } from "node:dns/promises";
|
|
3
|
-
import { isIP } from "node:net";
|
|
1
|
+
import { i as ssrfSafeFetchWhereSupported, n as isBlockedIp, r as ssrfSafeFetch, t as assertSafeFetchTarget } from "../ssrfGuard.node-BLX6b-Rf.mjs";
|
|
4
2
|
|
|
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
|
-
* {@link ssrfSafeFetch} for call sites that must keep working on every runtime.
|
|
153
|
-
*
|
|
154
|
-
* `ssrfSafeFetch` fails closed in edge runtimes, which is right for a URL the
|
|
155
|
-
* caller chose to upload — the alternative is fetching it unvalidated — but not
|
|
156
|
-
* for Tool Router session file transfers, where refusing would remove a working
|
|
157
|
-
* feature from Workers rather than close a hole reachable there. On Node this
|
|
158
|
-
* is the full guard; only the edge build differs.
|
|
159
|
-
*/
|
|
160
|
-
const ssrfSafeFetchWhereSupported = ssrfSafeFetch;
|
|
161
|
-
|
|
162
|
-
//#endregion
|
|
163
3
|
export { assertSafeFetchTarget, isBlockedIp, ssrfSafeFetch, ssrfSafeFetchWhereSupported };
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* hostname is publicly routable before connecting. Fail closed instead of
|
|
5
5
|
* falling back to an unguarded fetch.
|
|
6
6
|
*/
|
|
7
|
-
declare const ssrfSafeFetch: (rawUrl: string) => Promise<Response>;
|
|
7
|
+
declare const ssrfSafeFetch: (rawUrl: string, _init?: RequestInit, _maxRedirects?: number) => Promise<Response>;
|
|
8
8
|
/**
|
|
9
9
|
* Unguarded `fetch`, for the call sites that must keep working here.
|
|
10
10
|
*
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as ComposioBlockedInternalUrlError } from "../SsrfErrors-
|
|
1
|
+
import { t as ComposioBlockedInternalUrlError } from "../SsrfErrors-xhdVE4yw.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/utils/ssrfGuard.workerd.ts
|
|
4
4
|
/**
|
|
@@ -6,7 +6,7 @@ import { t as ComposioBlockedInternalUrlError } from "../SsrfErrors-DNJt57GL.mjs
|
|
|
6
6
|
* hostname is publicly routable before connecting. Fail closed instead of
|
|
7
7
|
* falling back to an unguarded fetch.
|
|
8
8
|
*/
|
|
9
|
-
const ssrfSafeFetch = async (rawUrl) => {
|
|
9
|
+
const ssrfSafeFetch = async (rawUrl, _init = {}, _maxRedirects) => {
|
|
10
10
|
throw new ComposioBlockedInternalUrlError("URL file uploads are not supported in edge runtimes because the destination cannot be safely validated", {
|
|
11
11
|
url: rawUrl,
|
|
12
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"]
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composio/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=22.22.3"
|
|
6
|
+
},
|
|
7
|
+
"version": "0.18.1",
|
|
5
8
|
"description": "",
|
|
6
9
|
"main": "dist/index.mjs",
|
|
7
10
|
"publishConfig": {
|
|
@@ -70,6 +73,24 @@
|
|
|
70
73
|
"types": "./dist/utils/json-schema.d.mts",
|
|
71
74
|
"default": "./dist/utils/json-schema.mjs"
|
|
72
75
|
},
|
|
76
|
+
"./utils/ssrf-guard": {
|
|
77
|
+
"workerd": {
|
|
78
|
+
"types": "./dist/utils/ssrfGuard.workerd.d.mts",
|
|
79
|
+
"default": "./dist/utils/ssrfGuard.workerd.mjs"
|
|
80
|
+
},
|
|
81
|
+
"edge-light": {
|
|
82
|
+
"types": "./dist/utils/ssrfGuard.workerd.d.mts",
|
|
83
|
+
"default": "./dist/utils/ssrfGuard.workerd.mjs"
|
|
84
|
+
},
|
|
85
|
+
"node": {
|
|
86
|
+
"types": "./dist/utils/ssrfGuard.node.d.mts",
|
|
87
|
+
"default": "./dist/utils/ssrfGuard.node.mjs"
|
|
88
|
+
},
|
|
89
|
+
"default": {
|
|
90
|
+
"types": "./dist/utils/ssrfGuard.node.d.mts",
|
|
91
|
+
"default": "./dist/utils/ssrfGuard.node.mjs"
|
|
92
|
+
}
|
|
93
|
+
},
|
|
73
94
|
"./generated": {
|
|
74
95
|
"types": "./generated/index.d.ts",
|
|
75
96
|
"default": "./generated/index.js"
|
|
@@ -95,7 +116,7 @@
|
|
|
95
116
|
"tsx": "^4.23.12",
|
|
96
117
|
"typedoc": "^0.28.20",
|
|
97
118
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
98
|
-
"vitest": "^4.1.
|
|
119
|
+
"vitest": "^4.1.11",
|
|
99
120
|
"zod": "^4.4.3"
|
|
100
121
|
},
|
|
101
122
|
"peerDependencies": {
|
|
@@ -109,8 +130,9 @@
|
|
|
109
130
|
"picocolors": "^1.1.1",
|
|
110
131
|
"pusher-js": "^8.6.0",
|
|
111
132
|
"semver": "^7.8.5",
|
|
133
|
+
"undici": "^7.29.0",
|
|
112
134
|
"zod-to-json-schema": "^3.25.2",
|
|
113
|
-
"@composio/json-schema-to-zod": "0.3.
|
|
135
|
+
"@composio/json-schema-to-zod": "0.3.2"
|
|
114
136
|
},
|
|
115
137
|
"gitHead": "4fae6e54d5c150fba955cc5fa314281da5a1e064",
|
|
116
138
|
"repository": {
|
package/src/errors/ToolErrors.ts
CHANGED
|
@@ -204,6 +204,7 @@ export class ComposioToolVersionRequiredError extends ComposioError {
|
|
|
204
204
|
'Set dangerouslySkipVersionCheck to true (this might cause unexpected behavior when new versions of the tools are released)',
|
|
205
205
|
],
|
|
206
206
|
});
|
|
207
|
+
this.name = 'ComposioToolVersionRequiredError';
|
|
207
208
|
}
|
|
208
209
|
}
|
|
209
210
|
export interface ComposioAPIServerErrorBody {
|
|
@@ -78,6 +78,7 @@ export class JsonSchemaToZodError extends ComposioError {
|
|
|
78
78
|
...options,
|
|
79
79
|
code: options.code || ValidationErrorCodes.JSON_SCHEMA_TO_ZOD_ERROR,
|
|
80
80
|
});
|
|
81
|
+
this.name = 'JsonSchemaToZodError';
|
|
81
82
|
}
|
|
82
83
|
}
|
|
83
84
|
|
|
@@ -95,5 +96,6 @@ export class JsonSchemaRefResolutionError extends ComposioError {
|
|
|
95
96
|
...options,
|
|
96
97
|
code: options.code || ValidationErrorCodes.JSON_SCHEMA_REF_RESOLUTION_ERROR,
|
|
97
98
|
});
|
|
99
|
+
this.name = 'JsonSchemaRefResolutionError';
|
|
98
100
|
}
|
|
99
101
|
}
|
package/src/index.ts
CHANGED
|
@@ -9,12 +9,18 @@ export {
|
|
|
9
9
|
deduplicateJsonSchemaRequiredArrays,
|
|
10
10
|
ensureObjectTypeOnProperties,
|
|
11
11
|
jsonSchemaToZodSchema,
|
|
12
|
+
omitNullToolArguments,
|
|
12
13
|
removeNonRequiredProperties,
|
|
14
|
+
toStrictJsonSchema,
|
|
13
15
|
} from './utils/jsonSchema';
|
|
14
16
|
export type {
|
|
15
17
|
DereferenceJsonSchemaOptions,
|
|
16
18
|
UnresolvedRefReason,
|
|
17
19
|
UnresolvedRefStrategy,
|
|
20
|
+
StrictSchemaChange,
|
|
21
|
+
StrictSchemaChangeReason,
|
|
22
|
+
StrictSchemaIncompatibility,
|
|
23
|
+
StrictJsonSchemaResult,
|
|
18
24
|
} from './utils/jsonSchema';
|
|
19
25
|
export { getExtensionFromMimeType } from './utils/mime';
|
|
20
26
|
export { normalizeToolArguments } from './utils/toolArguments';
|
package/src/models/CustomTool.ts
CHANGED
|
@@ -514,7 +514,11 @@ export function buildCustomToolsMapFromResponse(
|
|
|
514
514
|
}
|
|
515
515
|
}
|
|
516
516
|
|
|
517
|
-
const findHandle = (
|
|
517
|
+
const findHandle = (
|
|
518
|
+
finalSlug: string,
|
|
519
|
+
originalSlug: string,
|
|
520
|
+
toolkit?: string
|
|
521
|
+
): HandleMatch | undefined => {
|
|
518
522
|
const originalSlugKey = originalSlug.toUpperCase();
|
|
519
523
|
const qualifiedMatch = handlesByQualifiedOriginalSlug.get(
|
|
520
524
|
qualifiedOriginalSlugKey(toolkit, originalSlugKey)
|
|
@@ -522,13 +526,26 @@ export function buildCustomToolsMapFromResponse(
|
|
|
522
526
|
if (qualifiedMatch) return qualifiedMatch;
|
|
523
527
|
|
|
524
528
|
const bareMatches = handlesByOriginalSlug.get(originalSlugKey) ?? [];
|
|
525
|
-
|
|
529
|
+
// Response tool not defined locally; nothing to route in-process.
|
|
530
|
+
if (bareMatches.length === 0) return undefined;
|
|
531
|
+
// A bare match may only stand in when the response carries no toolkit identity.
|
|
532
|
+
// Binding a toolkit child to another toolkit's handler would silently execute
|
|
533
|
+
// the wrong implementation.
|
|
534
|
+
if (toolkit === undefined && bareMatches.length === 1) return bareMatches[0];
|
|
535
|
+
|
|
536
|
+
const registered = [
|
|
537
|
+
...new Set(bareMatches.map(m => (m.toolkit ?? 'custom').toUpperCase())),
|
|
538
|
+
].sort();
|
|
539
|
+
throw new ValidationError(
|
|
540
|
+
`Cannot map custom tool "${finalSlug}": original slug "${originalSlug}" for toolkit ` +
|
|
541
|
+
`"${toolkit ?? 'custom'}" has no exact local match. Registered for toolkits: ${registered.join(', ')}.`
|
|
542
|
+
);
|
|
526
543
|
};
|
|
527
544
|
|
|
528
545
|
const addEntry = (finalSlug: string, originalSlug: string, toolkit?: string | null) => {
|
|
529
546
|
const resolvedToolkit = toolkit ?? undefined;
|
|
530
|
-
const match = findHandle(originalSlug, resolvedToolkit);
|
|
531
|
-
if (!match) return;
|
|
547
|
+
const match = findHandle(finalSlug, originalSlug, resolvedToolkit);
|
|
548
|
+
if (!match) return;
|
|
532
549
|
|
|
533
550
|
const finalSlugKey = finalSlug.toUpperCase();
|
|
534
551
|
if (byFinalSlug.has(finalSlugKey)) {
|
|
@@ -537,13 +554,21 @@ export function buildCustomToolsMapFromResponse(
|
|
|
537
554
|
);
|
|
538
555
|
}
|
|
539
556
|
|
|
557
|
+
const entryToolkit = resolvedToolkit ?? match.toolkit;
|
|
558
|
+
const qualifiedKey = qualifiedOriginalSlugKey(entryToolkit, originalSlug);
|
|
559
|
+
if (byToolkitAndOriginalSlug.has(qualifiedKey)) {
|
|
560
|
+
throw new ValidationError(
|
|
561
|
+
`Custom tool slug collision: original slug "${match.handle.slug}" is already registered for toolkit "${entryToolkit ?? 'custom'}".`
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
|
|
540
565
|
const entry: CustomToolsMapEntry = {
|
|
541
566
|
handle: match.handle,
|
|
542
567
|
finalSlug,
|
|
543
|
-
toolkit:
|
|
568
|
+
toolkit: entryToolkit,
|
|
544
569
|
};
|
|
545
570
|
byFinalSlug.set(finalSlugKey, entry);
|
|
546
|
-
byToolkitAndOriginalSlug.set(
|
|
571
|
+
byToolkitAndOriginalSlug.set(qualifiedKey, entry);
|
|
547
572
|
addOriginalSlugAlias({ byOriginalSlug, ambiguousOriginalSlugs, originalSlug, entry });
|
|
548
573
|
};
|
|
549
574
|
|
|
@@ -563,6 +588,16 @@ export function buildCustomToolsMapFromResponse(
|
|
|
563
588
|
}
|
|
564
589
|
}
|
|
565
590
|
|
|
591
|
+
// Ambiguity follows the local definitions, not whichever subset the response
|
|
592
|
+
// happened to include: a shared child slug must never become callable by its
|
|
593
|
+
// bare name just because a sibling was omitted.
|
|
594
|
+
for (const [originalSlugKey, matches] of handlesByOriginalSlug) {
|
|
595
|
+
if (matches.length > 1) {
|
|
596
|
+
byOriginalSlug.delete(originalSlugKey);
|
|
597
|
+
ambiguousOriginalSlugs.add(originalSlugKey);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
566
601
|
return {
|
|
567
602
|
byFinalSlug,
|
|
568
603
|
byOriginalSlug,
|
package/src/models/RemoteFile.ts
CHANGED
|
@@ -8,7 +8,12 @@ function getParentDir(filePath: string): string {
|
|
|
8
8
|
return filePath.slice(0, lastSep);
|
|
9
9
|
}
|
|
10
10
|
import { RemoteFileData, RemoteFileDataSchema } from '../types/ToolRouterSessionFilesMount.types';
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
ComposioBlockedInternalUrlError,
|
|
13
|
+
RemoteFileDownloadError,
|
|
14
|
+
ValidationError,
|
|
15
|
+
} from '../errors';
|
|
16
|
+
import { readResponseBodyWithLimit } from '../utils/readResponseBody';
|
|
12
17
|
|
|
13
18
|
/**
|
|
14
19
|
* Represents a file stored in a tool router session's file mount.
|
|
@@ -77,31 +82,58 @@ export class RemoteFile {
|
|
|
77
82
|
return platform.basename(this.mountRelativePath);
|
|
78
83
|
}
|
|
79
84
|
|
|
85
|
+
private downloadError(message: string, cause: unknown, response?: Response) {
|
|
86
|
+
return new RemoteFileDownloadError(message, {
|
|
87
|
+
statusCode: response?.status,
|
|
88
|
+
statusText: response?.statusText,
|
|
89
|
+
downloadUrl: this.downloadUrl,
|
|
90
|
+
mountRelativePath: this.mountRelativePath,
|
|
91
|
+
filename: this.filename,
|
|
92
|
+
cause,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private async downloadBytes(): Promise<{ content: Uint8Array; contentType: string }> {
|
|
97
|
+
let response: Response;
|
|
98
|
+
try {
|
|
99
|
+
response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error instanceof ComposioBlockedInternalUrlError) {
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
throw this.downloadError('Failed to download file', error);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!response.ok) {
|
|
108
|
+
await response.body?.cancel().catch(() => undefined);
|
|
109
|
+
throw this.downloadError(
|
|
110
|
+
`Failed to download file: ${response.status} ${response.statusText}`,
|
|
111
|
+
new Error(`HTTP ${response.status}: ${response.statusText}`),
|
|
112
|
+
response
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const content = response.body
|
|
118
|
+
? await readResponseBodyWithLimit(response)
|
|
119
|
+
: new Uint8Array(await response.arrayBuffer());
|
|
120
|
+
return {
|
|
121
|
+
content,
|
|
122
|
+
contentType: response.headers?.get?.('content-type') ?? '',
|
|
123
|
+
};
|
|
124
|
+
} catch (error) {
|
|
125
|
+
await response.body?.cancel().catch(() => undefined);
|
|
126
|
+
throw this.downloadError('Failed to read downloaded file', error, response);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
80
130
|
/**
|
|
81
131
|
* Fetches the file content as a buffer.
|
|
82
132
|
* @returns The file content as a Uint8Array
|
|
83
133
|
* @throws RemoteFileDownloadError if the fetch fails
|
|
84
134
|
*/
|
|
85
135
|
async buffer(): Promise<Uint8Array> {
|
|
86
|
-
|
|
87
|
-
// input like every other response field, and its bytes are handed straight
|
|
88
|
-
// back to the caller. See ssrfGuard.node.ts.
|
|
89
|
-
const response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
|
|
90
|
-
if (!response.ok) {
|
|
91
|
-
throw new RemoteFileDownloadError(
|
|
92
|
-
`Failed to download file: ${response.status} ${response.statusText}`,
|
|
93
|
-
{
|
|
94
|
-
statusCode: response.status,
|
|
95
|
-
statusText: response.statusText,
|
|
96
|
-
downloadUrl: this.downloadUrl,
|
|
97
|
-
mountRelativePath: this.mountRelativePath,
|
|
98
|
-
filename: this.filename,
|
|
99
|
-
cause: new Error(`HTTP ${response.status}: ${response.statusText}`),
|
|
100
|
-
}
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
const arrayBuffer = await response.arrayBuffer();
|
|
104
|
-
return new Uint8Array(arrayBuffer);
|
|
136
|
+
return (await this.downloadBytes()).content;
|
|
105
137
|
}
|
|
106
138
|
|
|
107
139
|
/**
|
|
@@ -120,22 +152,8 @@ export class RemoteFile {
|
|
|
120
152
|
* @throws RemoteFileDownloadError if the fetch fails
|
|
121
153
|
*/
|
|
122
154
|
async blob(): Promise<Blob> {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (!response.ok) {
|
|
126
|
-
throw new RemoteFileDownloadError(
|
|
127
|
-
`Failed to download file: ${response.status} ${response.statusText}`,
|
|
128
|
-
{
|
|
129
|
-
statusCode: response.status,
|
|
130
|
-
statusText: response.statusText,
|
|
131
|
-
downloadUrl: this.downloadUrl,
|
|
132
|
-
mountRelativePath: this.mountRelativePath,
|
|
133
|
-
filename: this.filename,
|
|
134
|
-
cause: new Error(`HTTP ${response.status}: ${response.statusText}`),
|
|
135
|
-
}
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
return response.blob();
|
|
155
|
+
const { content, contentType } = await this.downloadBytes();
|
|
156
|
+
return new Blob([content as BlobPart], { type: contentType });
|
|
139
157
|
}
|
|
140
158
|
|
|
141
159
|
/**
|
|
@@ -41,7 +41,11 @@ import { ACL_ONLY_FOR_SHARED_ERROR_FRAGMENT, serializeExperimentalForWire } from
|
|
|
41
41
|
import { z } from 'zod/v3';
|
|
42
42
|
import { transform } from '../utils/transform';
|
|
43
43
|
import { ToolkitConnectionStateSchema } from '../types/toolRouter.types';
|
|
44
|
-
import {
|
|
44
|
+
import {
|
|
45
|
+
ComposioAclOnlyForSharedError,
|
|
46
|
+
ComposioInvalidModifierError,
|
|
47
|
+
ValidationError,
|
|
48
|
+
} from '../errors';
|
|
45
49
|
import { Tools } from './Tools';
|
|
46
50
|
import { ToolRouterSessionFilesMount } from './ToolRouterSessionFileMount';
|
|
47
51
|
import type {
|
|
@@ -51,7 +55,7 @@ import type {
|
|
|
51
55
|
RegisteredCustomTool,
|
|
52
56
|
RegisteredCustomToolkit,
|
|
53
57
|
} from '../types/customTool.types';
|
|
54
|
-
import type
|
|
58
|
+
import { ToolSchema, type Tool, type ToolExecuteResponse } from '../types/tool.types';
|
|
55
59
|
import type { SessionProxyExecuteParams } from '../types/toolRouter.types';
|
|
56
60
|
import type {
|
|
57
61
|
SessionExecuteParams,
|
|
@@ -190,13 +194,14 @@ export class ToolRouterSession<
|
|
|
190
194
|
requestOptions?: ComposioRequestOptions
|
|
191
195
|
): Promise<ReturnType<TProvider['wrapTools']>> {
|
|
192
196
|
const ToolsModel = new Tools<TToolCollection, TTool, TProvider>(this.client, this.config);
|
|
193
|
-
const
|
|
197
|
+
const rawTools = await ToolsModel.getRawToolRouterSessionTools(
|
|
194
198
|
this.sessionId,
|
|
195
|
-
|
|
199
|
+
undefined,
|
|
196
200
|
requestOptions
|
|
197
201
|
);
|
|
202
|
+
const tools = await this.applySchemaModifier(rawTools, modifiers?.modifySchema);
|
|
198
203
|
const sessionTools = await this.addPreloadedCustomTools(tools, modifiers);
|
|
199
|
-
const
|
|
204
|
+
const rawToolBySlug = new Map(rawTools.map(tool => [tool.slug.toUpperCase(), tool]));
|
|
200
205
|
|
|
201
206
|
if (this.hasCustomTools()) {
|
|
202
207
|
// Create an execute function that splits local/remote tools in COMPOSIO_MULTI_EXECUTE_TOOL
|
|
@@ -205,7 +210,7 @@ export class ToolRouterSession<
|
|
|
205
210
|
input: Record<string, unknown>
|
|
206
211
|
): Promise<ToolExecuteResponse> => {
|
|
207
212
|
if (toolSlug === COMPOSIO_MULTI_EXECUTE_TOOL) {
|
|
208
|
-
return this.routeMultiExecute(input, ToolsModel,
|
|
213
|
+
return this.routeMultiExecute(input, ToolsModel, rawTools, modifiers);
|
|
209
214
|
}
|
|
210
215
|
const customTool = findCustomTool(this.customToolsMap, toolSlug);
|
|
211
216
|
if (customTool) {
|
|
@@ -217,7 +222,7 @@ export class ToolRouterSession<
|
|
|
217
222
|
toolSlug,
|
|
218
223
|
input,
|
|
219
224
|
modifiers,
|
|
220
|
-
|
|
225
|
+
rawToolBySlug.get(toolSlug.toUpperCase())
|
|
221
226
|
);
|
|
222
227
|
};
|
|
223
228
|
|
|
@@ -233,10 +238,33 @@ export class ToolRouterSession<
|
|
|
233
238
|
}
|
|
234
239
|
|
|
235
240
|
// Standard path (no local tools)
|
|
236
|
-
const wrappedTools =
|
|
241
|
+
const wrappedTools = modifiers?.modifySchema
|
|
242
|
+
? ToolsModel.wrapToolsForToolRouter(this.sessionId, sessionTools, modifiers, rawTools)
|
|
243
|
+
: ToolsModel.wrapToolsForToolRouter(this.sessionId, sessionTools, modifiers);
|
|
237
244
|
return wrappedTools as ReturnType<TProvider['wrapTools']>;
|
|
238
245
|
}
|
|
239
246
|
|
|
247
|
+
private async applySchemaModifier(
|
|
248
|
+
tools: Tool[],
|
|
249
|
+
modifier?: SessionMetaToolOptions['modifySchema']
|
|
250
|
+
): Promise<Tool[]> {
|
|
251
|
+
if (!modifier) {
|
|
252
|
+
return tools;
|
|
253
|
+
}
|
|
254
|
+
if (typeof modifier !== 'function') {
|
|
255
|
+
throw new ComposioInvalidModifierError('Invalid schema modifier. Not a function.');
|
|
256
|
+
}
|
|
257
|
+
return Promise.all(
|
|
258
|
+
tools.map(tool =>
|
|
259
|
+
modifier({
|
|
260
|
+
toolSlug: tool.slug,
|
|
261
|
+
toolkitSlug: tool.toolkit?.slug ?? 'unknown',
|
|
262
|
+
schema: ToolSchema.parse(tool),
|
|
263
|
+
})
|
|
264
|
+
)
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
240
268
|
private async addPreloadedCustomTools(
|
|
241
269
|
tools: Tool[],
|
|
242
270
|
modifiers?: SessionMetaToolOptions
|
|
@@ -347,9 +375,13 @@ export class ToolRouterSession<
|
|
|
347
375
|
tools: tk.tools.map(tool => {
|
|
348
376
|
// Look up by toolkit + original slug so toolkits can safely reuse common names
|
|
349
377
|
// like VERSION, CLICK, or SEARCH without losing the backend-assigned final slug.
|
|
378
|
+
// Only trust a bare alias that belongs to this toolkit.
|
|
379
|
+
const bare = this.customToolsMap!.byOriginalSlug.get(tool.slug.toUpperCase());
|
|
350
380
|
const entry =
|
|
351
381
|
findCustomToolMapEntryByToolkitAndOriginalSlug(this.customToolsMap, tk.slug, tool.slug) ??
|
|
352
|
-
|
|
382
|
+
(bare?.toolkit && bare.toolkit.toLowerCase() === tk.slug.toLowerCase()
|
|
383
|
+
? bare
|
|
384
|
+
: undefined);
|
|
353
385
|
return {
|
|
354
386
|
slug: entry?.finalSlug ?? tool.slug,
|
|
355
387
|
name: tool.name,
|
|
@@ -527,9 +559,10 @@ export class ToolRouterSession<
|
|
|
527
559
|
/**
|
|
528
560
|
* Execute a tool within the session.
|
|
529
561
|
*
|
|
530
|
-
* For custom tools, accepts the
|
|
531
|
-
*
|
|
532
|
-
*
|
|
562
|
+
* For custom tools, accepts the full slug (e.g. "LOCAL_GREP") or the
|
|
563
|
+
* original slug (e.g. "GREP") when that original slug is unique across
|
|
564
|
+
* the session's custom tools and toolkits. Custom tools are executed
|
|
565
|
+
* in-process; remote tools are sent to the Composio backend.
|
|
533
566
|
*
|
|
534
567
|
* @param toolSlug - The tool slug to execute
|
|
535
568
|
* @param arguments_ - Optional tool arguments
|