diff --git a/packages/core/src/tool/runtime.ts b/packages/core/src/tool/runtime.ts index 7a65fa8345b..fa510787ad7 100644 --- a/packages/core/src/tool/runtime.ts +++ b/packages/core/src/tool/runtime.ts @@ -104,7 +104,10 @@ const jsonSchema = (schema: JsonSchema.JsonSchema) => { (typeof schema.$schema === "string" && schema.$schema.includes("draft-07")) || "definitions" in schema ? JsonSchema.fromSchemaDraft07(schema) : JsonSchema.fromSchemaDraft2020_12(schema) - return Schema.make>(SchemaRepresentation.fromJsonSchemaDocument(draft).ast) + // Tool schemas are untrusted, so skip regexes (ReDoS) but keep validating everything else. + return Schema.make>( + SchemaRepresentation.fromJsonSchemaDocument(draft, { patterns: "ignore" }).ast, + ) } const encodeOutput = (schema: Tool.ValueSchema, value: unknown) => { diff --git a/packages/core/test/tool-schema.test.ts b/packages/core/test/tool-schema.test.ts index 3670e7ac2c3..a045820ee62 100644 --- a/packages/core/test/tool-schema.test.ts +++ b/packages/core/test/tool-schema.test.ts @@ -515,6 +515,35 @@ test("raw JSON schemas resolve draft-07 definitions", async () => { ) }) +test("raw JSON schemas with regex patterns still validate input", async () => { + const tool: Info = { + name: "pattern", + description: "Pattern tool", + input: { + type: "object", + properties: { id: { type: "string", pattern: "^[a-f0-9]{32}$" }, count: { type: "number" } }, + required: ["id", "count"], + }, + execute: (input) => Effect.succeed({ content: JSON.stringify(input) }), + } + + expect(await Effect.runPromise(execute(tool, { id: "not-hex", count: 1 }, context))).toMatchObject({ + content: [{ type: "text", text: '{"id":"not-hex","count":1}' }], + }) + expect(await Effect.runPromise(Effect.flip(execute(tool, { id: "abc" }, context)))).toEqual( + new Tool.Error({ + message: + 'Invalid arguments for tool "pattern":\n- count: Missing key\n\nArguments provided:\n{\n "id": "abc"\n}\n\nUpdate the arguments and call the tool again.', + }), + ) + expect(await Effect.runPromise(Effect.flip(execute(tool, { id: "abc", count: "1" }, context)))).toEqual( + new Tool.Error({ + message: + 'Invalid arguments for tool "pattern":\n- count: Expected number\n\nArguments provided:\n{\n "id": "abc",\n "count": "1"\n}\n\nUpdate the arguments and call the tool again.', + }), + ) +}) + test("raw JSON schemas pass input through when they cannot be imported", async () => { const tool: Info = { name: "invalid-schema",