[GH-ISSUE #3015] [Issue]: parametersSchema accepted with no JSON-Schema validation; malformed nested schemas reach the LLM provider verbatim #1588

Closed
opened 2026-10-21 05:40:13 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3015

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

The Zod create/update schema types parametersSchema as z.record(z.unknown()).default({}) — any JSON object is accepted, with no validation of JSON-Schema shape. The only structural validation (validateToolSchema) runs at generation time and checks ONLY the root-level type, properties, and required. It does NOT validate nested property definitions. So a tool whose parametersSchema is e.g. {"type":"object","properties":{"foo":{"type":"notatype"}}} passes validateToolSchema and is forwarded to the provider verbatim. With a strict provider this can 400 the whole generation request (taking down all tools for that turn), not just the bad tool. Via the Tool Editor UI this is hard to trigger (the type dropdown is constrained), but it is fully reachable through the raw POST/PATCH API and through import.

How to reproduce (try it yourself)

  1. POST /custom-tools with {"name":"bad_schema","description":"x","executionType":"static","staticResult":"ok","parametersSchema":{"type":"object","properties":{"foo":{"type":"notatype"}}}}.
  2. The create succeeds (Zod allows any record; root-level validateToolSchema passes).
  3. Enable the tool and run a generation with tools enabled against a strict provider.

You'll see: the provider rejects the tools payload because property foo.type is invalid; the whole generation fails rather than silently skipping just this tool.

Expected: a single malformed custom tool is skipped (with a server warn) instead of poisoning the entire provider tools array; ideally the raw API also returns a clear 400 at create time.

Where it is

Zod default at packages/shared/src/schemas/custom-tool.schema.ts:15; CRUD boundary with no schema validation at packages/server/src/routes/custom-tools.routes.ts:32,46; root-only validateToolSchema at packages/server/src/services/generation/tool-resolution-runtime.ts:230-260 and verbatim push at :321-328; verbatim forward to provider at packages/server/src/services/llm/providers/openai.provider.ts:884,1136,1550-1555; unvalidated import path at packages/client/src/lib/custom-tool-transfer.ts:46-57,82-121,200-217.

What's happening

No JSON-Schema shape validation exists at the CRUD or import boundary (Zod accepts any record), and the resolution-time validateToolSchema only inspects the root object, leaving nested property definitions unchecked before the schema is handed to the provider.

Suggested fix

Make validateToolSchema in tool-resolution-runtime.ts recurse into property definitions, with a PERMISSIVE ruleset. The existing per-tool try/catch (lines 305-337) already skips an offending tool and logs a warn, so a recursive validator turns a malformed nested property into a skip instead of a verbatim push. Add a recursive validateParameterProperty(prop, path) that:

  • rejects a property whose type is present-but-invalid (not one of string/number/integer/boolean/array/object),
  • rejects a non-array enum, a structurally-wrong items, a non-object nested properties, or a non-string-array required,
  • recurses into items and nested properties,

and call it for each root property after the existing root checks. Do NOT require description, do NOT reject unknown/extra keywords (oneOf, additionalProperties, default, minimum, maximum, multipleOf must pass through), and accept properties with no explicit type, so the real schemas the system already emits still pass (e.g. spotify_play with oneOf+additionalProperties:false, save_lorebook_entry with array+items+enum). This fixes both new tools and any malformed row already persisted, with no Zod/route change.

Regression avoided: CRUD-boundary-only validation was rejected as incomplete — it only guards new writes, leaving pre-existing malformed rows (from prior raw-API writes or imports) to still poison the whole request at generation time. A strict/over-tight Zod mirror at the boundary would also reject legitimate oneOf/additionalProperties/items/enum schemas on the import path. The recursive resolution-time validator is permissive (type optional, no required description, unknown keys allowed), so it only rejects present-but-invalid property types and structurally-broken items/enum/properties/required — exactly the malformed cases. Optional defense-in-depth: call the same permissive ruleset at the CRUD boundary to return a 400 early; if added, reuse this exact ruleset, not a stricter mirror.

Related: this touches the same validateToolSchema function as the empty-{}-schema drop (filed as a sibling issue). Coordinate the two — this one adds nested validation; the other relaxes the empty case.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3015 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The Zod create/update schema types `parametersSchema` as `z.record(z.unknown()).default({})` — any JSON object is accepted, with no validation of JSON-Schema shape. The only structural validation (`validateToolSchema`) runs at generation time and checks ONLY the root-level `type`, `properties`, and `required`. It does NOT validate nested property definitions. So a tool whose `parametersSchema` is e.g. `{"type":"object","properties":{"foo":{"type":"notatype"}}}` passes `validateToolSchema` and is forwarded to the provider verbatim. With a strict provider this can 400 the whole generation request (taking down all tools for that turn), not just the bad tool. Via the Tool Editor UI this is hard to trigger (the type dropdown is constrained), but it is fully reachable through the raw POST/PATCH API and through import. **How to reproduce (try it yourself)** 1. `POST /custom-tools` with `{"name":"bad_schema","description":"x","executionType":"static","staticResult":"ok","parametersSchema":{"type":"object","properties":{"foo":{"type":"notatype"}}}}`. 2. The create succeeds (Zod allows any record; root-level `validateToolSchema` passes). 3. Enable the tool and run a generation with tools enabled against a strict provider. You'll see: the provider rejects the tools payload because property `foo.type` is invalid; the whole generation fails rather than silently skipping just this tool. Expected: a single malformed custom tool is skipped (with a server warn) instead of poisoning the entire provider tools array; ideally the raw API also returns a clear 400 at create time. **Where it is** Zod default at `packages/shared/src/schemas/custom-tool.schema.ts:15`; CRUD boundary with no schema validation at `packages/server/src/routes/custom-tools.routes.ts:32,46`; root-only `validateToolSchema` at `packages/server/src/services/generation/tool-resolution-runtime.ts:230-260` and verbatim push at `:321-328`; verbatim forward to provider at `packages/server/src/services/llm/providers/openai.provider.ts:884,1136,1550-1555`; unvalidated import path at `packages/client/src/lib/custom-tool-transfer.ts:46-57,82-121,200-217`. **What's happening** No JSON-Schema shape validation exists at the CRUD or import boundary (Zod accepts any record), and the resolution-time `validateToolSchema` only inspects the root object, leaving nested property definitions unchecked before the schema is handed to the provider. **Suggested fix** Make `validateToolSchema` in `tool-resolution-runtime.ts` recurse into property definitions, with a PERMISSIVE ruleset. The existing per-tool try/catch (lines 305-337) already skips an offending tool and logs a warn, so a recursive validator turns a malformed nested property into a skip instead of a verbatim push. Add a recursive `validateParameterProperty(prop, path)` that: - rejects a property whose `type` is present-but-invalid (not one of string/number/integer/boolean/array/object), - rejects a non-array `enum`, a structurally-wrong `items`, a non-object nested `properties`, or a non-string-array `required`, - recurses into `items` and nested `properties`, and call it for each root property after the existing root checks. Do NOT require `description`, do NOT reject unknown/extra keywords (`oneOf`, `additionalProperties`, `default`, `minimum`, `maximum`, `multipleOf` must pass through), and accept properties with no explicit `type`, so the real schemas the system already emits still pass (e.g. `spotify_play` with `oneOf`+`additionalProperties:false`, `save_lorebook_entry` with `array`+`items`+`enum`). This fixes both new tools and any malformed row already persisted, with no Zod/route change. *Regression avoided:* CRUD-boundary-only validation was rejected as incomplete — it only guards new writes, leaving pre-existing malformed rows (from prior raw-API writes or imports) to still poison the whole request at generation time. A strict/over-tight Zod mirror at the boundary would also reject legitimate `oneOf`/`additionalProperties`/`items`/`enum` schemas on the import path. The recursive resolution-time validator is permissive (type optional, no required description, unknown keys allowed), so it only rejects present-but-invalid property types and structurally-broken `items`/`enum`/`properties`/`required` — exactly the malformed cases. Optional defense-in-depth: call the same permissive ruleset at the CRUD boundary to return a 400 early; if added, reuse this exact ruleset, not a stricter mirror. *Related: this touches the same `validateToolSchema` function as the empty-`{}`-schema drop (filed as a sibling issue). Coordinate the two — this one adds nested validation; the other relaxes the empty case.* --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in merged staging PR #3018.

<!-- gh-comment-id:4833019673 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in merged staging PR #3018.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1588
No description provided.