Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Merge commit from fork
* fix(cli): escape generated source metadata * test(cli): execute generated Python regression * fix(cli): order Python fallback assignments --------- Co-authored-by: jkomyno <12381818+jkomyno@users.noreply.github.com>
Alberto Schiabel committed
Sep 8, 2026 at 14:59 UTC
0fb479b8f1a328b4fad7535c2c3a6db72028b7ad
6 files changed
+144
-11
ts/packages/cli/CHANGELOG.md
+3
@@ -4,6 +4,9 @@
4
5
### Patch Changes
6
7
+- `composio generate` now keeps tool and trigger metadata as inert data in
8
+ generated Python and TypeScript sources. Crafted slugs and descriptions can
9
+ no longer inject code into generated modules.
10
- `composio upgrade` (and any other command with a long spinner message) no
11
longer scrolls endlessly in terminals narrower than that message. The spinner
12
printed its line hundreds of times instead of animating in place; live spinner
ts/packages/cli/src/generation/python/generate-toolkit-sources.ts
+35
-10
@@ -91,6 +91,33 @@ function jsToPython(value: unknown, indent = 0): string {
91
return JSON.stringify(value);
92
}
93
94
+const PYTHON_IDENTIFIER_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
95
+
96
+function orderPythonAssignmentEntries<T>(entries: Array<[string, T]>): Array<[string, T]> {
97
+ const fallbackEntries: Array<[string, T]> = [];
98
+ const identifierEntries: Array<[string, T]> = [];
99
+
100
+ for (const entry of entries) {
101
+ if (PYTHON_IDENTIFIER_PATTERN.test(entry[0])) {
102
+ identifierEntries.push(entry);
103
+ } else {
104
+ fallbackEntries.push(entry);
105
+ }
106
+ }
107
+
108
+ return [...fallbackEntries, ...identifierEntries];
109
+}
110
+
111
+function generatePythonAssignment(name: string, valueLiteral: string, spacing: number): string {
112
+ const spacePad = ' '.repeat(spacing);
113
+
114
+ if (PYTHON_IDENTIFIER_PATTERN.test(name)) {
115
+ return `${spacePad}${name} = ${valueLiteral}`;
116
+ }
117
+
118
+ return `${spacePad}locals()[${jsToPython(name)}] = ${valueLiteral}`;
119
+}
120
+
121
/**
122
* Generates Python dictionary syntax for a trigger type object
123
*/
@@ -118,8 +145,6 @@ function generatePythonToolkitSource(banner: string) {
145
const filename = `${toolkit.slug}.py`;
146
147
const toolsEntries = (spacing: number) => {
121
- const spacePad = ' '.repeat(spacing);
122
-
148
if (Object.keys(toolkit.typeableTools.value).length > 0) {
149
const tools = Match.value(toolkit.typeableTools).pipe(
150
Match.when({ withTypes: true }, ({ value }) =>
@@ -131,27 +156,27 @@ function generatePythonToolkitSource(banner: string) {
156
Match.exhaustive
157
);
158
134
- return Object.entries(tools)
135
- .map(([toolName, toolValue]) => `${spacePad}${toolName} = "${toolValue}"`)
159
+ return orderPythonAssignmentEntries(Object.entries(tools))
160
+ .map(([toolName, toolValue]) =>
161
+ generatePythonAssignment(toolName, jsToPython(toolValue), spacing)
162
+ )
163
.join('\n');
164
}
165
139
- return `${spacePad}pass`;
166
+ return `${' '.repeat(spacing)}pass`;
167
};
168
169
const triggerTypesEntries = (spacing: number) => {
143
- const spacePad = ' '.repeat(spacing);
144
-
170
if (Record.size(toolkit.triggerTypes) > 0) {
146
- return Object.entries(toolkit.triggerTypes)
171
+ return orderPythonAssignmentEntries(Object.entries(toolkit.triggerTypes))
172
.map(([triggerName, triggerValue]) => {
173
const pythonDict = generateTriggerTypePythonDict(triggerValue, spacing);
149
- return `${spacePad}${triggerName} = ${pythonDict}`;
174
+ return generatePythonAssignment(triggerName, pythonDict, spacing);
175
})
176
.join('\n');
177
}
178
154
- return `${spacePad}pass`;
179
+ return `${' '.repeat(spacing)}pass`;
180
};
181
182
// Build version comment if version override was used
ts/packages/cli/test/src/generation/python/generate-toolkit-source.test.ts
+64
@@ -1,3 +1,4 @@
1
+import { spawnSync } from 'child_process';
2
import { describe, expect, it } from 'vitest';
3
import { generatePythonToolkitSources } from 'src/generation/python/generate-toolkit-sources';
4
import { createToolkitIndex } from 'src/generation/create-toolkit-index';
@@ -256,6 +257,69 @@ describe('generatePythonToolkitSources', () => {
257
assertPythonIsValid({ files: Object.fromEntries(sources) });
258
});
259
260
+ it('[Given] tool and trigger slugs containing Python source [Then] importing the module is safe', () => {
261
+ const toolkits = makeTestToolkits([
262
+ {
263
+ name: 'Gmail',
264
+ slug: 'gmail',
265
+ },
266
+ ]);
267
+ const maliciousToolSlug = 'GMAIL_x = "\'; __import__("os").system("echo compromised"); #';
268
+ const maliciousTriggerSlug = 'GMAIL_y = "\'; __import__("os").system("echo compromised"); #';
269
+
270
+ const index = createToolkitIndex({
271
+ toolkits,
272
+ typeableTools: { withTypes: false, tools: [maliciousToolSlug] },
273
+ triggerTypes: [{ ...TRIGGER_TYPES_GMAIL[0], slug: maliciousTriggerSlug }],
274
+ });
275
+
276
+ const sources = generatePythonToolkitSources(BANNER)(index);
277
+ const source = sources[0][1];
278
+
279
+ expect(source).toContain(
280
+ `locals()[${JSON.stringify(maliciousToolSlug.slice('GMAIL_'.length))}] = ${JSON.stringify(maliciousToolSlug)}`
281
+ );
282
+ expect(source).toContain(
283
+ `locals()[${JSON.stringify(maliciousTriggerSlug.slice('GMAIL_'.length))}] = `
284
+ );
285
+ assertPythonIsValid({ files: Object.fromEntries(sources) });
286
+
287
+ const result = spawnSync('python3', ['-c', source], { encoding: 'utf8' });
288
+ expect(result.stderr).toBe('');
289
+ expect(result.stdout).toBe('');
290
+ expect(result.status).toBe(0);
291
+ });
292
+
293
+ it('[Given] a locals slug before a fallback assignment [Then] importing the module succeeds', () => {
294
+ const toolkits = makeTestToolkits([
295
+ {
296
+ name: 'Gmail',
297
+ slug: 'gmail',
298
+ },
299
+ ]);
300
+ const localsSlug = 'GMAIL_locals';
301
+ const fallbackSlug = 'GMAIL_bad-name';
302
+
303
+ const index = createToolkitIndex({
304
+ toolkits,
305
+ typeableTools: { withTypes: false, tools: [localsSlug, fallbackSlug] },
306
+ triggerTypes: [
307
+ { ...TRIGGER_TYPES_GMAIL[0], slug: localsSlug },
308
+ { ...TRIGGER_TYPES_GMAIL[0], slug: fallbackSlug },
309
+ ],
310
+ });
311
+
312
+ const sources = generatePythonToolkitSources(BANNER)(index);
313
+ const source = sources[0][1];
314
+
315
+ expect(source.indexOf('locals()["bad-name"]')).toBeLessThan(source.indexOf('locals ='));
316
+ assertPythonIsValid({ files: Object.fromEntries(sources) });
317
+
318
+ const result = spawnSync('python3', ['-c', source], { encoding: 'utf8' });
319
+ expect(result.stderr).toBe('');
320
+ expect(result.status).toBe(0);
321
+ });
322
+
323
describe('[Given] versionMap with toolkit version overrides', () => {
324
describe('without type tools', () => {
325
it('[Given] a toolkit with version override [Then] it includes version comment in generated file', () => {
ts/packages/cli/test/src/generation/typescript/generate-toolkit-source.test.ts
+31
@@ -12,6 +12,7 @@ import { TOOLS_TYPES_GMAIL } from 'test/__mocks__/tools-types-gmail';
12
import { TRIGGER_TYPES_GITHUB } from 'test/__mocks__/trigger-types-github';
13
import { TRIGGER_TYPES_GMAIL } from 'test/__mocks__/trigger-types-gmail';
14
import { TRIGGER_TYPES_GOOGLEDRIVE } from 'test/__mocks__/trigger-types-googledrive';
15
+import { assertTypeScriptIsValid } from 'test/__utils__/typescript-compiler';
16
17
describe('generateTypeScriptToolkitSources', () => {
18
describe('with a single emitted file', () => {
@@ -83,6 +84,36 @@ describe('generateTypeScriptToolkitSources', () => {
84
})
85
);
86
87
+ it.effect(
88
+ '[Given] a tool description containing a comment terminator [Then] it remains inside the comment',
89
+ Effect.fn(function* () {
90
+ const toolkits = makeTestToolkits([
91
+ {
92
+ name: 'Github',
93
+ slug: 'github',
94
+ },
95
+ ]);
96
+ const maliciousDescription =
97
+ 'Sends an email. */ [(globalThis.compromised = true)]: 1, /*';
98
+ const maliciousTool = {
99
+ ...TOOLS_TYPES_GITHUB[0],
100
+ description: maliciousDescription,
101
+ };
102
+
103
+ const index = createToolkitIndex({
104
+ toolkits,
105
+ typeableTools: { withTypes: true, tools: [maliciousTool] },
106
+ triggerTypes: [],
107
+ });
108
+
109
+ const sources = yield* generateTypeScriptToolkitSources(banner)(index);
110
+ const source = sources[0][1];
111
+
112
+ expect(source).toContain('Sends an email. *\\/');
113
+ assertTypeScriptIsValid({ files: { 'github.ts': source } });
114
+ })
115
+ );
116
+
117
describe('without type tools', () => {
118
const withTypeTools = false;
119
ts/packages/ts-builders/src/DocComment.test.ts
+10
@@ -42,6 +42,16 @@ test('multiple addText calls', () => {
42
`);
43
});
44
45
+test('closing comment delimiters remain inside the comment', () => {
46
+ expect(stringify(docComment('Safe text */ globalThis.compromised = true')))
47
+ .toMatchInlineSnapshot(`
48
+ "/**
49
+ * Safe text *\\/ globalThis.compromised = true
50
+ */
51
+ "
52
+ `);
53
+});
54
+
55
test('tagged template - empty', () => {
56
expect(stringify(docComment``)).toMatchInlineSnapshot(`
57
"/**
ts/packages/ts-builders/src/DocComment.ts
+1
-1
@@ -18,7 +18,7 @@ export class DocComment implements BasicBuilder {
18
write(writer: Writer) {
19
writer.writeLine('/**');
20
for (const line of this.lines) {
21
- writer.writeLine(` * ${line}`);
21
+ writer.writeLine(` * ${line.replaceAll('*/', '*\\/')}`);
22
}
23
writer.writeLine(' */');
24
return writer;