[GH-ISSUE #3029] [Issue]: Imported/seeded preset generation parameters are never clamped, so out-of-range values (e.g. temperature 99) reach the provider #1600

Closed
opened 2026-10-21 05:40:28 +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/3029

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
The interactive preset CRUD routes validate generation parameters against a Zod schema (temperature 0..2, topP 0..1, etc.), but the import and seed paths write parameters straight to storage with no validation, and the assembler's read path performs no clamping either. So a hand-edited or malicious .marinara.json whose parameters contains out-of-range values is stored and later forwarded to the LLM provider unclamped.

How to reproduce (try it yourself)

  1. Craft a .marinara.json preset envelope with data.preset.parameters = {"temperature":99,"topP":5}.
  2. Import via POST /api/import (importMarinara -> importPreset -> storage.create).
  3. Set the imported preset on a chat and generate.

You'll see: parsePresetParameters returns temperature 99 / topP 5 with no clamping, and those values are forwarded to the provider request (which typically just rejects with a 400).
Expected: parameters are clamped/validated to schema ranges before reaching the provider.

Where it is
packages/server/src/services/import/marinara.importer.ts:565 (parameters: safeParseJson(p.parameters, {})) and 658-668 (passthrough); packages/server/src/services/storage/prompts.storage.ts:76 (JSON.stringify with no validation); read path packages/server/src/services/prompt/assembler.ts:215-225 (parsePresetParameters, no clamp); reaches provider at generate.routes.ts:2288-2290 and 6543-6548. The connection/chat stored-params path DOES clamp via generationParametersSchema.partial().safeParse (generate-route-utils.ts:1065), confirming only the preset-assembler path is unclamped.

What's happening
The import/seed write paths bypass generationParametersSchema, and neither storage nor the assembler re-validates the stored object. The Zod range checks only run on the interactive create/update routes.

Suggested fix
Clamp at the single read chokepoint parsePresetParameters() in packages/server/src/services/prompt/assembler.ts:215-225, using the NON-throwing safeParse with per-field salvage, mirroring the existing convention at generate-route-utils.ts:1065. This covers import, seed, AND presets already stored from a previously-imported bad file, for all chat modes. Add generationParametersSchema to the shared import (line 21) and replace the body so it (1) merges defaults + parsed, (2) tries generationParametersSchema.safeParse(merged) and returns .data on success, (3) on failure re-validates each field individually with the schema's per-field safeParse, keeping valid fields and falling back to the in-range default for any rejected field, and (4) never throws.

Regression avoided: do NOT use the throwing generationParametersSchema.parse(...) — parsePresetParameters and importPreset currently never throw, and a single wrong-TYPED field in a legacy/hand-edited file (e.g. temperature: "hot") would then abort the import or break the chat for every mode. The per-field-salvage safeParse approach clamps out-of-range values, tolerates legacy malformed fields, and never throws. (Defense-in-depth: optionally also validate at write time in importPreset and the seed with generationParametersSchema.partial().safeParse, but the read-path clamp alone resolves the bug.)


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/3029 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The interactive preset CRUD routes validate generation parameters against a Zod schema (temperature 0..2, topP 0..1, etc.), but the import and seed paths write parameters straight to storage with no validation, and the assembler's read path performs no clamping either. So a hand-edited or malicious `.marinara.json` whose `parameters` contains out-of-range values is stored and later forwarded to the LLM provider unclamped. **How to reproduce (try it yourself)** 1. Craft a `.marinara.json` preset envelope with `data.preset.parameters` = `{"temperature":99,"topP":5}`. 2. Import via `POST /api/import` (`importMarinara` -> `importPreset` -> `storage.create`). 3. Set the imported preset on a chat and generate. You'll see: `parsePresetParameters` returns temperature 99 / topP 5 with no clamping, and those values are forwarded to the provider request (which typically just rejects with a 400). Expected: parameters are clamped/validated to schema ranges before reaching the provider. **Where it is** `packages/server/src/services/import/marinara.importer.ts:565` (`parameters: safeParseJson(p.parameters, {})`) and `658-668` (passthrough); `packages/server/src/services/storage/prompts.storage.ts:76` (`JSON.stringify` with no validation); read path `packages/server/src/services/prompt/assembler.ts:215-225` (`parsePresetParameters`, no clamp); reaches provider at `generate.routes.ts:2288-2290` and `6543-6548`. The connection/chat stored-params path DOES clamp via `generationParametersSchema.partial().safeParse` (`generate-route-utils.ts:1065`), confirming only the preset-assembler path is unclamped. **What's happening** The import/seed write paths bypass `generationParametersSchema`, and neither storage nor the assembler re-validates the stored object. The Zod range checks only run on the interactive create/update routes. **Suggested fix** Clamp at the single read chokepoint `parsePresetParameters()` in `packages/server/src/services/prompt/assembler.ts:215-225`, using the NON-throwing `safeParse` with per-field salvage, mirroring the existing convention at `generate-route-utils.ts:1065`. This covers import, seed, AND presets already stored from a previously-imported bad file, for all chat modes. Add `generationParametersSchema` to the shared import (line 21) and replace the body so it (1) merges defaults + parsed, (2) tries `generationParametersSchema.safeParse(merged)` and returns `.data` on success, (3) on failure re-validates each field individually with the schema's per-field `safeParse`, keeping valid fields and falling back to the in-range default for any rejected field, and (4) never throws. Regression avoided: do NOT use the throwing `generationParametersSchema.parse(...)` — `parsePresetParameters` and `importPreset` currently never throw, and a single wrong-TYPED field in a legacy/hand-edited file (e.g. `temperature: "hot"`) would then abort the import or break the chat for every mode. The per-field-salvage `safeParse` approach clamps out-of-range values, tolerates legacy malformed fields, and never throws. (Defense-in-depth: optionally also validate at write time in `importPreset` and the seed with `generationParametersSchema.partial().safeParse`, but the read-path clamp alone resolves the bug.) --- *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 on staging in PR #3031 (merged as f542e282). Closing manually because GitHub auto-close keywords only close issues when merged into the default branch (main), not staging.

<!-- gh-comment-id:4834698182 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed on staging in PR #3031 (merged as f542e282). Closing manually because GitHub auto-close keywords only close issues when merged into the default branch (main), not staging.
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#1600
No description provided.