Skip to content

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

Alberto Schiabel0fb479b8f1a3
diff --git a/ts/packages/cli/CHANGELOG.md b/ts/packages/cli/CHANGELOG.md
index 1386b6969..867680763 100644
--- a/ts/packages/cli/CHANGELOG.md
+++ b/ts/packages/cli/CHANGELOG.md
@@ -4,6 +4,9 @@
### Patch Changes
+- `composio generate` now keeps tool and trigger metadata as inert data in
+ generated Python and TypeScript sources. Crafted slugs and descriptions can
+ no longer inject code into generated modules.
- `composio upgrade` (and any other command with a long spinner message) no
longer scrolls endlessly in terminals narrower than that message. The spinner
printed its line hundreds of times instead of animating in place; live spinner
diff --git a/ts/packages/cli/src/generation/python/generate-toolkit-sources.ts b/ts/packages/cli/src/generation/python/generate-toolkit-sources.ts
index e6bbd8cde..f0a4cab3a 100644
--- a/ts/packages/cli/src/generation/python/generate-toolkit-sources.ts
+++ b/ts/packages/cli/src/generation/python/generate-toolkit-sources.ts
@@ -91,6 +91,33 @@ function jsToPython(value: unknown, indent = 0): string {
return JSON.stringify(value);
}
+const PYTHON_IDENTIFIER_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
+
+function orderPythonAssignmentEntries<T>(entries: Array<[string, T]>): Array<[string, T]> {
+ const fallbackEntries: Array<[string, T]> = [];
+ const identifierEntries: Array<[string, T]> = [];
+
+ for (const entry of entries) {
+ if (PYTHON_IDENTIFIER_PATTERN.test(entry[0])) {
+ identifierEntries.push(entry);
+ } else {
+ fallbackEntries.push(entry);
+ }
+ }
+
+ return [...fallbackEntries, ...identifierEntries];
+}
+
+function generatePythonAssignment(name: string, valueLiteral: string, spacing: number): string {
+ const spacePad = ' '.repeat(spacing);
+
+ if (PYTHON_IDENTIFIER_PATTERN.test(name)) {
+ return `${spacePad}${name} = ${valueLiteral}`;
+ }
+
+ return `${spacePad}locals()[${jsToPython(name)}] = ${valueLiteral}`;
+}
+
/**
* Generates Python dictionary syntax for a trigger type object
*/
@@ -118,8 +145,6 @@ function generatePythonToolkitSource(banner: string) {
const filename = `${toolkit.slug}.py`;
const toolsEntries = (spacing: number) => {
- const spacePad = ' '.repeat(spacing);
-
if (Object.keys(toolkit.typeableTools.value).length > 0) {
const tools = Match.value(toolkit.typeableTools).pipe(
Match.when({ withTypes: true }, ({ value }) =>
@@ -131,27 +156,27 @@ function generatePythonToolkitSource(banner: string) {
Match.exhaustive
);
- return Object.entries(tools)
- .map(([toolName, toolValue]) => `${spacePad}${toolName} = "${toolValue}"`)
+ return orderPythonAssignmentEntries(Object.entries(tools))
+ .map(([toolName, toolValue]) =>
+ generatePythonAssignment(toolName, jsToPython(toolValue), spacing)
+ )
.join('\n');
}
- return `${spacePad}pass`;
+ return `${' '.repeat(spacing)}pass`;
};
const triggerTypesEntries = (spacing: number) => {
- const spacePad = ' '.repeat(spacing);
-
if (Record.size(toolkit.triggerTypes) > 0) {
- return Object.entries(toolkit.triggerTypes)
+ return orderPythonAssignmentEntries(Object.entries(toolkit.triggerTypes))
.map(([triggerName, triggerValue]) => {
const pythonDict = generateTriggerTypePythonDict(triggerValue, spacing);
- return `${spacePad}${triggerName} = ${pythonDict}`;
+ return generatePythonAssignment(triggerName, pythonDict, spacing);
})
.join('\n');
}
- return `${spacePad}pass`;
+ return `${' '.repeat(spacing)}pass`;
};
// Build version comment if version override was used
diff --git a/ts/packages/cli/test/src/generation/python/generate-toolkit-source.test.ts b/ts/packages/cli/test/src/generation/python/generate-toolkit-source.test.ts
index b2cfd71c3..72c42c2b0 100644
--- a/ts/packages/cli/test/src/generation/python/generate-toolkit-source.test.ts
+++ b/ts/packages/cli/test/src/generation/python/generate-toolkit-source.test.ts
@@ -1,3 +1,4 @@
+import { spawnSync } from 'child_process';
import { describe, expect, it } from 'vitest';
import { generatePythonToolkitSources } from 'src/generation/python/generate-toolkit-sources';
import { createToolkitIndex } from 'src/generation/create-toolkit-index';
@@ -256,6 +257,69 @@ describe('generatePythonToolkitSources', () => {
assertPythonIsValid({ files: Object.fromEntries(sources) });
});
+ it('[Given] tool and trigger slugs containing Python source [Then] importing the module is safe', () => {
+ const toolkits = makeTestToolkits([
+ {
+ name: 'Gmail',
+ slug: 'gmail',
+ },
+ ]);
+ const maliciousToolSlug = 'GMAIL_x = "\'; __import__("os").system("echo compromised"); #';
+ const maliciousTriggerSlug = 'GMAIL_y = "\'; __import__("os").system("echo compromised"); #';
+
+ const index = createToolkitIndex({
+ toolkits,
+ typeableTools: { withTypes: false, tools: [maliciousToolSlug] },
+ triggerTypes: [{ ...TRIGGER_TYPES_GMAIL[0], slug: maliciousTriggerSlug }],
+ });
+
+ const sources = generatePythonToolkitSources(BANNER)(index);
+ const source = sources[0][1];
+
+ expect(source).toContain(
+ `locals()[${JSON.stringify(maliciousToolSlug.slice('GMAIL_'.length))}] = ${JSON.stringify(maliciousToolSlug)}`
+ );
+ expect(source).toContain(
+ `locals()[${JSON.stringify(maliciousTriggerSlug.slice('GMAIL_'.length))}] = `
+ );
+ assertPythonIsValid({ files: Object.fromEntries(sources) });
+
+ const result = spawnSync('python3', ['-c', source], { encoding: 'utf8' });
+ expect(result.stderr).toBe('');
+ expect(result.stdout).toBe('');
+ expect(result.status).toBe(0);
+ });
+
+ it('[Given] a locals slug before a fallback assignment [Then] importing the module succeeds', () => {
+ const toolkits = makeTestToolkits([
+ {
+ name: 'Gmail',
+ slug: 'gmail',
+ },
+ ]);
+ const localsSlug = 'GMAIL_locals';
+ const fallbackSlug = 'GMAIL_bad-name';
+
+ const index = createToolkitIndex({
+ toolkits,
+ typeableTools: { withTypes: false, tools: [localsSlug, fallbackSlug] },
+ triggerTypes: [
+ { ...TRIGGER_TYPES_GMAIL[0], slug: localsSlug },
+ { ...TRIGGER_TYPES_GMAIL[0], slug: fallbackSlug },
+ ],
+ });
+
+ const sources = generatePythonToolkitSources(BANNER)(index);
+ const source = sources[0][1];
+
+ expect(source.indexOf('locals()["bad-name"]')).toBeLessThan(source.indexOf('locals ='));
+ assertPythonIsValid({ files: Object.fromEntries(sources) });
+
+ const result = spawnSync('python3', ['-c', source], { encoding: 'utf8' });
+ expect(result.stderr).toBe('');
+ expect(result.status).toBe(0);
+ });
+
describe('[Given] versionMap with toolkit version overrides', () => {
describe('without type tools', () => {
it('[Given] a toolkit with version override [Then] it includes version comment in generated file', () => {
diff --git a/ts/packages/cli/test/src/generation/typescript/generate-toolkit-source.test.ts b/ts/packages/cli/test/src/generation/typescript/generate-toolkit-source.test.ts
index ae1272a46..741b23ba7 100644
--- a/ts/packages/cli/test/src/generation/typescript/generate-toolkit-source.test.ts
+++ b/ts/packages/cli/test/src/generation/typescript/generate-toolkit-source.test.ts
@@ -12,6 +12,7 @@ import { TOOLS_TYPES_GMAIL } from 'test/__mocks__/tools-types-gmail';
import { TRIGGER_TYPES_GITHUB } from 'test/__mocks__/trigger-types-github';
import { TRIGGER_TYPES_GMAIL } from 'test/__mocks__/trigger-types-gmail';
import { TRIGGER_TYPES_GOOGLEDRIVE } from 'test/__mocks__/trigger-types-googledrive';
+import { assertTypeScriptIsValid } from 'test/__utils__/typescript-compiler';
describe('generateTypeScriptToolkitSources', () => {
describe('with a single emitted file', () => {
@@ -83,6 +84,36 @@ describe('generateTypeScriptToolkitSources', () => {
})
);
+ it.effect(
+ '[Given] a tool description containing a comment terminator [Then] it remains inside the comment',
+ Effect.fn(function* () {
+ const toolkits = makeTestToolkits([
+ {
+ name: 'Github',
+ slug: 'github',
+ },
+ ]);
+ const maliciousDescription =
+ 'Sends an email. */ [(globalThis.compromised = true)]: 1, /*';
+ const maliciousTool = {
+ ...TOOLS_TYPES_GITHUB[0],
+ description: maliciousDescription,
+ };
+
+ const index = createToolkitIndex({
+ toolkits,
+ typeableTools: { withTypes: true, tools: [maliciousTool] },
+ triggerTypes: [],
+ });
+
+ const sources = yield* generateTypeScriptToolkitSources(banner)(index);
+ const source = sources[0][1];
+
+ expect(source).toContain('Sends an email. *\\/');
+ assertTypeScriptIsValid({ files: { 'github.ts': source } });
+ })
+ );
+
describe('without type tools', () => {
const withTypeTools = false;
diff --git a/ts/packages/ts-builders/src/DocComment.test.ts b/ts/packages/ts-builders/src/DocComment.test.ts
index 378cee073..b5c3d4e9c 100644
--- a/ts/packages/ts-builders/src/DocComment.test.ts
+++ b/ts/packages/ts-builders/src/DocComment.test.ts
@@ -42,6 +42,16 @@ test('multiple addText calls', () => {
`);
});
+test('closing comment delimiters remain inside the comment', () => {
+ expect(stringify(docComment('Safe text */ globalThis.compromised = true')))
+ .toMatchInlineSnapshot(`
+ "/**
+ * Safe text *\\/ globalThis.compromised = true
+ */
+ "
+ `);
+});
+
test('tagged template - empty', () => {
expect(stringify(docComment``)).toMatchInlineSnapshot(`
"/**
diff --git a/ts/packages/ts-builders/src/DocComment.ts b/ts/packages/ts-builders/src/DocComment.ts
index 6c94d3fd7..3b78b9e1e 100644
--- a/ts/packages/ts-builders/src/DocComment.ts
+++ b/ts/packages/ts-builders/src/DocComment.ts
@@ -18,7 +18,7 @@ export class DocComment implements BasicBuilder {
write(writer: Writer) {
writer.writeLine('/**');
for (const line of this.lines) {
- writer.writeLine(` * ${line}`);
+ writer.writeLine(` * ${line.replaceAll('*/', '*\\/')}`);
}
writer.writeLine(' */');
return writer;