1
import { z } from 'zod/v3';
2
import { JsonSchemaRefResolutionError, JsonSchemaToZodError } from '../errors';
3
import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
4
+import logger from './logger';
5
import { isPlainObject } from './modifiers/FileToolModifier.utils.neutral';
6
7
const MAX_REF_CHAIN_DEPTH = 100;
8
const MAX_NODE_DEPTH = 512;
9
const CYCLE_BREAK_SENTINEL = { type: 'object', additionalProperties: true } as const;
10
+const POLLUTING_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
11
+const REF_RESOLUTION_FIXES = [
12
+ 'Ensure the $ref pointer matches a path in $defs or definitions',
13
+ 'External $ref pointers (http://, https://, file://, …) are not resolved by the SDK',
14
+];
15
16
const decodePointerSegment = (segment: string): string =>
17
segment.replace(/~1/g, '/').replace(/~0/g, '~');
19
const failResolution = (pointer: string, segment: string): never => {
20
throw new JsonSchemaRefResolutionError(`Cannot resolve $ref ${pointer}`, {
21
meta: { ref: pointer, failedAt: segment },
22
+ possibleFixes: REF_RESOLUTION_FIXES,
23
});
24
};
25
40
if (!pointer.startsWith('#/')) {
41
throw new JsonSchemaRefResolutionError(`Unsupported $ref pointer: ${pointer}`, {
42
meta: { ref: pointer },
43
+ possibleFixes: REF_RESOLUTION_FIXES,
44
});
45
}
46
return pointer
101
const root = schema as Record<string, unknown>;
102
const visiting = new WeakSet<object>();
103
104
+ // Filter `__proto__`/`constructor`/`prototype` keys to avoid altering the
105
+ // prototype of the cloned output node (which can break downstream
106
+ // `Object.keys` / `in` checks even though the global prototype is safe).
107
const cloneChildren = (
108
obj: Record<string, unknown>,
109
visitedRefs: ReadonlySet<string>,
111
nodeDepth: number
112
): Record<string, unknown> =>
113
Object.fromEntries(
103
- Object.entries(obj).map(([k, v]) => [k, walk(v, visitedRefs, chainDepth, nodeDepth + 1)])
114
+ Object.entries(obj)
115
+ .filter(([k]) => !POLLUTING_KEYS.has(k))
116
+ .map(([k, v]) => [k, walk(v, visitedRefs, chainDepth, nodeDepth + 1)])
117
);
118
119
// Function declaration so `cloneChildren` can reference `walk` despite being
126
): unknown {
127
if (nodeDepth >= MAX_NODE_DEPTH) {
128
throw new JsonSchemaRefResolutionError(
116
- `JSON Schema node depth exceeded cap (${MAX_NODE_DEPTH})`
129
+ `JSON Schema node depth exceeded cap (${MAX_NODE_DEPTH})`,
130
+ { possibleFixes: REF_RESOLUTION_FIXES }
131
);
132
}
133
if (Array.isArray(node)) {
146
const ref = typeof node.$ref === 'string' ? node.$ref : null;
147
// External refs and non-$ref nodes both pass through the same clone path.
148
if (ref === null || !ref.startsWith('#')) {
149
+ if (ref !== null) {
150
+ // Audit signal for security-sensitive deployments: a downstream
151
+ // resolver may fetch this and trigger SSRF or local-file disclosure.
152
+ logger.warn(`Leaving external $ref untouched: ${ref}`);
153
+ }
154
return cloneChildren(node, visitedRefs, chainDepth, nodeDepth);
155
}
156
157
if (chainDepth >= MAX_REF_CHAIN_DEPTH) {
158
throw new JsonSchemaRefResolutionError(
159
`JSON Schema $ref chain exceeded depth cap (${MAX_REF_CHAIN_DEPTH}): ${ref}`,
141
- { meta: { ref } }
160
+ { meta: { ref }, possibleFixes: REF_RESOLUTION_FIXES }
161
);
162
}
163
if (visitedRefs.has(ref)) return { ...CYCLE_BREAK_SENTINEL };