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

next
ts 60 lines 2.23 KB
Raw
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 });