mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2524] [Issue]: Gallery upload/delete path traversal — unsanitized chatId/filePath, no assertInsideDir #1251
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#1251
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 @Romuromylus on GitHub (Jun 9, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2524
Originally assigned to: @Romuromylus on GitHub.
Summary
Severity: Medium — security (path traversal). The gallery routes join request-controlled path segments into on-disk paths with no traversal sanitization and no
assertInsideDirbackstop. The upload handler joins an unsanitizedchatIdURL param into the write path, and the delete handler unlinksjoin(GALLERY_DIR, image.filePath)for afilePaththat the upload can poison. Both deviate from the sibling avatars/game-assets handlers, which validate the segment and wrap the target inassertInsideDir.Expected behavior
The upload write target and the delete unlink target should both be validated (reject
..,/,\, empty, null bytes onchatId) and wrapped inassertInsideDir(GALLERY_DIR, ...)before any filesystem op, mirroringavatars.routes.tsand everygame-assets.routes.tsmutating handler. The gallery serve route already rejects traversal onchatId(gallery.routes.ts:114) — upload and delete should match it.Actual behavior
Upload (
packages/server/src/routes/gallery.routes.ts:17-23, 69-85):Fastify decodes a percent-encoded
chatId, soPOST /api/gallery/%2e%2e%2f%2e%2e%2fevil/uploadresolveschatIdto../../eviland writes attacker-controlled image bytes into a recursively-created directory outsideGALLERY_DIR.Delete (
gallery.routes.ts:127-142):The stored
filePathis built from the same unsanitizedchatId, so an upload with a traversalchatIdpersists a row whosefilePathescapesGALLERY_DIR; deleting that row thenunlinkSyncs outside the gallery root.Steps to reproduce
POST /api/gallery/%2e%2e%2f%2e%2e%2fevil/uploadwith a multipartfilefield (any allowed image ext). The bytes land in a directory outsideGALLERY_DIR, and the storedchatImages.filePathis../../evil/<newId>.png.DELETE /api/gallery/<that row id>→unlinkSync(join(GALLERY_DIR, "../../evil/<newId>.png")), deleting a file outside the gallery root.Environment
staging @ a9fdae8)Logs, screenshots, or video
Static source audit, cited to file:line above.
Additional context
Fix: validate
chatId(reject..,/,\, empty, null bytes) and wrap the upload target inassertInsideDir(GALLERY_DIR, join(GALLERY_DIR, chatId, filename)); on delete, runassertInsideDir(GALLERY_DIR, join(GALLERY_DIR, image.filePath))beforeunlinkSyncfor defense-in-depth. The reference helperassertInsideDirlives inutils/security.ts:59-73and is already used byavatars.routes.ts:83/114,backgrounds.routes.ts, and every mutatinggame-assets.routes.tshandler (e.g.:643-647,:928-931). The in-file serve route's ownchatIdcheck (:114) is the precedent the upload/delete omit.CSRF (
csrf-protection.ts, Sec-Fetch-Site + untrusted-Origin rejection) blocks a pure cross-site drive-by, so this requires a valid same-origin authenticated session — hence Medium, not High. The nanoid filename makes existing-file overwrite impractical, but the write-outside-root and delete-outside-root primitives are real.Regression notes: found via static audit; both handlers are byte-identical on
main(65a0395), so this is a long-standing shared bug, not amain→stagingregression.Template check
Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked: