@devries-cameron20 / composio-1 / commits / b4b9fc4a3
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(json-schema-to-zod): guard schema `pattern` compilation (#4405)

This PR: - Adds `src/utils/compile-pattern.ts` in `@composio/json-schema-to-zod`, a shared helper that compiles `pattern` and `patternProperties` keys through `new RegExp` inside a try/catch and rethrows a typed `InvalidPatternError` (exported) that names the keyword, the pattern, and the property path (e.g. `at properties.name`), mirroring the eager `assertRegexCompiles` guard in `@composio/json-schema-to-effect-schema`. - Adds a 1024-character cap on pattern length, reported through the same error with `reason: 'too-long'`. - Chooses fail-at-conversion over degrade-with-warning: the package has no warning hook or lenient `refs` mode, its sibling packages and `oneOf` handling already throw on schema defects, and a silently dropped `pattern` would widen what a tool accepts without anyone noticing. - Threads `refs.path` into `parseString` and `parseTypelessConstraints` so the error carries the property path, and appends `patternProperties.<key>` for dynamic-key objects. - Makes `@composio/core`'s `jsonSchemaToZodSchema` include the cause message in `JsonSchemaToZodError`, so the wrapped error names the malformed property without unwrapping `cause`. - Leaves out a nested-quantifier (star-height) ReDoS heuristic on purpose: it flags linear patterns such as `^(\d+\.)*\d+$`, a wrong rejection makes `tools.get` fail for the whole tool, and it cannot be validated against the live toolkit catalog without false-positive risk. Catastrophic backtracking from a hostile `pattern` remains a known limitation; only zero-false-positive guards ship here. - Adds `test/compile-pattern.test.ts` (`(` -> `InvalidPatternError` with `SyntaxError` cause, length cap, typeless and `patternProperties` paths, `^(\d+\.)*\d+$` still compiles and enforces, valid patterns still enforced) and a core test for the wrapped message; adds a patch changeset for both packages. - Verification: `pnpm test` + `pnpm typecheck` in `ts/packages/json-schema-to-zod` (4 files, 255 tests), `pnpm test` in `ts/packages/core` (54 files, 1281 passed, 2 expected fail), root `pnpm typecheck` (14/14), `lint:packages` and Prettier clean. https://claude.ai/code/session_016ZuBv7JhVdSYTLYcTy2VJr

Alberto Schiabel committed Sep 9, 2026 at 19:53 UTC b4b9fc4a322cd020aa57716a6fb3edd4f74321e3
10 files changed +245 -11
.changeset/json-schema-to-zod-pattern-guard.md new
+6
@@ -0,0 +1,6 @@
1 +---
2 +'@composio/json-schema-to-zod': patch
3 +'@composio/core': patch
4 +---
5 +
6 +Guard schema `pattern` and `patternProperties` compilation. A pattern that does not compile or exceeds 1024 characters now fails conversion with an `InvalidPatternError` that names the offending property path instead of a raw `SyntaxError`. `@composio/core` surfaces that path in the `JsonSchemaToZodError` message. No backtracking heuristic is applied: a hostile `pattern` that backtracks catastrophically remains a known limitation.
ts/packages/core/src/utils/jsonSchema.ts
+4 -1
@@ -495,7 +495,10 @@ export function jsonSchemaToZodSchema<T extends z.ZodTypeAny>(
495 const zodSchema = jsonSchemaToZod(schema) as T;
496 return zodSchema;
497 } catch (error) {
498 - throw new JsonSchemaToZodError('Failed to convert JSON Schema to Zod Schema', {
498 + // The cause names the offending property path; surface it in the message
499 + // so a single malformed property is identifiable without unwrapping.
500 + const detail = error instanceof Error ? `: ${error.message}` : '';
501 + throw new JsonSchemaToZodError(`Failed to convert JSON Schema to Zod Schema${detail}`, {
502 cause: error,
503 });
504 }
ts/packages/core/test/utils/jsonSchema.test.ts
+23 -1
@@ -3,8 +3,12 @@ import {
3 deduplicateJsonSchemaRequiredArrays,
4 dereferenceJsonSchema,
5 ensureObjectTypeOnProperties,
6 + jsonSchemaToZodSchema,
7 } from '../../src/utils/jsonSchema';
7 -import { JsonSchemaRefResolutionError } from '../../src/errors/ValidationErrors';
8 +import {
9 + JsonSchemaRefResolutionError,
10 + JsonSchemaToZodError,
11 +} from '../../src/errors/ValidationErrors';
12 import logger from '../../src/utils/logger';
13 import { ToolSchema } from '../../src/types/tool.types';
14 import {
@@ -633,6 +637,24 @@ describe('ToolSchema parameter-root preservation', () => {
637 });
638 });
639
640 +describe('jsonSchemaToZodSchema', () => {
641 + it('names the property whose pattern cannot be compiled', () => {
642 + let caught: unknown;
643 + try {
644 + jsonSchemaToZodSchema({
645 + type: 'object',
646 + properties: { name: { type: 'string', pattern: '(' } },
647 + });
648 + } catch (error) {
649 + caught = error;
650 + }
651 +
652 + expect(caught).toBeInstanceOf(JsonSchemaToZodError);
653 + expect((caught as JsonSchemaToZodError).message).toContain('at properties.name');
654 + expect((caught as JsonSchemaToZodError).cause).toBeInstanceOf(Error);
655 + });
656 +});
657 +
658 describe('shared corpus invariants', () => {
659 const baseCase = (id: string): CorpusCase => ({
660 id,
ts/packages/json-schema-to-zod/src/index.ts
+1
@@ -1,2 +1,3 @@
1 export type * from './types';
2 export { jsonSchemaToZod, jsonSchemaToZodShape } from './json-schema-to-zod';
3 +export { InvalidPatternError, type InvalidPatternReason } from './utils/compile-pattern';
ts/packages/json-schema-to-zod/src/parsers/parse-object.ts
+2 -1
@@ -5,6 +5,7 @@ import { parseAnyOf } from './parse-any-of';
5 import { parseOneOf } from './parse-one-of';
6 import { parseSchema } from './parse-schema';
7 import type { JsonSchemaObject, Refs, JsonSchema } from '../types';
8 +import { compilePattern } from '../utils/compile-pattern';
9 import { its } from '../utils/its';
10
11 /**
@@ -104,7 +105,7 @@ function parseDynamicKeyObject(
105 ): z.ZodTypeAny {
106 const patterns = Object.entries(normalizedSchema.patternProperties ?? {}).map(
107 ([pattern, schema]) => ({
107 - regex: new RegExp(pattern),
108 + regex: compilePattern('patternProperties', pattern, refs),
109 parser: parseSchema(schema, {
110 ...refs,
111 path: [...refs.path, 'patternProperties', pattern],
ts/packages/json-schema-to-zod/src/parsers/parse-schema.ts
+2 -2
@@ -122,11 +122,11 @@ const selectParser: ParserSelector = (schema, refs) => {
122 } else if (its.a.not(schema)) {
123 return parseNot(schema, refs);
124 } else if (hasTypelessScalarConstraints(schema)) {
125 - return parseTypelessConstraints(schema);
125 + return parseTypelessConstraints(schema, refs);
126 } else if (its.a.multipleType(schema)) {
127 return parseMultipleType(schema, refs);
128 } else if (its.a.primitive(schema, 'string')) {
129 - return parseString(schema);
129 + return parseString(schema, refs);
130 } else if (its.a.primitive(schema, 'number') || its.a.primitive(schema, 'integer')) {
131 return parseNumber(schema);
132 } else if (its.a.primitive(schema, 'boolean')) {
ts/packages/json-schema-to-zod/src/parsers/parse-string.ts
+7 -3
@@ -1,9 +1,13 @@
1 import { z } from 'zod/v3';
2
3 -import type { JsonSchemaObject } from '../types';
3 +import type { JsonSchemaObject, Refs } from '../types';
4 +import { compilePattern } from '../utils/compile-pattern';
5 import { extendSchemaWithMessage } from '../utils/extend-schema';
6
6 -export const parseString = (jsonSchema: JsonSchemaObject & { type: 'string' }) => {
7 +export const parseString = (
8 + jsonSchema: JsonSchemaObject & { type: 'string' },
9 + refs: Pick<Refs, 'path'>
10 +) => {
11 let zodSchema = z.string();
12
13 zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, 'format', (zs, format, errorMsg) => {
@@ -39,7 +43,7 @@ export const parseString = (jsonSchema: JsonSchemaObject & { type: 'string' }) =
43 zs.base64(errorMsg)
44 );
45 zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, 'pattern', (zs, pattern, errorMsg) =>
42 - zs.regex(new RegExp(pattern), errorMsg)
46 + zs.regex(compilePattern('pattern', pattern, refs), errorMsg)
47 );
48 // JSON Schema length constraints count Unicode code points, while Zod's
49 // built-in `.min()`/`.max()` count UTF-16 code units and overcount astral
ts/packages/json-schema-to-zod/src/parsers/parse-typeless-constraints.ts
+6 -3
@@ -2,7 +2,7 @@ import { z } from 'zod/v3';
2
3 import { parseNumber } from './parse-number';
4 import { parseString } from './parse-string';
5 -import type { JsonSchemaObject } from '../types';
5 +import type { JsonSchemaObject, Refs } from '../types';
6
7 const SCALAR_CONSTRAINT_KEYS = [
8 'minLength',
@@ -23,8 +23,11 @@ export const hasTypelessScalarConstraints = (schema: JsonSchemaObject): boolean
23 * schema declares no `type`: `{ "minLength": 2 }` restricts strings while
24 * leaving every non-string value untouched.
25 */
26 -export const parseTypelessConstraints = (schema: JsonSchemaObject): z.ZodTypeAny => {
27 - const stringSchema = parseString({ ...schema, type: 'string' });
26 +export const parseTypelessConstraints = (
27 + schema: JsonSchemaObject,
28 + refs: Pick<Refs, 'path'>
29 +): z.ZodTypeAny => {
30 + const stringSchema = parseString({ ...schema, type: 'string' }, refs);
31 const numberSchema = parseNumber({ ...schema, type: 'number' });
32
33 return z.any().superRefine((value, ctx) => {
ts/packages/json-schema-to-zod/src/utils/compile-pattern.ts new
+76
@@ -0,0 +1,76 @@
1 +import type { Refs } from '../types';
2 +
3 +/**
4 + * Longest `pattern` / `patternProperties` key this package compiles. Tool
5 + * schemas come from third-party toolkit definitions; anything past this is
6 + * far outside what a format constraint needs and is rejected before it can
7 + * cost anything at validation time.
8 + */
9 +export const MAX_PATTERN_LENGTH = 1024;
10 +
11 +export type InvalidPatternReason = 'syntax' | 'too-long';
12 +
13 +/**
14 + * Raised at conversion time when a schema `pattern` cannot be turned into a
15 + * `RegExp`. Conversion fails eagerly, in line with how this package and
16 + * `@composio/json-schema-to-effect-schema` treat other schema defects: a broken
17 + * schema is the tool author's bug, so it surfaces once at construction rather
18 + * than on every validation as if the caller's input were wrong.
19 + */
20 +export class InvalidPatternError extends Error {
21 + override readonly name = 'InvalidPatternError';
22 +
23 + constructor(
24 + readonly keyword: 'pattern' | 'patternProperties',
25 + readonly pattern: string,
26 + readonly path: ReadonlyArray<string | number>,
27 + readonly reason: InvalidPatternReason,
28 + detail: string,
29 + options?: ErrorOptions
30 + ) {
31 + const location = path.length > 0 ? path.join('.') : '<root>';
32 + super(
33 + `Invalid ${keyword} regular expression ${JSON.stringify(pattern)} at ${location}: ${detail}`,
34 + options
35 + );
36 + }
37 +}
38 +
39 +/**
40 + * Compile a schema pattern, rejecting anything that does not compile or that
41 + * exceeds {@link MAX_PATTERN_LENGTH}.
42 + *
43 + * Both guards have no false positives: a pattern that fails here would never
44 + * have worked. No backtracking (ReDoS) heuristic is applied on purpose — the
45 + * syntactic checks that exist flag linear patterns such as `^(\d+\.)*\d+$`,
46 + * and a wrong rejection fails the whole tool the schema belongs to. A hostile
47 + * `pattern` that backtracks catastrophically remains a known limitation.
48 + *
49 + * No `u` flag is passed: existing tool schemas rely on identity escapes that
50 + * Unicode mode refuses, and the goal here is to fail on defects, not to
51 + * tighten which patterns are accepted.
52 + */
53 +export const compilePattern = (
54 + keyword: 'pattern' | 'patternProperties',
55 + pattern: string,
56 + refs: Pick<Refs, 'path'>
57 +): RegExp => {
58 + const path = keyword === 'pattern' ? refs.path : [...refs.path, 'patternProperties', pattern];
59 +
60 + if (pattern.length > MAX_PATTERN_LENGTH) {
61 + throw new InvalidPatternError(
62 + keyword,
63 + pattern,
64 + path,
65 + 'too-long',
66 + `pattern exceeds ${MAX_PATTERN_LENGTH} characters`
67 + );
68 + }
69 +
70 + try {
71 + return new RegExp(pattern);
72 + } catch (cause) {
73 + const detail = cause instanceof Error ? cause.message : String(cause);
74 + throw new InvalidPatternError(keyword, pattern, path, 'syntax', detail, { cause });
75 + }
76 +};
ts/packages/json-schema-to-zod/test/compile-pattern.test.ts new
+118
@@ -0,0 +1,118 @@
1 +import { describe, expect, it } from 'vitest';
2 +
3 +import { InvalidPatternError, jsonSchemaToZod } from '../src/index';
4 +import { MAX_PATTERN_LENGTH, compilePattern } from '../src/utils/compile-pattern';
5 +
6 +const invalidPatternAt = (build: () => unknown): InvalidPatternError => {
7 + try {
8 + build();
9 + } catch (error) {
10 + expect(error).toBeInstanceOf(InvalidPatternError);
11 + return error as InvalidPatternError;
12 + }
13 + throw new Error('expected conversion to fail');
14 +};
15 +
16 +describe('compilePattern', () => {
17 + it('compiles a valid pattern into an equivalent RegExp', () => {
18 + const regex = compilePattern('pattern', '^[a-z]+$', { path: [] });
19 + expect(regex.test('abc')).toBe(true);
20 + expect(regex.test('ABC')).toBe(false);
21 + });
22 +
23 + it('turns a RegExp SyntaxError into an InvalidPatternError that keeps the cause', () => {
24 + const error = invalidPatternAt(() =>
25 + compilePattern('pattern', '(', { path: ['properties', 'name'] })
26 + );
27 + expect(error.reason).toBe('syntax');
28 + expect(error.path).toEqual(['properties', 'name']);
29 + expect(error.cause).toBeInstanceOf(SyntaxError);
30 + expect(error.message).toContain('at properties.name');
31 + });
32 +
33 + it('rejects patterns above the length cap', () => {
34 + const error = invalidPatternAt(() =>
35 + compilePattern('pattern', 'a'.repeat(MAX_PATTERN_LENGTH + 1), { path: [] })
36 + );
37 + expect(error.reason).toBe('too-long');
38 + expect(error.message).toContain('<root>');
39 + });
40 +
41 + it('appends the patternProperties key to the reported path', () => {
42 + const error = invalidPatternAt(() =>
43 + compilePattern('patternProperties', '[', { path: ['properties', 'tags'] })
44 + );
45 + expect(error.reason).toBe('syntax');
46 + expect(error.path).toEqual(['properties', 'tags', 'patternProperties', '[']);
47 + });
48 +});
49 +
50 +describe('jsonSchemaToZod pattern guard', () => {
51 + it('reports a malformed property pattern with its path instead of a raw SyntaxError', () => {
52 + const error = invalidPatternAt(() =>
53 + jsonSchemaToZod({
54 + type: 'object',
55 + properties: { name: { type: 'string', pattern: '(' } },
56 + })
57 + );
58 + expect(error).not.toBeInstanceOf(SyntaxError);
59 + expect(error.keyword).toBe('pattern');
60 + expect(error.pattern).toBe('(');
61 + expect(error.message).toContain('at properties.name');
62 + });
63 +
64 + it('reports a typeless pattern with the path of the node carrying it', () => {
65 + const error = invalidPatternAt(() =>
66 + jsonSchemaToZod({
67 + type: 'object',
68 + properties: { slug: { pattern: '[' } },
69 + })
70 + );
71 + expect(error.path).toEqual(['properties', 'slug']);
72 + });
73 +
74 + it('reports a malformed patternProperties key with its path', () => {
75 + const error = invalidPatternAt(() =>
76 + jsonSchemaToZod({
77 + type: 'object',
78 + properties: {
79 + labels: {
80 + type: 'object',
81 + patternProperties: { '^x-(': { type: 'string' } },
82 + },
83 + },
84 + })
85 + );
86 + expect(error.keyword).toBe('patternProperties');
87 + expect(error.path).toEqual(['properties', 'labels', 'patternProperties', '^x-(']);
88 + expect(error.message).toContain('at properties.labels.patternProperties.^x-(');
89 + });
90 +
91 + it('keeps compiling quantified groups that backtrack linearly', () => {
92 + const schema = jsonSchemaToZod({
93 + type: 'object',
94 + properties: { version: { type: 'string', pattern: '^(\\d+\\.)*\\d+$' } },
95 + required: ['version'],
96 + });
97 +
98 + expect(schema.safeParse({ version: '1.2.3' }).success).toBe(true);
99 + expect(schema.safeParse({ version: '42' }).success).toBe(true);
100 + expect(schema.safeParse({ version: '1.2.' }).success).toBe(false);
101 + expect(schema.safeParse({ version: 'v1' }).success).toBe(false);
102 + });
103 +
104 + it('still enforces valid patterns', () => {
105 + const schema = jsonSchemaToZod({
106 + type: 'object',
107 + properties: {
108 + id: { type: 'string', pattern: '^[a-z]{3}-\\d+$' },
109 + meta: { type: 'object', patternProperties: { '^x-': { type: 'number' } } },
110 + },
111 + required: ['id'],
112 + });
113 +
114 + expect(schema.safeParse({ id: 'abc-42', meta: { 'x-rate': 1 } }).success).toBe(true);
115 + expect(schema.safeParse({ id: 'ABC-42' }).success).toBe(false);
116 + expect(schema.safeParse({ id: 'abc-42', meta: { 'x-rate': 'one' } }).success).toBe(false);
117 + });
118 +});