@composio/core 0.31.0 → 0.32.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.
Files changed (84) hide show
  1. package/dist/{CustomTool-9Vlb4XaS.mjs → CustomTool-B4dQn2M5.mjs} +1 -1
  2. package/dist/{CustomTool-Ds5aLDth.d.mts → CustomTool-CBx9kzea.d.mts} +1 -1
  3. package/dist/{FileModifierErrors-WrqokuIF.mjs → FileModifierErrors-D-AKc3ds.mjs} +1 -1
  4. package/dist/{SsrfErrors-BSVMW915.mjs → SsrfErrors-BekZPeWY.mjs} +59 -12
  5. package/dist/{Telemetry-a7SOQXcg.mjs → Telemetry-BWKZmDxM.mjs} +2 -2
  6. package/dist/composio-CPf6NHnB.d.mts +20334 -0
  7. package/dist/{customTool.types-DHxXBU6E.d.mts → customTool.types-BSbIG1OG.d.mts} +291 -68
  8. package/dist/docs/reference/sdk-reference/typescript/auth-configs.mdx +20 -12
  9. package/dist/docs/reference/sdk-reference/typescript/composio.mdx +28 -10
  10. package/dist/docs/reference/sdk-reference/typescript/connected-accounts.mdx +44 -32
  11. package/dist/docs/reference/sdk-reference/typescript/custom-toolkits.mdx +8 -6
  12. package/dist/docs/reference/sdk-reference/typescript/experimental.mdx +4 -3
  13. package/dist/docs/reference/sdk-reference/typescript/index.mdx +4 -2
  14. package/dist/docs/reference/sdk-reference/typescript/keyring.mdx +6 -3
  15. package/dist/docs/reference/sdk-reference/typescript/logs.mdx +5 -3
  16. package/dist/docs/reference/sdk-reference/typescript/mcp.mdx +53 -42
  17. package/dist/docs/reference/sdk-reference/typescript/meta.json +1 -0
  18. package/dist/docs/reference/sdk-reference/typescript/remote-file.mdx +5 -14
  19. package/dist/docs/reference/sdk-reference/typescript/session-configs.mdx +5 -3
  20. package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +7 -5
  21. package/dist/docs/reference/sdk-reference/typescript/session-webhooks.mdx +64 -0
  22. package/dist/docs/reference/sdk-reference/typescript/session.mdx +16 -16
  23. package/dist/docs/reference/sdk-reference/typescript/sessions.mdx +36 -8
  24. package/dist/docs/reference/sdk-reference/typescript/toolkits.mdx +14 -11
  25. package/dist/docs/reference/sdk-reference/typescript/tools.mdx +16 -18
  26. package/dist/docs/reference/sdk-reference/typescript/triggers.mdx +42 -30
  27. package/dist/docs/reference/sdk-reference/typescript/usage.mdx +6 -4
  28. package/dist/docs/reference/sdk-reference/typescript/webhook-endpoints.mdx +9 -7
  29. package/dist/docs/reference/sdk-reference/typescript/webhook-subscriptions.mdx +13 -9
  30. package/dist/docs/reference/sdk-reference/typescript/webhooks.mdx +3 -1
  31. package/dist/{errors-xC13uvaA.mjs → errors-CtIKA9BH.mjs} +2 -2
  32. package/dist/experimental/index.d.mts +2 -2
  33. package/dist/experimental/index.mjs +1 -1
  34. package/dist/{fileUtils.node-CAwIbWYH.mjs → fileUtils.node-BJUYMEph.mjs} +7 -7
  35. package/dist/index.d.mts +6 -11
  36. package/dist/index.mjs +526 -233
  37. package/dist/{jsonSchema-Cc4EtHRw.mjs → jsonSchema-BZoXGUCr.mjs} +2 -2
  38. package/dist/models/Files.node.mjs +3 -3
  39. package/dist/{sensitiveFileUploadPaths-CSyJ3Ku8.mjs → sensitiveFileUploadPaths-CcbMAYCC.mjs} +21 -15
  40. package/dist/{ssrfGuard.node-CdIkwqVu.mjs → ssrfGuard.node-B749ae5Q.mjs} +1 -1
  41. package/dist/{types-DcdZy5Nc.d.mts → types-ns3Q_WrP.d.mts} +2 -2
  42. package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +1 -1
  43. package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +1 -1
  44. package/dist/utils/modifiers/FileToolModifier.node.d.mts +1 -1
  45. package/dist/utils/modifiers/FileToolModifier.node.mjs +5 -5
  46. package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -1
  47. package/dist/utils/ssrfGuard.node.mjs +1 -1
  48. package/dist/utils/ssrfGuard.workerd.mjs +1 -1
  49. package/package.json +2 -2
  50. package/src/composio.ts +26 -7
  51. package/src/errors/ComposioError.ts +3 -2
  52. package/src/index.ts +19 -0
  53. package/src/lib/toolRouterParams.ts +38 -14
  54. package/src/models/AuthConfigs.ts +10 -3
  55. package/src/models/ConnectedAccounts.ts +30 -17
  56. package/src/models/CustomTool.ts +0 -10
  57. package/src/models/Keyring.ts +2 -1
  58. package/src/models/MCP.ts +43 -35
  59. package/src/models/SessionWebhooks.ts +136 -0
  60. package/src/models/ToolRouter.ts +39 -4
  61. package/src/models/ToolRouterSession.ts +2 -2
  62. package/src/models/ToolRouterSessionFileMount.ts +3 -1
  63. package/src/models/Toolkits.ts +1 -0
  64. package/src/models/Tools.ts +4 -8
  65. package/src/models/Triggers.ts +48 -213
  66. package/src/models/Webhooks.ts +2 -0
  67. package/src/provider/ComposioProvider.ts +0 -1
  68. package/src/telemetry/BatchProcessor.ts +1 -1
  69. package/src/telemetry/Telemetry.ts +1 -1
  70. package/src/types/connectedAccountAuthStates.types.ts +0 -1
  71. package/src/types/provider.types.ts +0 -1
  72. package/src/types/sessionWebhooks.types.ts +292 -0
  73. package/src/types/toolRouter.types.ts +33 -4
  74. package/src/types/triggers.types.ts +4 -0
  75. package/src/utils/crypto.ts +6 -5
  76. package/src/utils/env.ts +2 -5
  77. package/src/utils/fileUtils.node.ts +4 -3
  78. package/src/utils/logger.ts +19 -9
  79. package/src/utils/pusher.ts +5 -2
  80. package/src/utils/readResponseBody.ts +30 -17
  81. package/src/utils/transformers/authConfigs.ts +0 -1
  82. package/src/utils/userCallback.ts +3 -1
  83. package/src/utils/webhookVerification.ts +211 -0
  84. package/dist/composio-Ba89XGHL.d.mts +0 -9736
