@devries-cameron20 / composio-1 / commits / 3ebd4eba2
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.

fix(core): cap node depth and detect non-$ref object cycles in dereferencer

`dereferenceJsonSchema` was unsafe against two attacker-shaped inputs: 1. Pure object nesting blew the V8 stack — `MAX_REF_CHAIN_DEPTH` only counts `$ref` hops, so a 1000-level-deep `{properties:{x:{...}}}` schema killed the process before any cap fired. 2. JS object cycles that don't flow through `$ref` (e.g. `a.b = a`, plausible when consumers ingest schemas as live JS objects) infinite-looped — the string-marker cycle check only triggered inside the `$ref` branch. Adds: - `MAX_NODE_DEPTH = 512` enforced on every recursion. - A `WeakSet` of currently-descending node identities, populated on entry and cleared on exit (try/finally), returns the cycle-break sentinel on re-entry. Independent from the `$ref` chain detection. Two new red regression tests cover both cases. All 854 core tests pass.

jkomyno committed Apr 30, 2026 at 01:49 UTC 3ebd4eba24b4b34b8a13051d4aabeae3b4432658
2 files changed +94 -46
ts/packages/core/src/utils/jsonSchema.ts
+66 -46
@@ -3,6 +3,7 @@ import { JsonSchemaRefResolutionError, JsonSchemaToZodError } from '../errors';
3 import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
4
5 const MAX_REF_CHAIN_DEPTH = 100;
6 +const MAX_NODE_DEPTH = 512;
7 const CYCLE_BREAK_SENTINEL = Object.freeze({ type: 'object', additionalProperties: true });
8 const INTERNAL_REF_PREFIX = '#';
9
@@ -102,66 +103,85 @@ export function dereferenceJsonSchema<T = unknown>(schema: T): T {
103 if (!isPlainObject(schema)) return schema;
104
105 const root = schema as Record<string, unknown>;
106 + const visiting = new WeakSet<object>();
107
106 - const walk = (node: unknown, path: string[], depth: number): unknown => {
108 + const walk = (node: unknown, path: string[], depth: number, nodeDepth: number): unknown => {
109 + if (nodeDepth >= MAX_NODE_DEPTH) {
110 + throw new JsonSchemaRefResolutionError(
111 + `JSON Schema node depth exceeded cap (${MAX_NODE_DEPTH})`
112 + );
113 + }
114 if (Array.isArray(node)) {
108 - return node.map((item, index) => walk(item, [...path, String(index)], depth));
115 + if (visiting.has(node)) return { ...CYCLE_BREAK_SENTINEL };
116 + visiting.add(node);
117 + try {
118 + return node.map((item, index) =>
119 + walk(item, [...path, String(index)], depth, nodeDepth + 1)
120 + );
121 + } finally {
122 + visiting.delete(node);
123 + }
124 }
125 if (!isPlainObject(node)) return node;
111 -
112 - if (typeof node.$ref === 'string') {
113 - const ref = node.$ref;
114 - // External refs are out of scope; leave them alone.
115 - if (!ref.startsWith(INTERNAL_REF_PREFIX)) {
116 - const cloned: Record<string, unknown> = {};
117 - for (const [key, value] of Object.entries(node)) {
118 - cloned[key] = walk(value, [...path, key], depth);
126 + if (visiting.has(node)) return { ...CYCLE_BREAK_SENTINEL };
127 + visiting.add(node);
128 + try {
129 + if (typeof node.$ref === 'string') {
130 + const ref = node.$ref;
131 + // External refs are out of scope; leave them alone.
132 + if (!ref.startsWith(INTERNAL_REF_PREFIX)) {
133 + const cloned: Record<string, unknown> = {};
134 + for (const [key, value] of Object.entries(node)) {
135 + cloned[key] = walk(value, [...path, key], depth, nodeDepth + 1);
136 + }
137 + return cloned;
138 }
120 - return cloned;
121 - }
139
123 - if (depth >= MAX_REF_CHAIN_DEPTH) {
124 - throw new JsonSchemaRefResolutionError(
125 - `JSON Schema $ref chain exceeded depth cap (${MAX_REF_CHAIN_DEPTH}): ${ref}`,
126 - { meta: { ref } }
127 - );
128 - }
140 + if (depth >= MAX_REF_CHAIN_DEPTH) {
141 + throw new JsonSchemaRefResolutionError(
142 + `JSON Schema $ref chain exceeded depth cap (${MAX_REF_CHAIN_DEPTH}): ${ref}`,
143 + { meta: { ref } }
144 + );
145 + }
146
130 - // Cycle detection: if `ref` already appears in the current path, this
131 - // resolution would re-enter itself. Break the cycle.
132 - if (path.includes(`@ref:${ref}`)) {
133 - return { ...CYCLE_BREAK_SENTINEL };
134 - }
147 + // Cycle detection: if `ref` already appears in the current path, this
148 + // resolution would re-enter itself. Break the cycle.
149 + if (path.includes(`@ref:${ref}`)) {
150 + return { ...CYCLE_BREAK_SENTINEL };
151 + }
152
136 - const { target } = resolvePointer(root, ref);
137 - const resolved = walk(target, [...path, `@ref:${ref}`], depth + 1);
153 + const { target } = resolvePointer(root, ref);
154 + const resolved = walk(target, [...path, `@ref:${ref}`], depth + 1, nodeDepth + 1);
155
139 - // Shallow-merge sibling keywords (Draft 2020-12 semantics: siblings win
140 - // on collision). Draft 7 ignores siblings entirely, but the Composio
141 - // tool surface admits both drafts so we honor siblings for safety.
142 - const { $ref: _omit, ...siblings } = node;
143 - void _omit;
144 - if (Object.keys(siblings).length === 0) {
145 - return resolved;
146 - }
147 - if (!isPlainObject(resolved)) {
148 - return resolved;
149 - }
150 - const merged: Record<string, unknown> = { ...resolved };
151 - for (const [key, value] of Object.entries(siblings)) {
152 - merged[key] = walk(value, [...path, key], depth);
156 + // Shallow-merge sibling keywords (Draft 2020-12 semantics: siblings win
157 + // on collision). Draft 7 ignores siblings entirely, but the Composio
158 + // tool surface admits both drafts so we honor siblings for safety.
159 + const { $ref: _omit, ...siblings } = node;
160 + void _omit;
161 + if (Object.keys(siblings).length === 0) {
162 + return resolved;
163 + }
164 + if (!isPlainObject(resolved)) {
165 + return resolved;
166 + }
167 + const merged: Record<string, unknown> = { ...resolved };
168 + for (const [key, value] of Object.entries(siblings)) {
169 + merged[key] = walk(value, [...path, key], depth, nodeDepth + 1);
170 + }
171 + return merged;
172 }
154 - return merged;
155 - }
173
157 - const cloned: Record<string, unknown> = {};
158 - for (const [key, value] of Object.entries(node)) {
159 - cloned[key] = walk(value, [...path, key], depth);
174 + const cloned: Record<string, unknown> = {};
175 + for (const [key, value] of Object.entries(node)) {
176 + cloned[key] = walk(value, [...path, key], depth, nodeDepth + 1);
177 + }
178 + return cloned;
179 + } finally {
180 + visiting.delete(node);
181 }
161 - return cloned;
182 };
183
164 - const out = walk(root, [], 0);
184 + const out = walk(root, [], 0, 0);
185 if (isPlainObject(out)) {
186 delete out.$defs;
187 delete out.definitions;
ts/packages/core/test/utils/jsonSchema.test.ts
+28
@@ -170,6 +170,34 @@ describe('dereferenceJsonSchema', () => {
170 ).toThrow(JsonSchemaRefResolutionError);
171 });
172
173 + it('throws JsonSchemaRefResolutionError on pathologically deep object nesting', () => {
174 + // Build { properties: { x: { properties: { x: { ... } } } } } 1000 levels deep.
175 + // Without a node-depth cap this would blow the V8 stack; with the cap, it throws
176 + // a typed error before recursion grows unbounded.
177 + type Nested = { type: 'object'; properties: { x: Nested | { type: 'string' } } };
178 + let leaf: Nested | { type: 'string' } = { type: 'string' };
179 + for (let i = 0; i < 1000; i++) {
180 + leaf = { type: 'object', properties: { x: leaf } } as Nested;
181 + }
182 + expect(() => dereferenceJsonSchema(leaf)).toThrow(JsonSchemaRefResolutionError);
183 + });
184 +
185 + it('breaks JS object cycles that do not flow through a $ref', () => {
186 + // Composio ingests schemas as live JS objects (custom tools, telemetry, …),
187 + // so a non-$ref cycle is plausible. Without object-identity cycle detection
188 + // this would infinite-loop; with it, the cycle returns the permissive sentinel.
189 + const root: Record<string, unknown> = {
190 + type: 'object',
191 + properties: {},
192 + };
193 + (root.properties as Record<string, unknown>).self = root;
194 +
195 + const out = dereferenceJsonSchema(root) as {
196 + properties: { self: { type: string; additionalProperties: boolean } };
197 + };
198 + expect(out.properties.self).toEqual({ type: 'object', additionalProperties: true });
199 + });
200 +
201 it('leaves external $ref pointers untouched', () => {
202 const out = dereferenceJsonSchema({
203 type: 'object',