@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 06:00 UTC

next
ts 268 lines 10.4 KB
Raw
1 /**
2 * Regression test for https://github.com/ComposioHQ/composio/issues/3307 —
3 * the Composio API ships some `outputParameters` with a `$ref` into
4 * `#/$defs/...` while never declaring `$defs` (e.g., `GMAIL_FETCH_EMAILS`).
5 * The strict `dereferenceJsonSchema` throws on this, which cascades through
6 * `MastraProvider.wrapTool` and crashes `tools.get` upfront.
7 *
8 * `MastraProvider` opts the dereferencer into `onUnresolved: 'sentinel'`,
9 * replacing dangling internal refs with the cycle-break sentinel
10 * (`{ type: 'object', additionalProperties: true }`) and emitting one
11 * `logger.warn` per `(toolSlug, ref)` pair.
12 *
13 * Like `mastra-ref.test.ts`, this file deliberately does NOT mock
14 * `@mastra/schema-compat` — Vitest mock scoping is per-file, so the mock in
15 * `mastra.test.ts` does not leak here, and the test exercises the real
16 * AJV-backed compat layer.
17 */
18
19 import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
20 import {
21 logger,
22 telemetry,
23 type ExecuteToolFn,
24 type Tool,
25 type ToolExecuteResponse,
26 } from '@composio/core';
27 import { MastraProvider } from '../src';
28
29 // `@mastra/core`'s `createTool` (>= 1.43) wraps JSON Schemas in a
30 // `JsonSchemaWrapper` (a Standard Schema), so a wrapped tool's `inputSchema` /
31 // `outputSchema` is no longer the raw JSON Schema. Reach through `getSchema()`
32 // to inspect the resolved JSON Schema the compat layer produced.
33 const unwrapSchema = (schema: unknown): unknown =>
34 (schema as { getSchema?: () => unknown }).getSchema?.() ?? schema;
35
36 // The fixtures below deliberately build `Tool`-shaped objects whose
37 // `inputParameters` / `outputParameters` carry `$ref` properties — a shape
38 // `ParametersSchema` admits via `JSONSchemaPropertySchema` but that does not
39 // satisfy the parent record type at the literal level. `makeTool` localizes
40 // the cast so individual fixtures stay readable.
41 type TestSchema = Record<string, unknown>;
42 type TestToolOverrides = Partial<Omit<Tool, 'inputParameters' | 'outputParameters'>> & {
43 inputParameters?: TestSchema;
44 outputParameters?: TestSchema;
45 };
46
47 const makeTool = (overrides: TestToolOverrides): Tool =>
48 ({
49 slug: 'TEST_TOOL',
50 name: 'Test Tool',
51 description: '',
52 toolkit: { slug: 'test', name: 'Test' },
53 version: '20260510_00',
54 availableVersions: ['20260510_00'],
55 tags: [],
56 inputParameters: {
57 type: 'object',
58 properties: {},
59 },
60 outputParameters: {
61 type: 'object',
62 properties: {},
63 },
64 ...overrides,
65 }) as unknown as Tool;
66
67 // Exact shape of `GMAIL_FETCH_EMAILS.outputParameters` from
68 // https://github.com/ComposioHQ/composio/issues/3307: a `$ref` into
69 // `#/$defs/...` with no `$defs` block declared anywhere.
70 const danglingOutputTool = makeTool({
71 slug: 'DANGLING_OUTPUT_TOOL',
72 name: 'Dangling Output Tool',
73 description: 'Tool whose outputParameters carries a dangling $ref',
74 toolkit: { slug: 'dangling', name: 'Dangling Toolkit' },
75 inputParameters: {
76 type: 'object',
77 properties: { query: { type: 'string' } },
78 required: ['query'],
79 },
80 outputParameters: {
81 type: 'object',
82 properties: {
83 data: {
84 description: 'Data from the action execution',
85 title: 'Data',
86 $ref: '#/$defs/FetchEmailsResponse',
87 },
88 error: { type: 'string', description: 'Error if the action failed', title: 'Error' },
89 successful: {
90 type: 'boolean',
91 description: 'Whether the action was successful',
92 title: 'Successful',
93 },
94 },
95 required: ['data', 'successful'],
96 title: 'FetchEmailsResponseWrapper',
97 },
98 });
99
100 const danglingInputTool = makeTool({
101 slug: 'DANGLING_INPUT_TOOL',
102 name: 'Dangling Input Tool',
103 toolkit: { slug: 'dangling', name: 'Dangling Toolkit' },
104 inputParameters: {
105 type: 'object',
106 properties: { filter: { $ref: '#/$defs/UnknownFilter' } },
107 required: ['filter'],
108 },
109 outputParameters: { type: 'object', properties: { ok: { type: 'boolean' } } },
110 });
111
112 const resolvableRefTool = makeTool({
113 slug: 'RESOLVABLE_REF_TOOL',
114 name: 'Resolvable Ref Tool',
115 inputParameters: {
116 type: 'object',
117 properties: { user: { $ref: '#/$defs/User' } },
118 required: ['user'],
119 $defs: {
120 User: { type: 'object', properties: { id: { type: 'string' } }, required: ['id'] },
121 },
122 },
123 outputParameters: { type: 'object', properties: { ok: { type: 'boolean' } } },
124 });
125
126 describe('MastraProvider: dangling $ref tolerance', () => {
127 let provider: MastraProvider;
128 let warnSpy: ReturnType<typeof vi.spyOn>;
129 let telemetrySpy: ReturnType<typeof vi.spyOn>;
130 let exec: ExecuteToolFn;
131
132 beforeEach(() => {
133 provider = new MastraProvider();
134 const mockExec = vi.fn<Parameters<ExecuteToolFn>, ReturnType<ExecuteToolFn>>(
135 async () => ({ data: {}, error: null, successful: true }) as unknown as ToolExecuteResponse
136 );
137 exec = mockExec;
138 provider._setExecuteToolFn(exec);
139 warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {});
140 telemetrySpy = vi.spyOn(telemetry, 'sendMetric').mockResolvedValue(undefined);
141 });
142
143 afterEach(() => {
144 vi.restoreAllMocks();
145 });
146
147 it('does not throw when outputParameters has a $ref with no $defs declared (GMAIL_FETCH_EMAILS shape)', () => {
148 expect(() => provider.wrapTool(danglingOutputTool, exec)).not.toThrow();
149 });
150
151 it('produces a defined outputSchema with the dangling branch replaced by a permissive object', () => {
152 const wrapped = provider.wrapTool(danglingOutputTool, exec) as {
153 outputSchema: unknown;
154 };
155 expect(wrapped.outputSchema).toBeDefined();
156 // The wrapped schema went through applyCompatLayer (JSON Schema → Zod →
157 // JSON Schema). All we can guarantee at this level is no surviving
158 // $ref string anywhere in the structure.
159 expect(JSON.stringify(unwrapSchema(wrapped.outputSchema))).not.toContain('$ref');
160 });
161
162 it('emits exactly one logger.warn per (toolSlug, ref) pair, even when the same tool is wrapped multiple times', () => {
163 provider.wrapTool(danglingOutputTool, exec);
164 provider.wrapTool(danglingOutputTool, exec);
165 provider.wrapTool(danglingOutputTool, exec);
166
167 const calls = warnSpy.mock.calls.filter(
168 ([msg]) => typeof msg === 'string' && msg.includes('#/$defs/FetchEmailsResponse')
169 );
170 expect(calls).toHaveLength(1);
171 const message = calls[0][0] as string;
172 // User-controlled segments are JSON.stringified — they appear quoted in
173 // the warn output (CWE-117 mitigation; see warnDanglingRefOnce).
174 expect(message).toContain('"DANGLING_OUTPUT_TOOL"');
175 expect(message).toContain('"dangling"');
176 expect(message).toContain('"#/$defs/FetchEmailsResponse"');
177 expect(message).toContain('github.com/ComposioHQ/composio/issues/3307');
178 });
179
180 it('emits a separate warning for a different tool slug carrying the same ref', () => {
181 const sameRefDifferentSlug = makeTool({
182 ...danglingOutputTool,
183 slug: 'OTHER_DANGLING_OUTPUT_TOOL',
184 });
185 provider.wrapTool(danglingOutputTool, exec);
186 provider.wrapTool(sameRefDifferentSlug, exec);
187
188 const calls = warnSpy.mock.calls.filter(
189 ([msg]) => typeof msg === 'string' && msg.includes('#/$defs/FetchEmailsResponse')
190 );
191 expect(calls).toHaveLength(2);
192 });
193
194 it('tolerates a dangling $ref on inputParameters (symmetric with output)', () => {
195 expect(() => provider.wrapTool(danglingInputTool, exec)).not.toThrow();
196 const calls = warnSpy.mock.calls.filter(
197 ([msg]) => typeof msg === 'string' && msg.includes('#/$defs/UnknownFilter')
198 );
199 expect(calls).toHaveLength(1);
200 });
201
202 it('sanitizes control bytes / newlines / ANSI escapes in the warning (CWE-117 regression)', () => {
203 // An upstream-shaped tool whose $ref carries a newline + ANSI CSI sequence.
204 // Without JSON.stringify on the interpolated segments, these bytes would
205 // be written verbatim to stderr, forging a fake log line.
206 const malicious = makeTool({
207 slug: 'MALICIOUS_TOOL',
208 outputParameters: {
209 type: 'object',
210 properties: { data: { $ref: '#/$defs/Foo\n\x1b[31mFAKE LOG LINE\x1b[0m' } },
211 },
212 });
213 provider.wrapTool(malicious, exec);
214 const calls = warnSpy.mock.calls.filter(
215 ([msg]) => typeof msg === 'string' && msg.includes('MALICIOUS_TOOL')
216 );
217 expect(calls).toHaveLength(1);
218 const message = calls[0][0] as string;
219 // Raw newline / escape bytes must NOT survive into the warning string.
220 expect(message).not.toMatch(/[\x00-\x09\x0b-\x1f]/);
221 // Escapes appear in their JSON-encoded form instead.
222 expect(message).toContain('\\n');
223 expect(message).toContain('\\u001b');
224 });
225
226 it('fires one telemetry event per (toolSlug, ref) pair next to the warn', () => {
227 provider.wrapTool(danglingOutputTool, exec);
228 provider.wrapTool(danglingOutputTool, exec); // dedup: still 1 event
229
230 const calls = telemetrySpy.mock.calls.filter(([payload]) => {
231 const events = payload as Array<{ functionName: string; props?: Record<string, unknown> }>;
232 return events.some(e => e.functionName === 'composio.mastra.wrapTool.danglingRef');
233 });
234 expect(calls).toHaveLength(1);
235
236 const event = (calls[0][0] as Array<Record<string, unknown>>)[0];
237 expect(event.functionName).toBe('composio.mastra.wrapTool.danglingRef');
238 expect(event.props).toMatchObject({
239 toolSlug: 'DANGLING_OUTPUT_TOOL',
240 toolkitSlug: 'dangling',
241 ref: '#/$defs/FetchEmailsResponse',
242 reason: 'missing-target',
243 });
244 expect(event.metadata).toEqual({ provider: 'mastra' });
245 });
246
247 it('does NOT fire telemetry for tools whose $refs all resolve', () => {
248 provider.wrapTool(resolvableRefTool, exec);
249 const calls = telemetrySpy.mock.calls.filter(([payload]) => {
250 const events = payload as Array<{ functionName: string }>;
251 return events.some(e => e.functionName === 'composio.mastra.wrapTool.danglingRef');
252 });
253 expect(calls).toHaveLength(0);
254 });
255
256 it('preserves resolvable $defs (regression guard — no degraded permissive anyOf)', () => {
257 const wrapped = provider.wrapTool(resolvableRefTool, exec) as { inputSchema: unknown };
258 // Resolvable $ref should produce a real schema with the User shape inlined.
259 const inputJson = JSON.stringify(unwrapSchema(wrapped.inputSchema));
260 expect(inputJson).toContain('id');
261 expect(inputJson).not.toContain('$ref');
262 // No warning should be emitted for resolvable refs.
263 const calls = warnSpy.mock.calls.filter(
264 ([msg]) => typeof msg === 'string' && msg.includes('$ref')
265 );
266 expect(calls).toHaveLength(0);
267 });
268 });