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.

fix(cli): remove `new Function()` eval from JSON-ish input parsing (#3878)

Alberto Schiabel2dc9ef34d0d6
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 41a0d86bd..e3d24218e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1165,9 +1165,6 @@ importers:
'@zed-industries/codex-acp':
specifier: ^0.16.0
version: 0.16.0
- comment-json:
- specifier: ^5.0.0
- version: 5.0.0
effect:
specifier: 'catalog:'
version: 3.21.4
@@ -1180,6 +1177,9 @@ importers:
js-tiktoken:
specifier: ^1.0.21
version: 1.0.21
+ json5:
+ specifier: ^2.2.3
+ version: 2.2.3
open:
specifier: ^11.0.0
version: 11.0.0
@@ -4338,9 +4338,6 @@ packages:
resolution: {integrity: sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==}
- resolution: {integrity: sha512-/+3GRL7dDAGEfM6TseQk/U+mi18TU2Ms9I3UlLdUMhz2hbvGNTKdj9xniwXfUqgYhHxRx0+8UnKkvlNwVU+cWQ==}
-
resolution: {integrity: sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==}
engines: {node: '>=8'}
@@ -4534,10 +4531,6 @@ packages:
resolution: {integrity: sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==}
engines: {node: '>=14'}
- resolution: {integrity: sha512-uiqLcOiVDJtBP8WGkZHEP+FZIhTzP1dxvn59EfoYUi9gqupjrBWVQkO2atDrbnKPwLeotFYDsuNb26uBMqB+hw==}
- engines: {node: '>= 6'}
-
resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==}
@@ -5454,6 +5447,11 @@ packages:
resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==}
+ resolution: {integrity: sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==}
+ engines: {node: '>=6'}
+ hasBin: true
+
resolution: {integrity: sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==}
@@ -9894,8 +9892,6 @@ snapshots:
-
@@ -10086,11 +10082,6 @@ snapshots:
- dependencies:
- array-timsort: 1.0.3
- esprima: 4.0.1
-
@@ -10978,6 +10969,8 @@ snapshots:
+
optionalDependencies:
graceful-fs: 4.2.11
diff --git a/ts/packages/cli/package.json b/ts/packages/cli/package.json
index 6ad591c2f..016c7c10e 100644
--- a/ts/packages/cli/package.json
+++ b/ts/packages/cli/package.json
@@ -94,11 +94,11 @@
"@modelcontextprotocol/sdk": "catalog:",
"@zed-industries/claude-code-acp": "^0.16.2",
"@zed-industries/codex-acp": "^0.16.0",
- "comment-json": "^5.0.0",
"effect": "catalog:",
"extract-zip": "^2.0.1",
"indent-string": "^5.0.0",
"js-tiktoken": "^1.0.21",
+ "json5": "^2.2.3",
"open": "^11.0.0",
"openapi-typescript": "^7.13.0",
"picocolors": "catalog:",
diff --git a/ts/packages/cli/src/commands/listen.cmd.ts b/ts/packages/cli/src/commands/listen.cmd.ts
index 004ec5206..fbda3ff4f 100644
--- a/ts/packages/cli/src/commands/listen.cmd.ts
+++ b/ts/packages/cli/src/commands/listen.cmd.ts
@@ -1,7 +1,7 @@
import { Args, Command, Options } from '@effect/cli';
import { FileSystem } from '@effect/platform';
import type { Composio as RawComposioClient } from '@composio/client';
-import { Deferred, Effect, Option, Runtime } from 'effect';
+import { Deferred, Effect, Either, Option, Runtime } from 'effect';
import path from 'node:path';
import { requireAuth } from 'src/effects/require-auth';
import { resolveOptionalTextInput } from 'src/effects/resolve-optional-text-input';
@@ -20,7 +20,7 @@ import {
formatConnectedAccountChoices,
resolveConnectedAccountSelection,
} from 'src/services/connected-account-selection';
-import { parseJsonIsh } from 'src/utils/parse-json-ish';
+import { parseJsonRecord } from 'src/utils/parse-json';
import { toolkitFromToolSlug } from 'src/utils/toolkit-from-tool-slug';
import { ComposioCliUserConfig } from 'src/services/cli-user-config';
import { CLI_EXPERIMENTAL_FEATURES } from 'src/constants';
@@ -80,23 +80,16 @@ const resolveParamsInput = (input: Option.Option<string>) =>
resolveOptionalTextInput(input, { missingValue: '{}' });
const parseCreateParams = (raw: string) =>
- Effect.gen(function* () {
- const parsed = yield* Effect.try({
- try: () => parseJsonIsh(raw),
- catch: () =>
+ parseJsonRecord(raw).pipe(
+ Either.mapLeft(
+ error =>
new Error(
- "Invalid --params input. Provide JSON or a JS-style object literal, e.g. -p '{ trigger_config: { ... } }'."
- ),
- });
-
- if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
- return yield* Effect.fail(
- new Error("Expected --params to be an object, e.g. -p '{ trigger_config: { ... } }'.")
- );
- }
-
- return parsed as Record<string, unknown>;
- });
+ error.reason === 'not-a-record'
+ ? "Expected --params to be an object, e.g. -p '{ trigger_config: { ... } }'."
+ : "Invalid --params input. Provide JSON or a JS-style object literal, e.g. -p '{ trigger_config: { ... } }'."
+ )
+ )
+ );
const assertSupportedListenParams = (params: {
listeningToProjectEvent: boolean;
diff --git a/ts/packages/cli/src/commands/proxy.cmd.ts b/ts/packages/cli/src/commands/proxy.cmd.ts
index 9ae0d4e89..f6a12ff52 100644
--- a/ts/packages/cli/src/commands/proxy.cmd.ts
+++ b/ts/packages/cli/src/commands/proxy.cmd.ts
@@ -1,5 +1,5 @@
import { Args, Command, Options } from '@effect/cli';
-import { Effect, Option } from 'effect';
+import { Effect, Either, Option } from 'effect';
import type {
SessionProxyExecuteParams,
SessionProxyExecuteResponse,
@@ -21,7 +21,7 @@ import {
ComposioNoActiveConnectionError,
mapComposioError,
} from 'src/services/composio-error-overrides';
-import { parseJsonIsh } from 'src/utils/parse-json-ish';
+import { parseJsonRecord } from 'src/utils/parse-json';
const endpoint = Args.text({ name: 'url' }).pipe(
Args.withDescription('Absolute or relative API endpoint to call through proxy execute.')
@@ -94,13 +94,8 @@ export const parseProxyHeader = (value: string): { name: string; value: string }
const resolveBodyInput = (input: Option.Option<string>) => resolveOptionalTextInput(input);
-export const parseProxyBody = (raw: string): unknown => {
- try {
- return parseJsonIsh(raw);
- } catch {
- return raw;
- }
-};
+export const parseProxyBody = (raw: string): unknown =>
+ Either.getOrElse(parseJsonRecord(raw), (): unknown => raw);
const formatProxyOutput = (
result: Pick<SessionProxyExecuteResponse, 'status' | 'data' | 'headers' | 'binary_data'>
diff --git a/ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts b/ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts
index 275061780..4b3976c2f 100644
--- a/ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts
+++ b/ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts
@@ -5,7 +5,7 @@ import util from 'node:util';
import { Effect, Option, Either, Exit, Fiber, Cause } from 'effect';
import { encodingForModel } from 'js-tiktoken';
import { redact } from 'src/ui/redact';
-import { parseJsonIsh } from 'src/utils/parse-json-ish';
+import { parseJsonRecord } from 'src/utils/parse-json';
import { toolkitFromToolSlug } from 'src/utils/toolkit-from-tool-slug';
import { requireAuth } from 'src/effects/require-auth';
import { resolveOptionalTextInput } from 'src/effects/resolve-optional-text-input';
@@ -122,21 +122,16 @@ const resolveInput = (input: Option.Option<string>) =>
});
const parseArguments = (raw: string) =>
- Effect.gen(function* () {
- const parsed = yield* Effect.try({
- try: () => parseJsonIsh(raw),
- catch: () =>
+ parseJsonRecord(raw).pipe(
+ Either.mapLeft(
+ error =>
new Error(
- 'Invalid JSON input. Provide JSON or a JS-style object literal, e.g. -d \'{ "key": "value" }\''
- ),
- });
- if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
- return yield* Effect.fail(
- new Error('Expected a JSON object for tool arguments, e.g. -d \'{ "key": "value" }\'')
- );
- }
- return parsed as Record<string, unknown>;
- });
+ error.reason === 'not-a-record'
+ ? 'Expected a JSON object for tool arguments, e.g. -d \'{ "key": "value" }\''
+ : 'Invalid JSON input. Provide JSON or a JS-style object literal, e.g. -d \'{ "key": "value" }\''
+ )
+ )
+ );
const hasNestedKey = (
record: Record<string, unknown>,
@@ -705,8 +700,7 @@ class ToolExecutionError extends Error {
}
type CachedValidationDecision =
- | { readonly status: 'valid' | 'stale' }
- | { readonly status: 'fail'; readonly error: unknown };
+ { readonly status: 'valid' | 'stale' } | { readonly status: 'fail'; readonly error: unknown };
type ValidationState = {
readonly cacheHit: boolean;
diff --git a/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/index.ts b/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/index.ts
index eb80f3366..e574c9a6e 100644
--- a/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/index.ts
+++ b/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/index.ts
@@ -1,2 +1,2 @@
-export * from './json-parsing.error';
+export { JsonParsingError } from 'src/utils/parse-json';
export * from './read-json';
diff --git a/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/json-parsing.error.ts b/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/json-parsing.error.ts
deleted file mode 100644
index d5edd79bb..000000000
--- a/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/json-parsing.error.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { TaggedError } from 'effect/Data';
-
-export class JsonParsingError extends TaggedError('json-parsing-error')<{
- cause?: unknown;
- message?: string;
-}> {}
diff --git a/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/parse-json.ts b/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/parse-json.ts
deleted file mode 100644
index 320fe0a81..000000000
--- a/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/parse-json.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-import { parse } from 'comment-json';
-import { Effect, pipe } from 'effect';
-
-import { JsonParsingError } from './json-parsing.error';
-
-export const parseJson = (data: string) =>
- pipe(
- Effect.sync(() => parse(data, null, true)),
- Effect.catchAll(e =>
- Effect.fail(
- new JsonParsingError({
- cause: e,
- })
- )
- ),
- Effect.withSpan('parse-json', {
- attributes: {
- data,
- },
- })
- );
diff --git a/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/read-json.ts b/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/read-json.ts
index f489557d2..4b87e3bcf 100644
--- a/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/read-json.ts
+++ b/ts/packages/cli/src/effect-errors/dependencies/fs/read-json/read-json.ts
@@ -1,7 +1,7 @@
import { FileSystem } from '@effect/platform/FileSystem';
import { Effect, pipe } from 'effect';
-import { parseJson } from './parse-json';
+import { parseJsonRecord } from 'src/utils/parse-json';
export const readJsonEffect = <TJson>(filePath: string) =>
pipe(
@@ -13,7 +13,7 @@ export const readJsonEffect = <TJson>(filePath: string) =>
return null;
}
- const json = yield* parseJson(data);
+ const json = yield* parseJsonRecord(data);
return json as TJson;
}),
diff --git a/ts/packages/cli/src/utils/parse-json-ish.ts b/ts/packages/cli/src/utils/parse-json-ish.ts
deleted file mode 100644
index 679532ed0..000000000
--- a/ts/packages/cli/src/utils/parse-json-ish.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { parse as parseJsonWithComments } from 'comment-json';
-
-export const parseJsonIsh = (raw: string): unknown => {
- try {
- return JSON.parse(raw) as unknown;
- } catch {
- try {
- return parseJsonWithComments(raw, undefined, true) as unknown;
- } catch {
- return Function(`"use strict"; return (${raw});`)() as unknown;
- }
- }
-};
diff --git a/ts/packages/cli/src/utils/parse-json.ts b/ts/packages/cli/src/utils/parse-json.ts
new file mode 100644
index 000000000..216dfad94
--- /dev/null
+++ b/ts/packages/cli/src/utils/parse-json.ts
@@ -0,0 +1,41 @@
+import { Data, Either, Predicate } from 'effect';
+import JSON5 from 'json5';
+
+export class JsonParsingError extends Data.TaggedError('JsonParsingError')<{
+ /**
+ * `syntax` — the input is not parseable at all;
+ * `not-a-record` — valid JSON, but an array/scalar/null instead of an object.
+ */
+ reason: 'syntax' | 'not-a-record';
+ cause: unknown;
+}> {}
+
+/**
+ * Parses JSON or a JS-style object literal (unquoted keys, single quotes,
+ * trailing commas, comments) into a record. JSON5 covers the documented
+ * "JSON or JS-style object literal" contract without evaluating the input.
+ * Inputs that parse to anything other than an object (arrays, scalars,
+ * `null`) fail with `JsonParsingError`.
+ *
+ * Returns an `Either` because parsing is synchronous; `Either` is a subtype
+ * of `Effect`, so call sites can `yield*` it or pipe it into Effect
+ * combinators directly.
+ */
+export const parseJsonRecord = (
+ raw: string
+): Either.Either<Record<string, unknown>, JsonParsingError> =>
+ Either.try({
+ try: (): unknown => JSON5.parse(raw),
+ catch: cause => new JsonParsingError({ reason: 'syntax', cause }),
+ }).pipe(
+ Either.filterOrLeft(
+ Predicate.isRecord,
+ parsed =>
+ new JsonParsingError({
+ reason: 'not-a-record',
+ cause: new Error(
+ `Expected a JSON object, received ${Array.isArray(parsed) ? 'an array' : typeof parsed}`
+ ),
+ })
+ )
+ );
diff --git a/ts/packages/cli/test/src/utils/parse-json.test.ts b/ts/packages/cli/test/src/utils/parse-json.test.ts
new file mode 100644
index 000000000..c4311dd01
--- /dev/null
+++ b/ts/packages/cli/test/src/utils/parse-json.test.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it } from '@effect/vitest';
+import { Either } from 'effect';
+import { JsonParsingError, parseJsonRecord } from 'src/utils/parse-json';
+
+const parse = (raw: string) => Either.getOrThrowWith(parseJsonRecord(raw), error => error);
+const parseError = (raw: string) => Either.flip(parseJsonRecord(raw)).pipe(Either.getOrNull);
+
+describe('parseJsonRecord', () => {
+ it('parses strict JSON objects', () => {
+ expect(parse('{"foo": "bar", "n": 1}')).toEqual({ foo: 'bar', n: 1 });
+ });
+
+ it('parses JS-style object literals (unquoted keys, single quotes, trailing commas)', () => {
+ expect(parse("{ foo: 'bar', trailing: 1, }")).toEqual({ foo: 'bar', trailing: 1 });
+ expect(parse("{ nested: { a: [1, 2,], b: 'c' } }")).toEqual({
+ nested: { a: [1, 2], b: 'c' },
+ });
+ });
+
+ it('parses JSON with comments', () => {
+ expect(
+ parse(`{
+ // line comment
+ "foo": "bar",
+ /* block comment */
+ "n": 1
+ }`)
+ ).toEqual({ foo: 'bar', n: 1 });
+ });
+
+ it('fails for JSON values that are not objects', () => {
+ expect(parseError('[1, 2, 3]')).toBeInstanceOf(JsonParsingError);
+ expect(parseError('"hello"')).toBeInstanceOf(JsonParsingError);
+ expect(parseError('42')).toBeInstanceOf(JsonParsingError);
+ expect(parseError('null')).toBeInstanceOf(JsonParsingError);
+ });
+
+ it('does not execute code embedded in the input', () => {
+ const globals = globalThis as { __pwned?: boolean };
+ delete globals.__pwned;
+
+ expect(parseError('(globalThis.__pwned = true, {})')).toBeInstanceOf(JsonParsingError);
+ expect(globals.__pwned).toBeUndefined();
+
+ expect(parseError('process.exit(0)')).toBeInstanceOf(JsonParsingError);
+ expect(parseError("require('node:child_process').execSync('true')")).toBeInstanceOf(
+ JsonParsingError
+ );
+ expect(parseError('{ foo: (() => { globalThis.__pwned = true; })() }')).toBeInstanceOf(
+ JsonParsingError
+ );
+ expect(globals.__pwned).toBeUndefined();
+ });
+
+ it('rejects plain JS expressions instead of evaluating them', () => {
+ expect(parseError('1+1')).toBeInstanceOf(JsonParsingError);
+ expect(parseError('[1, 2].map(x => x)')).toBeInstanceOf(JsonParsingError);
+ expect(parseError('new Date()')).toBeInstanceOf(JsonParsingError);
+ });
+
+ it('fails for garbage input', () => {
+ expect(parseError('not json at all!')).toBeInstanceOf(JsonParsingError);
+ expect(parseError('')).toBeInstanceOf(JsonParsingError);
+ });
+});