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.

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

Saransh Rana961b0a041877
diff --git a/ts/packages/ts-builders/src/PropertyValue.test.ts b/ts/packages/ts-builders/src/PropertyValue.test.ts
new file mode 100644
index 000000000..63115fa4a
--- /dev/null
+++ b/ts/packages/ts-builders/src/PropertyValue.test.ts
@@ -0,0 +1,60 @@
+import { expect, test } from 'vitest';
+
+import { objectValue } from './ObjectValue';
+import { propertyValue } from './PropertyValue';
+import { stringify } from './stringify';
+import { stringLiteral } from './StringLiteralType';
+import { toStringTag } from './WellKnownSymbol';
+
+test('name and value', () => {
+ const prop = propertyValue('SEND', stringLiteral('SLACK_SEND').asValue());
+
+ expect(stringify(prop)).toMatchInlineSnapshot(`"SEND: "SLACK_SEND""`);
+});
+
+test('well-known symbol', () => {
+ const prop = propertyValue(toStringTag, stringLiteral('x').asValue());
+
+ expect(stringify(prop)).toMatchInlineSnapshot(`"[Symbol.toStringTag]: "x""`);
+});
+
+test('optional', () => {
+ const prop = propertyValue('SEND', stringLiteral('x').asValue()).optional();
+
+ expect(stringify(prop)).toMatchInlineSnapshot(`"SEND?: "x""`);
+});
+
+// A slug reaches this builder as an object-literal key. Written verbatim it can
+// close the key and inject code that runs when the generated SDK is imported.
+// Non-identifier names must become quoted computed keys, as Property already does.
+test('a crafted slug cannot break out of the key position', () => {
+ const malicious = '["k"]: (() => { globalThis.__PWNED__ = 1; })(), ["SEND"]';
+
+ const out = stringify(propertyValue(malicious, stringLiteral('SLACK_SEND').asValue()));
+
+ expect(out).toBe(`[${JSON.stringify(malicious)}]: "SLACK_SEND"`);
+
+ // The payload text survives inside the quoted key; what matters is that
+ // evaluating the generated literal does not execute it.
+ const evaluated = new Function(`return { ${out} };`)() as Record<string, unknown>;
+ expect(Object.keys(evaluated)).toEqual([malicious]);
+ expect((globalThis as Record<string, unknown>).__PWNED__).toBeUndefined();
+});
+
+test('injected slug stays inert inside a generated object literal', () => {
+ const malicious = 'a": 1, "__proto__';
+
+ const out = stringify(
+ objectValue()
+ .add(propertyValue(malicious, stringLiteral('v').asValue()))
+ .formatInline()
+ );
+
+ expect(out).toBe(`{ [${JSON.stringify(malicious)}]: "v" }`);
+});
+
+test('a slug that is not a valid identifier is quoted, not dropped', () => {
+ const out = stringify(propertyValue('123FOO', stringLiteral('T_123FOO').asValue()));
+
+ expect(out).toBe('["123FOO"]: "T_123FOO"');
+});
diff --git a/ts/packages/ts-builders/src/PropertyValue.ts b/ts/packages/ts-builders/src/PropertyValue.ts
index 12050fc19..47d92b887 100644
--- a/ts/packages/ts-builders/src/PropertyValue.ts
+++ b/ts/packages/ts-builders/src/PropertyValue.ts
@@ -1,4 +1,5 @@
import { DocComment } from './DocComment';
+import { isValidJsIdentifier } from './isValidJsIdentifier';
import { WellKnownSymbol } from './WellKnownSymbol';
import { ValueBuilder } from './ValueBuilder';
import { Writer } from './Writer';
@@ -29,7 +30,11 @@ export class PropertyValue implements BasicBuilder {
}
if (typeof this.name === 'string') {
- writer.write(this.name);
+ if (isValidJsIdentifier(this.name)) {
+ writer.write(this.name);
+ } else {
+ writer.write('[').write(JSON.stringify(this.name)).write(']');
+ }
} else {
writer.write('[').write(this.name).write(']');
}
diff --git a/ts/packages/ts-builders/src/TypeDeclaration.test.ts b/ts/packages/ts-builders/src/TypeDeclaration.test.ts
index a2544ef9f..2f5103400 100644
--- a/ts/packages/ts-builders/src/TypeDeclaration.test.ts
+++ b/ts/packages/ts-builders/src/TypeDeclaration.test.ts
@@ -33,3 +33,26 @@ test('with multiple generic parameters', () => {
.addGenericParameter(genericParameter('U'));
expect(stringify(decl)).toMatchInlineSnapshot(`"type B<T, U> = A"`);
});
+
+// A slug reaches this builder as a type-declaration name. Written verbatim it can
+// terminate the declaration and inject top-level statements that run at import.
+// A type name has no computed-key form, so the only safe response is to refuse.
+test('refuses a type name that is not a valid identifier', () => {
+ const malicious = 'X = any;\nconst __pwned = (() => { globalThis.__PWNED__ = 1; })();\ntype Y';
+
+ expect(() => stringify(typeDeclaration(malicious, A))).toThrow(
+ /not a valid TypeScript identifier/
+ );
+});
+
+test('refuses a type name with a dash', () => {
+ expect(() => stringify(typeDeclaration('FOO-BAR', A))).toThrow(
+ /not a valid TypeScript identifier/
+ );
+});
+
+test('a string type body still short-circuits without touching the name', () => {
+ expect(stringify(typeDeclaration('B', 'export type B = A'))).toMatchInlineSnapshot(
+ `"export type B = A"`
+ );
+});
diff --git a/ts/packages/ts-builders/src/TypeDeclaration.ts b/ts/packages/ts-builders/src/TypeDeclaration.ts
index 88fb72aae..5227728ea 100644
--- a/ts/packages/ts-builders/src/TypeDeclaration.ts
+++ b/ts/packages/ts-builders/src/TypeDeclaration.ts
@@ -1,6 +1,7 @@
import type { BasicBuilder } from './BasicBuilder';
import { DocComment } from './DocComment';
import { GenericParameter } from './GenericParameter';
+import { isValidJsIdentifier } from './isValidJsIdentifier';
import { TypeBuilder } from './TypeBuilder';
import { Writer } from './Writer';
@@ -42,6 +43,13 @@ export class TypeDeclaration<InnerType extends TypeBuilder = TypeBuilder> implem
return;
}
+ // A type name has no quoted form, so a non-identifier cannot be emitted safely.
+ if (!isValidJsIdentifier(this.name)) {
+ throw new TypeError(
+ `Type declaration name is not a valid TypeScript identifier: ${JSON.stringify(this.name)}`
+ );
+ }
+
writer.write('type ').write(this.name);
if (this.genericParameters.length > 0) {
writer.write('<').writeJoined(', ', this.genericParameters).write('>');