@composio/core 0.18.0 → 0.19.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/{CustomTool-C4tv-GL1.mjs → CustomTool-BMlZfJVz.mjs} +16 -6
- package/dist/{CustomTool-BKhHv2hJ.d.mts → CustomTool-CB5vm1Jc.d.mts} +1 -1
- package/dist/{FileModifierErrors-B8V8_3VE.mjs → FileModifierErrors-BqFmpRsm.mjs} +1 -1
- package/dist/{FileToolModifier.utils.neutral-D_YkHwZP.mjs → FileToolModifier.utils.neutral-DWFpnsj2.mjs} +4 -2
- package/dist/{SsrfErrors-DNJt57GL.mjs → SsrfErrors-C91YhASE.mjs} +137 -13
- package/dist/{Telemetry-CBtSnWrm.mjs → Telemetry-HlftxMx6.mjs} +2 -34
- package/dist/{cancellation-jUVItfHw.mjs → cancellation-CXpfmiVo.mjs} +7 -5
- package/dist/{composio-CldtZUyC.d.mts → composio-DLGaeN70.d.mts} +333 -32
- package/dist/{customTool.types-DC5fPOOi.d.mts → customTool.types-BAmEh3MB.d.mts} +131 -38
- package/dist/docs/reference/sdk-reference/typescript/auth-configs.mdx +2 -2
- package/dist/docs/reference/sdk-reference/typescript/composio.mdx +3 -13
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +50 -6
- package/dist/docs/reference/sdk-reference/typescript/toolkits.mdx +4 -4
- package/dist/docs/reference/sdk-reference/typescript/tools.mdx +33 -23
- package/dist/docs/reference/sdk-reference/typescript/triggers.mdx +3 -2
- package/dist/{errors-DyZt79fi.mjs → errors-BuEKehbI.mjs} +23 -3
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-BwpUOF0s.mjs → fileUtils.node-L7XD8UPr.mjs} +11 -9
- package/dist/index.d.mts +164 -104
- package/dist/index.mjs +382 -153
- package/dist/models/Files.node.d.mts +2 -3
- package/dist/models/Files.node.mjs +2 -2
- package/dist/models/Files.workerd.d.mts +2 -3
- package/dist/platform/node.d.mts +2 -3
- package/dist/platform/workerd.d.mts +2 -3
- package/dist/{sensitiveFileUploadPaths-B0w8dqpv.mjs → sensitiveFileUploadPaths-B6pBt9lO.mjs} +10 -7
- package/dist/{ssrfGuard.node-DlxO8uzr.mjs → ssrfGuard.node-B9xQB4za.mjs} +197 -30
- package/dist/{types-BgsRW-XH.d.mts → types-CxLPO0Xq.d.mts} +2 -2
- package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +3 -4
- package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +3 -4
- package/dist/utils/modifiers/FileToolModifier.node.d.mts +3 -4
- package/dist/utils/modifiers/FileToolModifier.node.mjs +6 -6
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +3 -4
- package/dist/utils/modifiers/FileToolModifier.workerd.mjs +1 -1
- package/dist/utils/ssrfGuard.node.d.mts +19 -10
- package/dist/utils/ssrfGuard.node.mjs +1 -1
- package/dist/utils/ssrfGuard.workerd.d.mts +12 -4
- package/dist/utils/ssrfGuard.workerd.mjs +2 -2
- package/package.json +26 -8
- package/src/errors/ToolErrors.ts +24 -0
- package/src/index.ts +4 -0
- package/src/models/CustomTool.ts +41 -6
- package/src/models/MCP.ts +29 -15
- package/src/models/RemoteFile.ts +54 -36
- package/src/models/ToolRouter.ts +1 -0
- package/src/models/ToolRouterSession.ts +115 -13
- package/src/models/Toolkits.ts +1 -2
- package/src/models/Tools.ts +55 -25
- package/src/models/Triggers.ts +22 -2
- package/src/services/pusher/Pusher.ts +29 -10
- package/src/telemetry/redact.ts +133 -6
- package/src/types/toolRouter.types.ts +54 -3
- package/src/types/toolkit.types.ts +17 -0
- package/src/utils/fileUtils.node.ts +10 -2
- package/src/utils/jsonSchema.ts +4 -1
- package/src/utils/logger.ts +6 -6
- package/src/utils/pinnedDispatcher.node.ts +109 -13
- package/src/utils/sdk.ts +1 -1
- package/src/utils/ssrfGuard.node.ts +153 -27
- package/src/utils/ssrfGuard.workerd.ts +15 -1
- package/src/utils/transformers/toolkits.ts +79 -2
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { isIP } from 'node:net';
|
|
2
|
+
import { request as httpRequest } from 'node:http';
|
|
3
|
+
import { request as httpsRequest } from 'node:https';
|
|
4
|
+
import { Readable } from 'node:stream';
|
|
2
5
|
import type { Agent } from 'undici';
|
|
3
6
|
|
|
4
7
|
/**
|
|
@@ -36,6 +39,37 @@ const GLOBAL_DISPATCHER_SYMBOLS = [
|
|
|
36
39
|
let undici: Promise<typeof import('undici')> | undefined;
|
|
37
40
|
const loadUndici = (): Promise<typeof import('undici')> => (undici ??= import('undici'));
|
|
38
41
|
|
|
42
|
+
const lookupPinnedAddresses = (
|
|
43
|
+
addresses: ReadonlyArray<string>,
|
|
44
|
+
options: { all?: boolean },
|
|
45
|
+
callback: (
|
|
46
|
+
error: NodeJS.ErrnoException | null,
|
|
47
|
+
address: string | Array<{ address: string; family: number }>,
|
|
48
|
+
family?: number
|
|
49
|
+
) => void
|
|
50
|
+
) => {
|
|
51
|
+
if (options.all) {
|
|
52
|
+
callback(
|
|
53
|
+
null,
|
|
54
|
+
addresses.map(address => ({ address, family: isIP(address) }))
|
|
55
|
+
);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const [address] = addresses;
|
|
60
|
+
callback(null, address, isIP(address));
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const knownBodyLength = (body: BodyInit | null | undefined): number | undefined => {
|
|
64
|
+
if (body === null || body === undefined) return undefined;
|
|
65
|
+
if (typeof body === 'string') return new TextEncoder().encode(body).byteLength;
|
|
66
|
+
if (body instanceof URLSearchParams) return new TextEncoder().encode(body.toString()).byteLength;
|
|
67
|
+
if (body instanceof ArrayBuffer) return body.byteLength;
|
|
68
|
+
if (ArrayBuffer.isView(body)) return body.byteLength;
|
|
69
|
+
if (body instanceof Blob) return body.size;
|
|
70
|
+
return undefined;
|
|
71
|
+
};
|
|
72
|
+
|
|
39
73
|
/**
|
|
40
74
|
* A `fetch` dispatcher that connects to `addresses` instead of resolving the
|
|
41
75
|
* hostname again.
|
|
@@ -71,24 +105,86 @@ export const createPinnedDispatcher = async (addresses: ReadonlyArray<string>):
|
|
|
71
105
|
// off, or a `family`/`localAddress` was requested. Answering in the
|
|
72
106
|
// wrong shape is rejected as an invalid address, which would fail
|
|
73
107
|
// every pinned fetch in such a process.
|
|
74
|
-
|
|
75
|
-
// All of them, in resolver order: handing over a single address of a
|
|
76
|
-
// dual-stack host would strand callers whose network cannot reach
|
|
77
|
-
// that family, where the runtime would otherwise have fallen back.
|
|
78
|
-
callback(
|
|
79
|
-
null,
|
|
80
|
-
addresses.map(address => ({ address, family: isIP(address) }))
|
|
81
|
-
);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const [address] = addresses;
|
|
86
|
-
callback(null, address, isIP(address));
|
|
108
|
+
lookupPinnedAddresses(addresses, options, callback);
|
|
87
109
|
},
|
|
88
110
|
},
|
|
89
111
|
});
|
|
90
112
|
};
|
|
91
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Fetch through the Node-compatible HTTP client while pinning DNS resolution.
|
|
116
|
+
*
|
|
117
|
+
* Bun implements the Node HTTP client and its `lookup` hook, but its native
|
|
118
|
+
* `fetch` ignores undici's `dispatcher` option. This transport preserves the
|
|
119
|
+
* original hostname for Host and TLS SNI while making the socket use an
|
|
120
|
+
* address that the SSRF guard already validated.
|
|
121
|
+
*/
|
|
122
|
+
export const pinnedHttpFetch = async (
|
|
123
|
+
rawUrl: string,
|
|
124
|
+
init: RequestInit,
|
|
125
|
+
addresses: ReadonlyArray<string>
|
|
126
|
+
): Promise<Response> => {
|
|
127
|
+
const normalized = new Request(rawUrl, init);
|
|
128
|
+
const url = new URL(rawUrl);
|
|
129
|
+
const request = url.protocol === 'https:' ? httpsRequest : httpRequest;
|
|
130
|
+
const headers = new Headers(normalized.headers);
|
|
131
|
+
const bodyLength = knownBodyLength(init.body);
|
|
132
|
+
if (bodyLength !== undefined && !headers.has('content-length')) {
|
|
133
|
+
headers.set('content-length', String(bodyLength));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return new Promise<Response>((resolve, reject) => {
|
|
137
|
+
const outgoing = request(
|
|
138
|
+
url,
|
|
139
|
+
{
|
|
140
|
+
method: normalized.method,
|
|
141
|
+
headers: Object.fromEntries(headers.entries()),
|
|
142
|
+
signal: normalized.signal,
|
|
143
|
+
lookup: (_hostname, options, callback) =>
|
|
144
|
+
lookupPinnedAddresses(addresses, options, callback),
|
|
145
|
+
},
|
|
146
|
+
incoming => {
|
|
147
|
+
const headers = new Headers();
|
|
148
|
+
for (let index = 0; index < incoming.rawHeaders.length; index += 2) {
|
|
149
|
+
headers.append(incoming.rawHeaders[index], incoming.rawHeaders[index + 1]);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const status = incoming.statusCode ?? 500;
|
|
153
|
+
const body = [101, 103, 204, 205, 304].includes(status)
|
|
154
|
+
? null
|
|
155
|
+
: (Readable.toWeb(incoming) as ReadableStream<Uint8Array>);
|
|
156
|
+
resolve(
|
|
157
|
+
new Response(body, {
|
|
158
|
+
status,
|
|
159
|
+
statusText: incoming.statusMessage,
|
|
160
|
+
headers,
|
|
161
|
+
})
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
outgoing.once('error', reject);
|
|
167
|
+
|
|
168
|
+
void (async () => {
|
|
169
|
+
try {
|
|
170
|
+
if (normalized.body) {
|
|
171
|
+
const reader = normalized.body.getReader();
|
|
172
|
+
while (true) {
|
|
173
|
+
const { done, value } = await reader.read();
|
|
174
|
+
if (done) break;
|
|
175
|
+
if (!outgoing.write(value)) {
|
|
176
|
+
await new Promise<void>(resolveDrain => outgoing.once('drain', resolveDrain));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
outgoing.end();
|
|
181
|
+
} catch (error) {
|
|
182
|
+
outgoing.destroy(error instanceof Error ? error : new Error(String(error)));
|
|
183
|
+
}
|
|
184
|
+
})();
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
|
|
92
188
|
/**
|
|
93
189
|
* Whether the process-wide dispatcher `fetch` falls back to (when a request
|
|
94
190
|
* carries no `dispatcher` of its own) is something other than a stock `Agent`.
|
package/src/utils/sdk.ts
CHANGED
|
@@ -53,7 +53,7 @@ export function getSDKConfig(baseUrl?: string | null, apiKey?: string | null) {
|
|
|
53
53
|
ComposioError.handleAndThrow(new ComposioNoAPIKeyError());
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
logger.debug('Environment',
|
|
56
|
+
logger.debug('Environment', 'API Key: [REDACTED]');
|
|
57
57
|
logger.debug('Environment', `Base URL: ${baseURLParsed}`);
|
|
58
58
|
|
|
59
59
|
return { baseURL: baseURLParsed, apiKey: apiKeyParsed };
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { lookup } from 'node:dns/promises'; // we're in a Node.js-specific module
|
|
2
2
|
import { isIP } from 'node:net';
|
|
3
3
|
import { ComposioBlockedInternalUrlError } from '../errors/SsrfErrors';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
createPinnedDispatcher,
|
|
6
|
+
hasCustomGlobalDispatcher,
|
|
7
|
+
pinnedHttpFetch,
|
|
8
|
+
} from './pinnedDispatcher.node';
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* SSRF guard for user-supplied URL file inputs.
|
|
@@ -23,31 +27,112 @@ import { createPinnedDispatcher, hasCustomGlobalDispatcher } from './pinnedDispa
|
|
|
23
27
|
* to a public address during validation and rebind to an internal one before
|
|
24
28
|
* the connect — a TOCTOU window the Python guard closes the same way.
|
|
25
29
|
*
|
|
26
|
-
*
|
|
30
|
+
* By default, a hop whose effective dispatcher is a configured route — a
|
|
27
31
|
* caller-supplied `init.dispatcher`, a non-stock global dispatcher (a
|
|
28
32
|
* `ProxyAgent` or `EnvHttpProxyAgent` installed via `setGlobalDispatcher`), or
|
|
29
|
-
* the runtime's env-proxy mode (
|
|
33
|
+
* the runtime's env-proxy mode (automatic on Bun, opt-in via
|
|
34
|
+
* `NODE_USE_ENV_PROXY` on Node) — keeps only the
|
|
30
35
|
* pre-flight validation. Pinning would dial the validated address instead of
|
|
31
36
|
* the proxy, and the proxy resolves the hostname itself where the SDK cannot
|
|
32
37
|
* see or pin that resolution. The Python guard carries the same residual for
|
|
33
|
-
* the same reason (`_proxy_applies`).
|
|
38
|
+
* the same reason (`_proxy_applies`). Callers that cannot accept that residual
|
|
39
|
+
* can set `requirePinnedConnection`, which fails closed before fetching.
|
|
34
40
|
*/
|
|
35
41
|
|
|
36
42
|
const MAX_REDIRECTS = 5;
|
|
37
43
|
|
|
44
|
+
export interface SsrfSafeFetchOptions {
|
|
45
|
+
/** Maximum number of redirects to follow. Defaults to 5. */
|
|
46
|
+
maxRedirects?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Refuse a request when a configured dispatcher or environment proxy means
|
|
49
|
+
* the connection cannot be pinned to the address validated by the guard.
|
|
50
|
+
*/
|
|
51
|
+
requirePinnedConnection?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The Fetch standard's "redirect status" set. A 3xx outside it is not a
|
|
56
|
+
* redirect to follow even when it carries a `location` — `304 Not Modified`
|
|
57
|
+
* and `305 Use Proxy` most of all, and following those would replay the
|
|
58
|
+
* request against a target the caller never asked for. The Python guard
|
|
59
|
+
* follows the same set (`_REDIRECT_STATUS_CODES`).
|
|
60
|
+
*/
|
|
61
|
+
const REDIRECT_STATUS_CODES = new Set([301, 302, 303, 307, 308]);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Headers that describe a request body, so they have to go when the body does:
|
|
65
|
+
* the Fetch standard's "request-body-header name" set, plus the two the Python
|
|
66
|
+
* guard also drops because `requests` recomputes them from the body it sends.
|
|
67
|
+
*/
|
|
68
|
+
const BODY_HEADERS = [
|
|
69
|
+
'content-encoding',
|
|
70
|
+
'content-language',
|
|
71
|
+
'content-length',
|
|
72
|
+
'content-location',
|
|
73
|
+
'content-type',
|
|
74
|
+
'transfer-encoding',
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The method the hop after a redirect uses, or `null` to replay as-is. A
|
|
79
|
+
* returned method also means the request body goes.
|
|
80
|
+
*
|
|
81
|
+
* These are the Fetch standard's redirect rules, which following redirects by
|
|
82
|
+
* hand (`redirect: 'manual'`) means `fetch` never applies for us: `303` points
|
|
83
|
+
* at a result URL that has no use for the original body, so every method loses
|
|
84
|
+
* it and everything but `HEAD` becomes a `GET`; `301`/`302` do the same to a
|
|
85
|
+
* `POST` only; `307`/`308` replay both. Without this, a `303` answering an
|
|
86
|
+
* upload would PUT the payload again at a URL that expects a GET. The Python
|
|
87
|
+
* guard applies the same rules in `safe_request`.
|
|
88
|
+
*/
|
|
89
|
+
const redirectRewrite = (status: number, method: string): string | null => {
|
|
90
|
+
if (status === 303) return method === 'HEAD' ? 'HEAD' : 'GET';
|
|
91
|
+
if ((status === 301 || status === 302) && method === 'POST') return 'GET';
|
|
92
|
+
return null;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Headers that carry a credential for the origin the request was addressed
|
|
97
|
+
* to, so they must not follow a redirect to a different origin. The Fetch
|
|
98
|
+
* standard strips `Authorization` on a cross-origin redirect; `Cookie` and
|
|
99
|
+
* `Proxy-Authorization` go with it the way `requests` and curl drop them on a
|
|
100
|
+
* host change. `redirect: 'manual'` means `fetch` never applies that rule for
|
|
101
|
+
* us either. The Python guard drops the same three (`_CREDENTIAL_HEADERS`).
|
|
102
|
+
*/
|
|
103
|
+
const CREDENTIAL_HEADERS = ['authorization', 'cookie', 'proxy-authorization'];
|
|
104
|
+
|
|
105
|
+
const applyRedirectSemantics = (
|
|
106
|
+
init: RequestInit,
|
|
107
|
+
status: number,
|
|
108
|
+
fromUrl: string,
|
|
109
|
+
toUrl: string
|
|
110
|
+
): RequestInit => {
|
|
111
|
+
const method = redirectRewrite(status, (init.method ?? 'GET').toUpperCase());
|
|
112
|
+
const crossOrigin = new URL(fromUrl).origin !== new URL(toUrl).origin;
|
|
113
|
+
if (method === null && !crossOrigin) return init;
|
|
114
|
+
|
|
115
|
+
const headers = new Headers(init.headers);
|
|
116
|
+
if (method !== null) for (const name of BODY_HEADERS) headers.delete(name);
|
|
117
|
+
if (crossOrigin) for (const name of CREDENTIAL_HEADERS) headers.delete(name);
|
|
118
|
+
if (method === null) return { ...init, headers };
|
|
119
|
+
return { ...init, method, body: undefined, headers };
|
|
120
|
+
};
|
|
121
|
+
|
|
38
122
|
/**
|
|
39
123
|
* Whether the runtime would route `url` through an env proxy the SDK cannot
|
|
40
|
-
* see into.
|
|
41
|
-
* `fetch`
|
|
42
|
-
*
|
|
43
|
-
*
|
|
124
|
+
* see into. Bun honors proxy environment variables automatically. Node's
|
|
125
|
+
* built-in `fetch` requires the `NODE_USE_ENV_PROXY` opt-in (Node >= 24), so
|
|
126
|
+
* a bare env-var check on Node would drop pinning for users whose fetch never
|
|
127
|
+
* proxied.
|
|
44
128
|
*
|
|
45
129
|
* `NO_PROXY=*` is the one bypass honored precisely (nothing is proxied, so
|
|
46
130
|
* pinning is safe again); per-host `NO_PROXY` entries are treated
|
|
47
131
|
* conservatively as proxied rather than parsed.
|
|
48
132
|
*/
|
|
49
|
-
const envProxyApplies = (url: URL): boolean => {
|
|
50
|
-
const useEnvProxy =
|
|
133
|
+
const envProxyApplies = (url: URL, isBun: boolean): boolean => {
|
|
134
|
+
const useEnvProxy =
|
|
135
|
+
isBun || ['1', 'true'].includes((process.env.NODE_USE_ENV_PROXY ?? '').toLowerCase());
|
|
51
136
|
if (!useEnvProxy) return false;
|
|
52
137
|
if ((process.env.NO_PROXY ?? process.env.no_proxy ?? '') === '*') return false;
|
|
53
138
|
const schemeVar = url.protocol === 'https:' ? 'HTTPS_PROXY' : 'HTTP_PROXY';
|
|
@@ -71,6 +156,7 @@ const IPV4_BLOCKED_CIDRS: ReadonlyArray<readonly [string, number]> = [
|
|
|
71
156
|
['172.16.0.0', 12], // private
|
|
72
157
|
['192.0.0.0', 24], // IETF protocol assignments
|
|
73
158
|
['192.0.2.0', 24], // TEST-NET-1
|
|
159
|
+
['192.88.99.0', 24], // 6to4 relay anycast (deprecated, RFC 7526)
|
|
74
160
|
['192.168.0.0', 16], // private
|
|
75
161
|
['198.18.0.0', 15], // benchmarking
|
|
76
162
|
['198.51.100.0', 24], // TEST-NET-2
|
|
@@ -140,8 +226,20 @@ const isBlockedIpv6 = (ip: string): boolean => {
|
|
|
140
226
|
return isBlockedIpv4Long((((h[6] << 16) >>> 0) | h[7]) >>> 0);
|
|
141
227
|
}
|
|
142
228
|
|
|
229
|
+
// Transition and tunnel ranges. Each one either carries an arbitrary IPv4
|
|
230
|
+
// address in its low bits or is reserved, so a public-looking literal here
|
|
231
|
+
// can still name internal space: `2002:7f00:1::` is 6to4 for `127.0.0.1`.
|
|
232
|
+
// Blocking the whole range rather than decoding it matches the Python guard,
|
|
233
|
+
// where `ipaddress.is_global` already rejects all of them.
|
|
234
|
+
if (h[0] === 0x2001 && (h[1] & 0xfe00) === 0) return true; // IETF protocol assignments 2001::/23 (incl. Teredo)
|
|
235
|
+
if (h[0] === 0x2001 && h[1] === 0x0db8) return true; // documentation 2001:db8::/32
|
|
236
|
+
if (h[0] === 0x2002) return true; // 6to4 2002::/16
|
|
237
|
+
if (h[0] === 0x0064 && h[1] === 0xff9b && h[2] === 0x0001) return true; // local-use NAT64 64:ff9b:1::/48
|
|
238
|
+
if (h[0] === 0x0100 && h[1] === 0 && h[2] === 0 && h[3] === 0) return true; // discard-only 100::/64
|
|
239
|
+
|
|
143
240
|
if ((h[0] & 0xfe00) === 0xfc00) return true; // unique local fc00::/7
|
|
144
241
|
if ((h[0] & 0xffc0) === 0xfe80) return true; // link-local fe80::/10
|
|
242
|
+
if ((h[0] & 0xffc0) === 0xfec0) return true; // site-local fec0::/10 (deprecated)
|
|
145
243
|
if ((h[0] & 0xff00) === 0xff00) return true; // multicast ff00::/8
|
|
146
244
|
|
|
147
245
|
return false;
|
|
@@ -215,42 +313,68 @@ export const assertSafeFetchTarget = async (rawUrl: string): Promise<string[]> =
|
|
|
215
313
|
* connects to the address it validated, and re-validates and re-pins every
|
|
216
314
|
* redirect hop (redirects are followed manually up to {@link MAX_REDIRECTS}).
|
|
217
315
|
* Intermediate redirect bodies are cancelled; non-redirect responses are
|
|
218
|
-
* returned unchanged.
|
|
316
|
+
* returned unchanged. Each hop carries the method, body, and credential headers
|
|
317
|
+
* the Fetch standard says it should — see {@link applyRedirectSemantics}.
|
|
219
318
|
*
|
|
220
|
-
*
|
|
221
|
-
* `init.dispatcher`, non-stock global dispatcher, env-proxy
|
|
222
|
-
* pinned
|
|
319
|
+
* By default, a hop whose effective dispatcher is a configured route
|
|
320
|
+
* (caller-supplied `init.dispatcher`, non-stock global dispatcher, env-proxy
|
|
321
|
+
* mode) is *not* pinned. Set `requirePinnedConnection` to fail closed instead.
|
|
223
322
|
*/
|
|
224
323
|
export const ssrfSafeFetch = async (
|
|
225
324
|
rawUrl: string,
|
|
226
325
|
init: RequestInit = {},
|
|
227
|
-
|
|
326
|
+
optionsOrMaxRedirects: SsrfSafeFetchOptions | number = {}
|
|
228
327
|
): Promise<Response> => {
|
|
328
|
+
const options =
|
|
329
|
+
typeof optionsOrMaxRedirects === 'number'
|
|
330
|
+
? { maxRedirects: optionsOrMaxRedirects }
|
|
331
|
+
: optionsOrMaxRedirects;
|
|
332
|
+
const maxRedirects = options.maxRedirects ?? MAX_REDIRECTS;
|
|
229
333
|
let currentUrl = rawUrl;
|
|
334
|
+
// Rebound per hop: a redirect can drop the method and body (see
|
|
335
|
+
// `applyRedirectSemantics`), and the following hops must send what is left.
|
|
336
|
+
let currentInit = init;
|
|
230
337
|
|
|
231
338
|
for (let hop = 0; hop <= maxRedirects; hop++) {
|
|
232
339
|
const addresses = await assertSafeFetchTarget(currentUrl);
|
|
340
|
+
const isBun = typeof process.versions.bun === 'string';
|
|
233
341
|
|
|
234
342
|
// Pinning replaces the connect target; through a configured route that
|
|
235
343
|
// would dial the validated origin instead of the route's next hop (the
|
|
236
344
|
// proxy), so those hops keep the pre-flight check only.
|
|
237
|
-
const callerDispatcher = (
|
|
345
|
+
const callerDispatcher = (currentInit as RequestInit & { dispatcher?: unknown }).dispatcher;
|
|
238
346
|
const respectConfiguredRoute =
|
|
239
347
|
callerDispatcher !== undefined ||
|
|
240
|
-
envProxyApplies(new URL(currentUrl)) ||
|
|
348
|
+
envProxyApplies(new URL(currentUrl), isBun) ||
|
|
241
349
|
hasCustomGlobalDispatcher();
|
|
242
350
|
|
|
243
|
-
|
|
351
|
+
if (options.requirePinnedConnection && respectConfiguredRoute) {
|
|
352
|
+
throw new ComposioBlockedInternalUrlError(
|
|
353
|
+
'Refusing to fetch through a configured network route because the validated address cannot be pinned',
|
|
354
|
+
{ url: currentUrl }
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const dispatcher =
|
|
359
|
+
respectConfiguredRoute || isBun ? undefined : await createPinnedDispatcher(addresses);
|
|
244
360
|
|
|
245
361
|
let response: Response;
|
|
246
362
|
try {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
363
|
+
if (isBun && !respectConfiguredRoute) {
|
|
364
|
+
response = await pinnedHttpFetch(
|
|
365
|
+
currentUrl,
|
|
366
|
+
{ ...currentInit, redirect: 'manual' },
|
|
367
|
+
addresses
|
|
368
|
+
);
|
|
369
|
+
} else {
|
|
370
|
+
// `dispatcher` is a Node-only extension to `RequestInit`.
|
|
371
|
+
response = await fetch(
|
|
372
|
+
currentUrl,
|
|
373
|
+
(dispatcher === undefined
|
|
374
|
+
? { ...currentInit, redirect: 'manual' }
|
|
375
|
+
: { ...currentInit, redirect: 'manual', dispatcher }) as RequestInit
|
|
376
|
+
);
|
|
377
|
+
}
|
|
254
378
|
} catch (error) {
|
|
255
379
|
if (dispatcher !== undefined) {
|
|
256
380
|
await dispatcher.close().catch(() => undefined);
|
|
@@ -265,7 +389,7 @@ export const ssrfSafeFetch = async (
|
|
|
265
389
|
}
|
|
266
390
|
|
|
267
391
|
const isRedirect =
|
|
268
|
-
|
|
392
|
+
REDIRECT_STATUS_CODES.has(response.status) && response.headers.has('location');
|
|
269
393
|
if (!isRedirect) {
|
|
270
394
|
return response;
|
|
271
395
|
}
|
|
@@ -274,7 +398,9 @@ export const ssrfSafeFetch = async (
|
|
|
274
398
|
// than leaving it to the garbage collector (mirrors `readResponseBodyWithLimit`).
|
|
275
399
|
await response.body?.cancel().catch(() => undefined);
|
|
276
400
|
|
|
277
|
-
|
|
401
|
+
const nextUrl = new URL(response.headers.get('location')!, currentUrl).toString();
|
|
402
|
+
currentInit = applyRedirectSemantics(currentInit, response.status, currentUrl, nextUrl);
|
|
403
|
+
currentUrl = nextUrl;
|
|
278
404
|
}
|
|
279
405
|
|
|
280
406
|
throw new ComposioBlockedInternalUrlError(
|
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
import { ComposioBlockedInternalUrlError } from '../errors/SsrfErrors';
|
|
2
2
|
|
|
3
|
+
export interface SsrfSafeFetchOptions {
|
|
4
|
+
/** Maximum number of redirects to follow. Defaults to 5. */
|
|
5
|
+
maxRedirects?: number;
|
|
6
|
+
/**
|
|
7
|
+
* Refuse a request when a configured dispatcher or environment proxy means
|
|
8
|
+
* the connection cannot be pinned to the address validated by the guard.
|
|
9
|
+
*/
|
|
10
|
+
requirePinnedConnection?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
3
13
|
/**
|
|
4
14
|
* Edge runtimes do not expose the DNS resolution APIs needed to prove that a
|
|
5
15
|
* hostname is publicly routable before connecting. Fail closed instead of
|
|
6
16
|
* falling back to an unguarded fetch.
|
|
7
17
|
*/
|
|
8
|
-
export const ssrfSafeFetch = async (
|
|
18
|
+
export const ssrfSafeFetch = async (
|
|
19
|
+
rawUrl: string,
|
|
20
|
+
_init: RequestInit = {},
|
|
21
|
+
_optionsOrMaxRedirects: SsrfSafeFetchOptions | number = {}
|
|
22
|
+
): Promise<Response> => {
|
|
9
23
|
throw new ComposioBlockedInternalUrlError(
|
|
10
24
|
'URL file uploads are not supported in edge runtimes because the destination cannot be safely validated',
|
|
11
25
|
{
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
import {
|
|
7
7
|
ToolKitListResponse,
|
|
8
8
|
ToolKitListResponseSchema,
|
|
9
|
+
ToolkitAuthField,
|
|
9
10
|
ToolkitRetrieveResponse,
|
|
10
11
|
ToolkitRetrieveResponseSchema,
|
|
11
12
|
ToolkitRetrieveCategoriesResponse,
|
|
@@ -13,6 +14,75 @@ import {
|
|
|
13
14
|
} from '../../types/toolkit.types';
|
|
14
15
|
import { transform } from '../transform';
|
|
15
16
|
|
|
17
|
+
type RawToolkitAuthConfigDetailFields = NonNullable<
|
|
18
|
+
RawToolkitRetrieveResponse['auth_config_details']
|
|
19
|
+
>[number]['fields'];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A single auth form field as the API ships it, across all four groups. The
|
|
23
|
+
* generated client declares each group as its own interface; they are identical
|
|
24
|
+
* today, so the union costs nothing and keeps the mapping honest if they ever
|
|
25
|
+
* drift apart.
|
|
26
|
+
*
|
|
27
|
+
* Most keys are already camelCase on the wire (`displayName`), but `is_secret`
|
|
28
|
+
* and `legacy_template_name` are not, so the field needs an explicit mapping
|
|
29
|
+
* instead of a pass-through: a pass-through leaves them under keys the schema
|
|
30
|
+
* does not know and zod strips them while validating.
|
|
31
|
+
*
|
|
32
|
+
* `type-tests/toolkit-auth-field-mapping.test-d.ts` fails the build when the
|
|
33
|
+
* generated client grows a key in any of the four groups that is not listed
|
|
34
|
+
* there.
|
|
35
|
+
*/
|
|
36
|
+
type RawToolkitAuthField =
|
|
37
|
+
| RawToolkitAuthConfigDetailFields['auth_config_creation']['required'][number]
|
|
38
|
+
| RawToolkitAuthConfigDetailFields['auth_config_creation']['optional'][number]
|
|
39
|
+
| RawToolkitAuthConfigDetailFields['connected_account_initiation']['required'][number]
|
|
40
|
+
| RawToolkitAuthConfigDetailFields['connected_account_initiation']['optional'][number];
|
|
41
|
+
|
|
42
|
+
const transformToolkitAuthField = (field: RawToolkitAuthField): ToolkitAuthField => ({
|
|
43
|
+
name: field.name,
|
|
44
|
+
displayName: field.displayName,
|
|
45
|
+
description: field.description,
|
|
46
|
+
type: field.type,
|
|
47
|
+
required: field.required,
|
|
48
|
+
// Optional keys are spread conditionally rather than assigned. Assigning an
|
|
49
|
+
// absent wire key creates an own property holding `undefined`, which silently
|
|
50
|
+
// beats a caller's fallback in `{ default: 'fallback', ...field }`. JSON never
|
|
51
|
+
// carries an explicit `undefined`, so checking for it is the same test as
|
|
52
|
+
// asking whether the server sent the key at all — and `null` and `false`
|
|
53
|
+
// survive.
|
|
54
|
+
...(field.default !== undefined && { default: field.default }),
|
|
55
|
+
...(field.is_secret !== undefined && { isSecret: field.is_secret }),
|
|
56
|
+
...(field.legacy_template_name !== undefined && {
|
|
57
|
+
legacyTemplateName: field.legacy_template_name,
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Normalize one field group to empty lists when the API omits it, the way this
|
|
63
|
+
* repo's own docs pipeline does (`docs/lib/toolkit-api.ts` defaults a missing
|
|
64
|
+
* group to `{ required: [], optional: [] }`).
|
|
65
|
+
*
|
|
66
|
+
* The generated client declares both groups and both lists as required, but a
|
|
67
|
+
* response that omits one reached zod and failed validation there. Mapping it
|
|
68
|
+
* eagerly would instead throw a `TypeError` on the property access, before
|
|
69
|
+
* validation, turning a handled validation error into a crash of
|
|
70
|
+
* `toolkits.get()`. Normalizing also leaves the other group usable, which is
|
|
71
|
+
* typically the one the caller asked for.
|
|
72
|
+
*/
|
|
73
|
+
const transformToolkitAuthFieldGroup = (
|
|
74
|
+
group:
|
|
75
|
+
| {
|
|
76
|
+
required?: Array<RawToolkitAuthField> | null;
|
|
77
|
+
optional?: Array<RawToolkitAuthField> | null;
|
|
78
|
+
}
|
|
79
|
+
| null
|
|
80
|
+
| undefined
|
|
81
|
+
) => ({
|
|
82
|
+
required: (group?.required ?? []).map(transformToolkitAuthField),
|
|
83
|
+
optional: (group?.optional ?? []).map(transformToolkitAuthField),
|
|
84
|
+
});
|
|
85
|
+
|
|
16
86
|
export const transformToolkitListResponse = (
|
|
17
87
|
response: RawToolkitListResponse
|
|
18
88
|
): ToolKitListResponse => {
|
|
@@ -70,9 +140,16 @@ export const transformToolkitRetrieveResponse = (
|
|
|
70
140
|
authConfigDetails: response.auth_config_details?.map(authConfig => ({
|
|
71
141
|
name: authConfig.name,
|
|
72
142
|
mode: authConfig.mode,
|
|
143
|
+
...(authConfig.auth_hint_url !== undefined && {
|
|
144
|
+
authHintUrl: authConfig.auth_hint_url,
|
|
145
|
+
}),
|
|
73
146
|
fields: {
|
|
74
|
-
authConfigCreation:
|
|
75
|
-
|
|
147
|
+
authConfigCreation: transformToolkitAuthFieldGroup(
|
|
148
|
+
authConfig.fields?.auth_config_creation
|
|
149
|
+
),
|
|
150
|
+
connectedAccountInitiation: transformToolkitAuthFieldGroup(
|
|
151
|
+
authConfig.fields?.connected_account_initiation
|
|
152
|
+
),
|
|
76
153
|
},
|
|
77
154
|
proxy: {
|
|
78
155
|
baseUrl: authConfig.proxy?.base_url,
|