mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(core): validate JSON Schema tool input even when the schema has regex patterns
This commit is contained in:
@@ -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<Schema.Codec<unknown>>(SchemaRepresentation.fromJsonSchemaDocument(draft).ast)
|
||||
// Tool schemas are untrusted, so skip regexes (ReDoS) but keep validating everything else.
|
||||
return Schema.make<Schema.Codec<unknown>>(
|
||||
SchemaRepresentation.fromJsonSchemaDocument(draft, { patterns: "ignore" }).ast,
|
||||
)
|
||||
}
|
||||
|
||||
const encodeOutput = (schema: Tool.ValueSchema<any>, value: unknown) => {
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user