Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
fix(ts/core): address cursorbot
jkomyno committed
Jan 29, 2026 at 21:03 UTC
07c909b3573fef2b1514e7602e82ce0da20b8b27
3 files changed
+123
-9
ts/packages/core/src/models/Triggers.ts
+38
-9
@@ -49,6 +49,20 @@ import { ComposioConfig } from '../composio';
49
import { BaseComposioProvider } from '../provider/BaseProvider';
50
import { hmacSha256Base64, timingSafeEqual } from '../utils/crypto';
51
import { CONFIG_DEFAULTS } from '../utils/config-defaults';
52
+
53
+/**
54
+ * Safely converts a value to a string, returning the default if the value is null, undefined, or empty.
55
+ * This prevents runtime crashes when calling string methods like `.toLowerCase()` on non-string values.
56
+ * @private
57
+ */
58
+const toStringOrDefault = (value: unknown, defaultValue: string): string => {
59
+ if (value === null || value === undefined) {
60
+ return defaultValue;
61
+ }
62
+ const str = String(value);
63
+ return str.length > 0 ? str : defaultValue;
64
+};
65
+
66
/**
67
* Trigger (Instance) class
68
* /api/v3/trigger_instances
@@ -452,6 +466,7 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
466
/**
467
* Tries to parse data as V1, V2, or V3 webhook payload format.
468
* Returns the parsed result with version info, or null if no format matches.
469
+ * Also returns any schema validation errors for debugging purposes.
470
* @private
471
*/
472
private tryParseVersionedPayload(data: unknown):
@@ -531,19 +546,33 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
546
logger.warn('Unknown Pusher payload format. Payload keys: ' + Object.keys(data).join(', '));
547
548
// Return a minimal payload structure to avoid breaking the subscription
549
+ // Use toStringOrDefault to safely convert values and prevent crashes when
550
+ // non-string values are passed (e.g., numbers, objects) and .toLowerCase() is called later
551
+ const id = toStringOrDefault(data.id, toStringOrDefault(data.trigger_id, 'unknown'));
552
+ const uuid = toStringOrDefault(data.uuid, toStringOrDefault(data.id, 'unknown'));
553
+ const triggerSlug = toStringOrDefault(
554
+ data.triggerSlug,
555
+ toStringOrDefault(data.trigger_name, 'UNKNOWN')
556
+ );
557
+ const toolkitSlug = toStringOrDefault(
558
+ data.toolkitSlug,
559
+ toStringOrDefault(data.appName, 'UNKNOWN')
560
+ );
561
+ const userId = toStringOrDefault(data.userId, '');
562
+
563
return {
535
- id: (data.id as string) || (data.trigger_id as string) || 'unknown',
536
- uuid: (data.uuid as string) || (data.id as string) || 'unknown',
537
- triggerSlug: (data.triggerSlug as string) || (data.trigger_name as string) || 'UNKNOWN',
538
- toolkitSlug: (data.toolkitSlug as string) || (data.appName as string) || 'UNKNOWN',
539
- userId: (data.userId as string) || '',
564
+ id,
565
+ uuid,
566
+ triggerSlug,
567
+ toolkitSlug,
568
+ userId,
569
payload: (data.payload as Record<string, unknown>) || data,
570
originalPayload: (data.originalPayload as Record<string, unknown>) || data,
571
metadata: {
543
- id: (data.id as string) || 'unknown',
544
- uuid: (data.uuid as string) || 'unknown',
545
- toolkitSlug: (data.toolkitSlug as string) || 'UNKNOWN',
546
- triggerSlug: (data.triggerSlug as string) || 'UNKNOWN',
572
+ id,
573
+ uuid,
574
+ triggerSlug,
575
+ toolkitSlug,
576
triggerConfig: {},
577
connectedAccount: {
578
id: '',
ts/packages/core/test/models/triggers.test.ts
+61
@@ -1144,6 +1144,67 @@ describe('Triggers', () => {
1144
);
1145
});
1146
1147
+ it('should handle non-string values in fallback payload without crashing', async () => {
1148
+ const malformedPayload = {
1149
+ toolkitSlug: 123, // number instead of string
1150
+ triggerSlug: { foo: 1 }, // object instead of string
1151
+ id: null,
1152
+ userId: undefined,
1153
+ trigger_name: 456, // number instead of string for fallback field
1154
+ };
1155
+
1156
+ await triggers.subscribe(mockCallback);
1157
+ const subscribeCall = vi.mocked(mockPusherService.subscribe).mock.calls[0];
1158
+ const filterCallback = subscribeCall[0];
1159
+
1160
+ // Should not throw, even with non-string values
1161
+ expect(() => filterCallback(malformedPayload)).not.toThrow();
1162
+ expect(mockCallback).toHaveBeenCalledTimes(1);
1163
+
1164
+ // The callback should receive a payload with string values that are safe to use with string methods
1165
+ const receivedPayload = mockCallback.mock.calls[0][0];
1166
+ expect(typeof receivedPayload.toolkitSlug).toBe('string');
1167
+ expect(typeof receivedPayload.triggerSlug).toBe('string');
1168
+ expect(typeof receivedPayload.id).toBe('string');
1169
+ expect(typeof receivedPayload.userId).toBe('string');
1170
+
1171
+ // Verify that calling toLowerCase() doesn't crash (this is what shouldSendTriggerAfterFilters does)
1172
+ expect(() => receivedPayload.toolkitSlug.toLowerCase()).not.toThrow();
1173
+ expect(() => receivedPayload.triggerSlug.toLowerCase()).not.toThrow();
1174
+ });
1175
+
1176
+ it('should safely convert various non-string types in fallback payload', async () => {
1177
+ const payloadWithVariousTypes = {
1178
+ id: 12345, // number
1179
+ uuid: true, // boolean
1180
+ toolkitSlug: ['array'], // array
1181
+ triggerSlug: { nested: 'object' }, // object
1182
+ userId: Symbol('symbol'), // symbol - will convert to 'Symbol(symbol)'
1183
+ appName: null, // null
1184
+ trigger_name: undefined, // undefined
1185
+ };
1186
+
1187
+ await triggers.subscribe(mockCallback);
1188
+ const subscribeCall = vi.mocked(mockPusherService.subscribe).mock.calls[0];
1189
+ const filterCallback = subscribeCall[0];
1190
+
1191
+ // Should not throw
1192
+ expect(() => filterCallback(payloadWithVariousTypes)).not.toThrow();
1193
+ expect(mockCallback).toHaveBeenCalledTimes(1);
1194
+
1195
+ const receivedPayload = mockCallback.mock.calls[0][0];
1196
+
1197
+ // All values should be strings
1198
+ expect(typeof receivedPayload.id).toBe('string');
1199
+ expect(typeof receivedPayload.uuid).toBe('string');
1200
+ expect(typeof receivedPayload.toolkitSlug).toBe('string');
1201
+ expect(typeof receivedPayload.triggerSlug).toBe('string');
1202
+ expect(typeof receivedPayload.userId).toBe('string');
1203
+
1204
+ // Numeric ID should be converted to string
1205
+ expect(receivedPayload.id).toBe('12345');
1206
+ });
1207
+
1208
it('should filter V3 payloads correctly by triggerId', async () => {
1209
const filters = { triggerId: 'trigger-456' };
1210
await triggers.subscribe(mockCallback, filters);
ts/packages/core/test/models/verifyWebhook.test.ts
+24
@@ -503,6 +503,30 @@ describe('Triggers.verifyWebhook', () => {
503
})
504
).rejects.toThrow('does not match any known version');
505
});
506
+
507
+ it('should include detailed schema errors when payload does not match any version', async () => {
508
+ const invalidPayload = JSON.stringify({ invalid: 'data' });
509
+ const signature = createSignature(testWebhookId, testTimestamp, invalidPayload, testSecret);
510
+
511
+ try {
512
+ await triggers.verifyWebhook({
513
+ payload: invalidPayload,
514
+ signature,
515
+ secret: testSecret,
516
+ id: testWebhookId,
517
+ timestamp: testTimestamp,
518
+ });
519
+ expect.fail('Should have thrown');
520
+ } catch (error) {
521
+ expect(error).toBeInstanceOf(ComposioWebhookPayloadError);
522
+ const webhookError = error as ComposioWebhookPayloadError;
523
+ expect(webhookError.cause).toBeDefined();
524
+ // The cause should contain v1Error, v2Error, and v3Error with schema validation messages
525
+ expect((webhookError.cause as any).v1Error).toBeDefined();
526
+ expect((webhookError.cause as any).v2Error).toBeDefined();
527
+ expect((webhookError.cause as any).v3Error).toBeDefined();
528
+ }
529
+ });
530
});
531
532
describe('timestamp validation', () => {