@devries-cameron20 / composio Mirror · read-only / commits / 961b0a041
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, 13:30 UTC

security: fix Parameter finding #91 (CWE-94) - identifier guard in ts-builders codegen (#4324)

## What the vulnerability was The CLI's TypeScript generator wrote API-supplied tool and trigger slugs straight into generated `.ts` files at two places where the slug becomes **code**, not data: - **object-literal property keys** (`PropertyValue.write`), and - **type-declaration names** (`TypeDeclaration.write`). Slugs are unconstrained strings (`ToolAsEnum = Schema.String`, no character allowlist) and the threat model treats API responses as untrusted. So a crafted slug could close the key and inject arbitrary top-level TypeScript, which then executes when the generated SDK is imported or built. That is remote code execution on the developer or CI host running `composio ts generate`. The sibling `Property` builder already got this right: it tests `isValidJsIdentifier` and falls back to a JSON-encoded computed key. `PropertyValue` and `TypeDeclaration` simply lacked the same guard. ## Why this change addresses it - `PropertyValue` now applies the exact guard `Property` uses. A name that is not a valid identifier becomes a quoted computed key (`["..."]`), so everything it contains is parsed as a string, never as an expression. - `TypeDeclaration` refuses a non-identifier name with a `TypeError`. A type name has no quoted or computed form, so quoting is not available and failing loudly is the only safe option. The guard sits after the existing early return for a string type body, which never writes the name. Output is byte-identical for every name that is already a valid identifier, so no existing generation changes. The CLI codegen snapshots contain only identifier-shaped slugs (`GITHUB_ACCEPT_A_REPOSITORY_INVITATION_INPUT`) and no computed keys, and they are unchanged. Worth noting on the throw: the two sinks differ in exposure. `typeDeclaration` receives the full `tool.slug` (for example `GITHUB_FOO`), which is identifier-shaped in practice. `propertyValue` receives `stripPrefix(tool.slug)`, which can plausibly start with a digit for real data and now gets safely quoted rather than throwing. Any name that would now throw was already producing syntactically invalid TypeScript, so this converts a confusing downstream compile error into a clear codegen error. ## Tests **Exploit tests**, not behaviour-pinning. Nine new tests across the two files. The key one builds the generated object literal from a malicious slug, evaluates it, and asserts the injected IIFE did not run: ```ts const evaluated = new Function(`return { ${out} };`)() as Record<string, unknown>; expect(Object.keys(evaluated)).toEqual([malicious]); expect((globalThis as Record<string, unknown>).__PWNED__).toBeUndefined(); ``` Each test was confirmed to fail against the unpatched builders and pass after the change. On the vulnerable code the generator emits `["k"]: (() => { globalThis.__PWNED__ = 1; })(), ["SEND"]: "SLACK_SEND"`, which is the payload as live code. Existing behaviour is also pinned: valid identifier keys, well-known symbols, optional properties, generic parameters, doc comments, and the string-type-body short circuit. ## What was not verified - **No end-to-end run of `composio ts generate` against a real or malicious API response.** The fix and its tests are at the builder layer. The call sites in `ts/packages/cli/src/generation/typescript/generate-toolkit-sources.ts` were read to confirm they pass attacker-controlled slugs into these builders, but were not exercised. - **No model-layer slug validation was added.** The finding also suggests validating slugs against `^[A-Za-z0-9_]+$` at the model layer. That is deliberately out of scope here: it would change what the CLI accepts from the API and belongs in a separate, human-reviewed change. This PR closes the two injection sinks only. - **The `TypeDeclaration` throw is a new failure mode.** If any live toolkit ships a slug that is not a valid identifier, codegen will now fail loudly for it instead of emitting broken TypeScript. No such slug appears in the repo's fixtures, but the full set of production slugs was not enumerated. - Python codegen was not reviewed. This finding and fix are TypeScript-only. ## Testing performed Clean-HEAD baseline captured before any edit: full monorepo `pnpm run test` green, 26/26 turbo tasks, exit 0. After the change, the same full suite is green, 26/26 tasks, exit 0. `@composio/ts-builders` goes from 131 to 140 tests, all passing. `@composio/cli` unchanged at 1304 passed. 20 example packages validated. No new failures and no snapshot churn. ## Finding Parameter finding #91 (CWE-94), validated live at `abc8e038218305cab7f3373b82a37144fa15e630`. Linear: https://linear.app/composio/issue/SEC-579 parameter-finding: kcy19unq784bp2z34b3299w0 Co-authored-by: Saransh <saranshrana@Saranshs-MacBook-Pro.local> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>

