mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3061] [Issue]: Chat-gallery and global-gallery uploads accept arbitrary file content (extension-only validation, no magic-byte check) #1631
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#1631
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/3061
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
POST /api/gallery/:chatId/uploadandPOST /api/global-gallery/uploadvalidate the upload ONLY by the client-supplied filename extension and then stream the raw bytes to disk. They never inspect file content, so any bytes — HTML, a script, an arbitrary binary — can be stored as long as the filename ends in .png/.jpg/.gif/.webp/.avif. The stored file is later served back with a content type derived from its extension. By contrast, backgrounds and avatars verify magic bytes viaisAllowedImageBufferbefore persisting.The direct-load XSS vector is mitigated because the global security-headers hook sets
X-Content-Type-Options: nosniff, so a fake-PNG HTML file will not be sniffed as HTML. But the content-validation invariant is still bypassed: non-image data is accepted into the gallery and served under an image MIME type — exactly what the magic-byte check on the other routes exists to prevent.How to reproduce (try it yourself)
POST /api/gallery/<chatId>/uploadwith a file part namedevil.pngwhose body is<html><script>...</script></html>(or any non-image bytes).GETthe returned url/api/gallery/file/<chatId>/<id>.png.You'll see: the upload succeeds (200), a
chat_imagesrecord is created, and the GET returns the non-image bytes withContent-Type: image/png. The same works for/api/global-gallery/upload.Expected: backgrounds (
/api/backgrounds/upload) and NPC avatars reject the identical payload with 'Unsupported or invalid image file' because they magic-byte-check the buffer; the gallery routes should too.Where it is
packages/server/src/routes/gallery.routes.ts:382-396(upload handler 372-420) —const ext = extname(data.filename); if (!ALLOWED_EXTS.has(ext)) {...}thenawait pipeline(data.file, createWriteStream(filePath)); no content inspection.packages/server/src/routes/global-gallery.routes.ts:89-112(upload handler 83-142) — identical pattern.backgrounds.routes.ts:143(isAllowedImageBuffer(buffer, ext)),avatars.routes.ts:109. Validator:packages/server/src/utils/security.ts:82-121.gallery.routes.ts:434,global-gallery.routes.ts:155.What's happening
Upload validation relies solely on the attacker-controlled filename extension;
isAllowedImageBuffer(or any content sniff) is never called on the uploaded bytes for these two routes.Suggested fix
Mirror
backgrounds.routes.ts:118-146in both gallery upload handlers: take the file with an explicit per-call size limit, buffer it with size-limit handling, magic-byte validate, then write. Addconst GALLERY_UPLOAD_MAX_BYTES = 20 * 1024 * 1024;and importisAllowedImageBufferfrom../utils/security.jsandwriteFilefromfs/promises. In each handler:data.fieldsstays readable afterward, so the prompt/provider/model/width/height block and the storage create are unchanged. For global-gallery, validating before the write means no orphan can be stranded by a rejected upload; leave the existing DB-rollback try/catch (lines 121-141) as-is.Regression avoided: the current code uses a streaming write that never holds the whole file in memory, but the global multipart
fileSizecap is 256 MB. A naivedata.toBuffer()with no per-call limit would buffer up to 256 MB per upload, and both gallery clients fan out withPromise.allSettledover a file array, so several such buffers could be held at once — a memory-exhaustion regression. Passing the tight per-callGALLERY_UPLOAD_MAX_BYTESlimit and catchingFST_REQ_FILE_TOO_LARGE(exactly as backgrounds does) avoids it. The dot-prefixed lowercasedextis the formisAllowedImageBufferexpects (its AVIF branch gates onexpectedExt === ".avif").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.