mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(core): treat oneOf as anyOf once patterns are ignored
This commit is contained in:
@@ -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,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user