Saransh Rana committed Sep 1, 2026 at 14:20 UTC 961b0a041877239f88de77880f687bd29b9d31b6
4 files changed +97 -1
ts/packages/ts-builders/src/PropertyValue.test.ts new
+60
@@ -0,0 +1,60 @@
1 +import { expect, test } from 'vitest';
2 +
3 +import { objectValue } from './ObjectValue';
4 +import { propertyValue } from './PropertyValue';
5 +import { stringify } from './stringify';
6 +import { stringLiteral } from './StringLiteralType';
7 +import { toStringTag } from './WellKnownSymbol';
8 +
9 +test('name and value', () => {
10 + const prop = propertyValue('SEND', stringLiteral('SLACK_SEND').asValue());
11 +
12 + expect(stringify(prop)).toMatchInlineSnapshot(`"SEND: "SLACK_SEND""`);
13 +});
14 +
15 +test('well-known symbol', () => {
16 + const prop = propertyValue(toStringTag, stringLiteral('x').asValue());
17 +
18 + expect(stringify(prop)).toMatchInlineSnapshot(`"[Symbol.toStringTag]: "x""`);
19 +});
20 +
21 +test('optional', () => {
22 + const prop = propertyValue('SEND', stringLiteral('x').asValue()).optional();
23 +
24 + expect(stringify(prop)).toMatchInlineSnapshot(`"SEND?: "x""`);
25 +});
26 +
27 +// A slug reaches this builder as an object-literal key. Written verbatim it can
28 +// close the key and inject code that runs when the generated SDK is imported.
29 +// Non-identifier names must become quoted computed keys, as Property already does.
30 +test('a crafted slug cannot break out of the key position', () => {
31 + const malicious = '["k"]: (() => { globalThis.__PWNED__ = 1; })(), ["SEND"]';
32 +
33 + const out = stringify(propertyValue(malicious, stringLiteral('SLACK_SEND').asValue()));
34 +
35 + expect(out).toBe(`[${JSON.stringify(malicious)}]: "SLACK_SEND"`);
36 +
37 + // The payload text survives inside the quoted key; what matters is that
38 + // evaluating the generated literal does not execute it.
39 + const evaluated = new Function(`return { ${out} };`)() as Record<string, unknown>;
40 + expect(Object.keys(evaluated)).toEqual([malicious]);
41 + expect((globalThis as Record<string, unknown>).__PWNED__).toBeUndefined();
42 +});
43 +
44 +test('injected slug stays inert inside a generated object literal', () => {
45 + const malicious = 'a": 1, "__proto__';
46 +
47 + const out = stringify(
48 + objectValue()
49 + .add(propertyValue(malicious, stringLiteral('v').asValue()))
50 + .formatInline()
51 + );
52 +
53 + expect(out).toBe(`{ [${JSON.stringify(malicious)}]: "v" }`);
54 +});
55 +
56 +test('a slug that is not a valid identifier is quoted, not dropped', () => {
57 + const out = stringify(propertyValue('123FOO', stringLiteral('T_123FOO').asValue()));
58 +
59 + expect(out).toBe('["123FOO"]: "T_123FOO"');
60 +});
ts/packages/ts-builders/src/PropertyValue.ts
+6 -1
@@ -1,4 +1,5 @@
1 import { DocComment } from './DocComment';
2 +import { isValidJsIdentifier } from './isValidJsIdentifier';
3 import { WellKnownSymbol } from './WellKnownSymbol';
4 import { ValueBuilder } from './ValueBuilder';
5 import { Writer } from './Writer';
@@ -29,7 +30,11 @@ export class PropertyValue implements BasicBuilder {
30 }
31
32 if (typeof this.name === 'string') {
32 - writer.write(this.name);
33 + if (isValidJsIdentifier(this.name)) {
34 + writer.write(this.name);
35 + } else {
36 + writer.write('[').write(JSON.stringify(this.name)).write(']');
37 + }
38 } else {
39 writer.write('[').write(this.name).write(']');
40 }
ts/packages/ts-builders/src/TypeDeclaration.test.ts
+23
@@ -33,3 +33,26 @@ test('with multiple generic parameters', () => {
33 .addGenericParameter(genericParameter('U'));
34 expect(stringify(decl)).toMatchInlineSnapshot(`"type B<T, U> = A"`);
35 });
36 +
37 +// A slug reaches this builder as a type-declaration name. Written verbatim it can
38 +// terminate the declaration and inject top-level statements that run at import.
39 +// A type name has no computed-key form, so the only safe response is to refuse.
40 +test('refuses a type name that is not a valid identifier', () => {
41 + const malicious = 'X = any;\nconst __pwned = (() => { globalThis.__PWNED__ = 1; })();\ntype Y';
42 +
43 + expect(() => stringify(typeDeclaration(malicious, A))).toThrow(
44 + /not a valid TypeScript identifier/
45 + );
46 +});
47 +
48 +test('refuses a type name with a dash', () => {
49 + expect(() => stringify(typeDeclaration('FOO-BAR', A))).toThrow(
50 + /not a valid TypeScript identifier/
51 + );
52 +});
53 +
54 +test('a string type body still short-circuits without touching the name', () => {
55 + expect(stringify(typeDeclaration('B', 'export type B = A'))).toMatchInlineSnapshot(
56 + `"export type B = A"`
57 + );
58 +});
ts/packages/ts-builders/src/TypeDeclaration.ts
+8
@@ -1,6 +1,7 @@
1 import type { BasicBuilder } from './BasicBuilder';
2 import { DocComment } from './DocComment';
3 import { GenericParameter } from './GenericParameter';
4 +import { isValidJsIdentifier } from './isValidJsIdentifier';
5 import { TypeBuilder } from './TypeBuilder';
6 import { Writer } from './Writer';
7
@@ -42,6 +43,13 @@ export class TypeDeclaration<InnerType extends TypeBuilder = TypeBuilder> implem
43 return;
44 }
45
46 + // A type name has no quoted form, so a non-identifier cannot be emitted safely.
47 + if (!isValidJsIdentifier(this.name)) {
48 + throw new TypeError(
49 + `Type declaration name is not a valid TypeScript identifier: ${JSON.stringify(this.name)}`
50 + );
51 + }
52 +
53 writer.write('type ').write(this.name);
54 if (this.genericParameters.length > 0) {
55 writer.write('<').writeJoined(', ', this.genericParameters).write('>');