@@ -0,0 +1,211 @@
1
+ import {
2
+ ComposioWebhookPayloadError,
3
+ ComposioWebhookSignatureVerificationError,
4
+ } from '../errors/TriggerErrors';
5
+ import type { WebhookRequestLike } from '../types/triggers.types';
6
+ import { hmacSha256Base64, timingSafeEqual } from './crypto';
7
+ import { assertValidBodyLimit, readBodyWithLimit } from './readResponseBody';
8
+ import { describeThrown } from './userCallback';
9
+
10
+ const DEFAULT_WEBHOOK_BODY_LIMIT_BYTES = 10 * 1024 * 1024;
11
+
12
+ /**
13
+ * The signature header names Composio sends with every webhook request.
14
+ */
15
+ export const WEBHOOK_HEADERS = {
16
+ id: 'webhook-id',
17
+ timestamp: 'webhook-timestamp',
18
+ signature: 'webhook-signature',
19
+ } as const;
20
+
21
+ const isFetchRequest = (request: WebhookRequestLike): request is Request =>
22
+ typeof Request !== 'undefined' && request instanceof Request;
23
+
24
+ /**
25
+ * Coerces an already-buffered request body into the string expected by
26
+ * signature verification.
27
+ */
28
+ const bodyToString = (body: unknown): string => {
29
+ if (typeof body === 'string') {
30
+ return body;
31
+ }
32
+ if (body === null || body === undefined) {
33
+ return '';
34
+ }
35
+ // Already-parsed object (e.g. Next.js Pages Router with the JSON body parser).
36
+ // Note: re-stringifying cannot reproduce the exact bytes Composio signed, so
37
+ // signature verification on a pre-parsed body is best-effort only.
38
+ if (typeof body === 'object') {
39
+ return JSON.stringify(body);
40
+ }
41
+ return describeThrown(body);
42
+ };
43
+
44
+ const webhookBodyLimitError = (maxBytes: number): ComposioWebhookPayloadError =>
45
+ new ComposioWebhookPayloadError(`Webhook payload exceeds the ${maxBytes}-byte limit`);
46
+
47
+ export const assertWebhookBodySize = (body: string | Uint8Array, maxBytes: number): void => {
48
+ assertValidBodyLimit(maxBytes);
49
+ if (
50
+ body.length > maxBytes ||
51
+ (typeof body === 'string' && new TextEncoder().encode(body).byteLength > maxBytes)
52
+ ) {
53
+ throw webhookBodyLimitError(maxBytes);
54
+ }
55
+ };
56
+
57
+ /**
58
+ * Reads the raw body and the headers from either request shape: a Fetch API
59
+ * `Request`, or a plain `{ body, headers }` object.
60
+ */
61
+ export const readWebhookRequest = async (
62
+ request: WebhookRequestLike,
63
+ maxBytes = DEFAULT_WEBHOOK_BODY_LIMIT_BYTES
64
+ ): Promise<{ body: string; headers: unknown }> => {
65
+ if (isFetchRequest(request)) {
66
+ if (request.bodyUsed) {
67
+ throw new TypeError('Webhook request body has already been read');
68
+ }
69
+ const bytes = await readBodyWithLimit(request, maxBytes, () => webhookBodyLimitError(maxBytes));
70
+ return { body: new TextDecoder().decode(bytes), headers: request.headers };
71
+ }
72
+ const body = request.body instanceof Uint8Array ? request.body : bodyToString(request.body);
73
+ assertWebhookBodySize(body, maxBytes);
74
+ return {
75
+ body: typeof body === 'string' ? body : new TextDecoder().decode(body),
76
+ headers: request.headers,
77
+ };
78
+ };
79
+
80
+ /**
81
+ * Reads a single header value case-insensitively from either a `Headers`
82
+ * instance or a plain record (where values may be `string | string[]`).
83
+ * Returns `undefined` when the header is missing.
84
+ */
85
+ export const getHeader = (headers: unknown, name: string): string | undefined => {
86
+ if (typeof Headers !== 'undefined' && headers instanceof Headers) {
87
+ return headers.get(name) ?? undefined;
88
+ }
89
+
90
+ if (headers === null || typeof headers !== 'object') {
91
+ return undefined;
92
+ }
93
+
94
+ // Plain record: match the header name case-insensitively.
95
+ const target = name.toLowerCase();
96
+ for (const [key, value] of Object.entries(headers as Record<string, unknown>)) {
97
+ if (key.toLowerCase() !== target) {
98
+ continue;
99
+ }
100
+ if (Array.isArray(value)) {
101
+ const first = value.find((item): item is string => typeof item === 'string');
102
+ return first;
103
+ }
104
+ return typeof value === 'string' ? value : undefined;
105
+ }
106
+ return undefined;
107
+ };
108
+
109
+ /**
110
+ * Verifies the HMAC-SHA256 signature of a webhook payload.
111
+ * The signature format used by Composio is: `v1,base64EncodedSignature`
112
+ * The signing input is the UTF-8 prefix `${msgId}.${timestamp}.` followed by
113
+ * the original payload bytes.
114
+ */
115
+ export async function verifyWebhookSignature(
116
+ webhookId: string,
117
+ webhookTimestamp: string,
118
+ payload: string | Uint8Array,
119
+ signature: string,
120
+ secret: string
121
+ ): Promise<void> {
122
+ if (payload.length === 0) {
123
+ throw new ComposioWebhookSignatureVerificationError('No webhook payload was provided.');
124
+ }
125
+
126
+ if (signature.length === 0) {
127
+ throw new ComposioWebhookSignatureVerificationError(
128
+ "No signature header value was provided. Please pass the value of the 'webhook-signature' header."
129
+ );
130
+ }
131
+
132
+ if (secret.length === 0) {
133
+ throw new ComposioWebhookSignatureVerificationError(
134
+ 'No webhook secret was provided. You can find your webhook secret in your Composio dashboard.'
135
+ );
136
+ }
137
+
138
+ if (webhookId.length === 0) {
139
+ throw new ComposioWebhookSignatureVerificationError(
140
+ "No webhook ID was provided. Please pass the value of the 'webhook-id' header."
141
+ );
142
+ }
143
+
144
+ if (webhookTimestamp.length === 0) {
145
+ throw new ComposioWebhookSignatureVerificationError(
146
+ "No webhook timestamp was provided. Please pass the value of the 'webhook-timestamp' header."
147
+ );
148
+ }
149
+
150
+ // Parse signature - may have multiple signatures prefixed with version (e.g., "v1,base64sig")
151
+ const signatures = signature.split(' ');
152
+ const v1Signatures: string[] = [];
153
+
154
+ for (const sig of signatures) {
155
+ const [version, value] = sig.split(',');
156
+ if (version === 'v1' && value) {
157
+ v1Signatures.push(value);
158
+ }
159
+ }
160
+
161
+ if (v1Signatures.length === 0) {
162
+ throw new ComposioWebhookSignatureVerificationError(
163
+ 'No valid v1 signature found in the webhook-signature header. ' +
164
+ "Expected format: 'v1,base64EncodedSignature'"
165
+ );
166
+ }
167
+
168
+ const encoder = new TextEncoder();
169
+ const prefix = `${webhookId}.${webhookTimestamp}.`;
170
+ let toSign: Uint8Array<ArrayBuffer>;
171
+ if (typeof payload === 'string') {
172
+ toSign = encoder.encode(prefix + payload);
173
+ } else {
174
+ const prefixBytes = encoder.encode(prefix);
175
+ toSign = new Uint8Array(prefixBytes.length + payload.length);
176
+ toSign.set(prefixBytes);
177
+ toSign.set(payload, prefixBytes.length);
178
+ }
179
+ const expectedSignature = await hmacSha256Base64(secret, toSign);
180
+
181
+ if (!v1Signatures.some(candidate => timingSafeEqual(candidate, expectedSignature))) {
182
+ throw new ComposioWebhookSignatureVerificationError(
183
+ 'The signature provided is invalid. Please ensure you are using the correct webhook secret.'
184
+ );
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Validates that the webhook timestamp is within the allowed tolerance
190
+ */
191
+ export function validateWebhookTimestamp(webhookTimestamp: string, tolerance: number): void {
192
+ const timestampSeconds = parseInt(webhookTimestamp, 10);
193
+
194
+ if (Number.isNaN(timestampSeconds)) {
195
+ throw new ComposioWebhookPayloadError(
196
+ `Invalid webhook timestamp: ${webhookTimestamp}. Expected Unix timestamp in seconds.`
197
+ );
198
+ }
199
+
200
+ const webhookTimeMs = timestampSeconds * 1000;
201
+ const currentTime = Date.now();
202
+ const timeDifference = Math.abs(currentTime - webhookTimeMs);
203
+
204
+ if (timeDifference > tolerance * 1000) {
205
+ throw new ComposioWebhookSignatureVerificationError(
206
+ `The webhook timestamp is outside the allowed tolerance. ` +
207
+ `The webhook was sent ${Math.round(timeDifference / 1000)} seconds ago, ` +
208
+ `but the maximum allowed age is ${tolerance} seconds.`
209
+ );
210
+ }
211
+ }