mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3057] [Issue]: Unvalidated sprite-sheet cols/rows silently drop all expressions or crash slicing after a paid generation #1625
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1625
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3057
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
POST /api/sprites/generate-sheet(and its preview) takecols/rowsstraight from the request body with no validation, clamping, or integer check. The values feedexpressions.slice(0, cols * rows)and the cell-slicing math. Two concrete failures occur, both AFTER the (typically paid) provider image generation has already run:colsis a non-numeric string,cols * rowsisNaN, soslice(0, NaN)becomesslice(0, 0)->[]. The entry guard only checks the RAWbody.expressions, not the sliced plan, so the request passes. The slicing loop never iterates, and the handler returns 200 with a populatedsheetBase64butcells: []— zero sprites, no error.cols: 1.5,idx = row * plan.cols + colis non-integer andleft = col * Math.floor(imgWidth / 1.5)can exceed the image width, so sharp.extract()rejects with 'bad extract area',Promise.allrejects, and the handler returns 500 — again only after the image was generated.How to reproduce (try it yourself)
POST /api/sprites/generate-sheetwith a validconnectionId,appearance,expressions: ["happy","sad"], andcols: "x"(any non-numeric).cols: 1.5, rows: 2.You'll see: case (a) returns 200 with
cells: []and a populatedsheetBase64, no error, despite a successful image generation; case (b) generates the image then returns 500 ('... bad extract area').Expected: a malformed
cols/rowsis rejected with a 400 BEFORE any provider call, so no generation is wasted; valid requests always slice on positive integers.Where it is
packages/server/src/routes/sprites.routes.ts:buildSpritePromptPlanlines 1086-1089 —const cols = body.cols ?? 2; const rows = body.rows ?? 3;thenslice(0, cols * rows);??only catches null/undefined, not a string or fractional number.body.expressionsonly.generateImagecall line 1824 — runs before slicing.Math.floor(imgWidth / plan.cols),idx = row * plan.cols + col,sharp.extract.routes/index.ts:72.What's happening
cols/rowsare accepted from untrusted input without coercion to a positive integer. The TSnumber?type on the body provides no runtime protection (the handler doesreq.body as SpriteGenerateSheetBody). The entry guard checks only the raw expressions array, not the post-slice plan, and the slicing math assumes positive integers.Suggested fix
Two surgical changes in
sprites.routes.ts:(A) Coerce inside
buildSpritePromptPlansoplan.cols/plan.rowsare always safe positive integers for both routes and all internal math. Replace lines 1086-1087 withconst cols = coerceSpriteGridDimension(body.cols, 2);/const rows = coerceSpriteGridDimension(body.rows, 3);and add:Number(undefined)isNaN, so an omitted value falls back to the existing defaults (2/3) — happy path preserved.(B) In the
/generate-sheethandler, BEFORE any provider work (right after the expressions guard at line 1690), reject only PRESENT-but-invalid values so omitted values still use defaults:Then after the plan is built (after line 1711), guard the sliced result:
Regression avoided: the original suggestion to return a 400 from inside
buildSpritePromptPlancannot work — that function returns aSpritePromptPlan, not a Fastify reply, and a throw there would surface as a 500 and also wrongly reject the provider-free preview route. Splitting coercion (in the plan builder, for both routes) from the 400 validation (in the handler, before the provider call) keeps the preview route working and avoids over-rejecting the omitted-value case.AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Claiming this for the current staging sweep in draft PR #3064: https://github.com/Pasta-Devs/Marinara-Engine/pull/3064
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Fixed in PR #3064 on branch
codex/issue-sweep-3054-3063(commit3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.