fix(core): treat oneOf as anyOf once patterns are ignored

This commit is contained in:
Aiden Cline
2026-10-31 01:25:46 -05:00
Unverified
parent 7fc3aa518b
commit 16259fb5bc
2 changed files with 30 additions and 2 deletions
+10 -2
View File
@@ -104,9 +104,17 @@ const jsonSchema = (schema: JsonSchema.JsonSchema) => {
(typeof schema.$schema === "string" && schema.$schema.includes("draft-07")) || "definitions" in schema
? JsonSchema.fromSchemaDraft07(schema)
: JsonSchema.fromSchemaDraft2020_12(schema)
// Tool schemas are untrusted, so skip regexes (ReDoS) but keep validating everything else.
// Tool schemas are untrusted, so skip regexes (ReDoS) but keep validating everything else. With regexes gone,
// `oneOf` branches that only differed by pattern would all match and reject valid input, so treat it as `anyOf`.
return Schema.make<Schema.Codec<unknown>>(
SchemaRepresentation.fromJsonSchemaDocument(draft, { patterns: "ignore" }).ast,
SchemaRepresentation.fromJsonSchemaDocument(draft, {
patterns: "ignore",
onEnter: (node) => {
if (!("oneOf" in node) || "anyOf" in node) return node
const { oneOf, ...rest } = node
return { ...rest, anyOf: oneOf }
},
}).ast,
)
}
+20
View File
@@ -530,6 +530,26 @@ test("raw JSON schemas with regex patterns still validate input", async () => {
expect(await Effect.runPromise(execute(tool, { id: "not-hex", count: 1 }, context))).toMatchObject({
content: [{ type: "text", text: '{"id":"not-hex","count":1}' }],
})
const oneOf: Info = {
...tool,
name: "pattern-one-of",
input: {
type: "object",
properties: {
v: {
oneOf: [
{ type: "string", pattern: "^a" },
{ type: "integer", minimum: 0 },
{ type: "string", pattern: "^b" },
],
},
},
},
}
expect(await Effect.runPromise(execute(oneOf, { v: "abc" }, context))).toMatchObject({
content: [{ type: "text", text: '{"v":"abc"}' }],
})
expect(await Effect.runPromise(Effect.flip(execute(oneOf, { v: true }, context)))).toBeInstanceOf(Tool.Error)
expect(await Effect.runPromise(Effect.flip(execute(tool, { id: "abc" }, context)))).toEqual(
new Tool.Error({
message: