[GH-ISSUE #2589] Harden pre-existing character + chat gallery routes (path traversal, orphaned files) #1276

Closed
opened 2026-10-21 05:30:32 +07:00 by bsakaguchi · 0 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

While addressing the CodeRabbit review on #2584 (persona + global galleries), the same path-handling / robustness patterns turned up in the pre-existing character and chat gallery routes. These predate the galleries work and aren't in the #2584 / #2585 diffs, so fixing them there would be out of scope — tracking here instead.

Findings

  1. Path traversal in DELETE /api/characters/:id (most serious). In packages/server/src/routes/characters.routes.ts, the character delete route derives galleryDir = join(CHARACTER_GALLERY_ROOT, req.params.id) and calls rmSync(galleryDir, { recursive: true, force: true }). An id of .. resolves to data/gallery and would recursively delete the entire gallery tree. (The persona equivalent was hardened in #2584; the character route has the identical pattern.)

  2. Backslash not blocked in gallery file-serve routes. The character gallery file route (GET /:id/gallery/file/:filename) and the chat gallery file route (gallery.routes.ts) reject .. and / but not \, which is a path separator on Windows.

  3. No assertInsideDir before unlink in the character gallery image-delete route. join(DATA_DIR, "gallery", image.filePath) is unlinked without a canonical containment check. The chat gallery delete already uses assertInsideDir; the character one does not.

  4. No rollback on upload metadata failure. The character (/:id/gallery/upload) and chat (/:chatId/upload) uploads write bytes, then insert the DB row; a failed insert strands an orphan file on disk. (Fixed for persona + global in #2584.)

  5. Silent delete failures (minor UX). The character gallery tab delete (CharacterEditor.tsx) calls remove.mutate without awaiting/handling the result, so a failed delete isn't surfaced to the user. (Fixed for the persona tab in #2584.)

Notes

This is a local, CSRF-protected single-user app, so the threat surface is limited — but #1 in particular is a real footgun worth closing (defense in depth). All of these mirror fixes already applied to the new persona/global routes in #2584, so the patches should be straightforward to port across.

Originally created by @kolacheee on GitHub (Jun 16, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2589 Originally assigned to: @SpicyMarinara on GitHub. ## Summary While addressing the CodeRabbit review on #2584 (persona + global galleries), the same path-handling / robustness patterns turned up in the **pre-existing** character and chat gallery routes. These predate the galleries work and aren't in the #2584 / #2585 diffs, so fixing them there would be out of scope — tracking here instead. ## Findings 1. **Path traversal in `DELETE /api/characters/:id` (most serious).** In `packages/server/src/routes/characters.routes.ts`, the character delete route derives `galleryDir = join(CHARACTER_GALLERY_ROOT, req.params.id)` and calls `rmSync(galleryDir, { recursive: true, force: true })`. An `id` of `..` resolves to `data/gallery` and would recursively delete the entire gallery tree. (The persona equivalent was hardened in #2584; the character route has the identical pattern.) 2. **Backslash not blocked in gallery file-serve routes.** The character gallery file route (`GET /:id/gallery/file/:filename`) and the chat gallery file route (`gallery.routes.ts`) reject `..` and `/` but not `\`, which is a path separator on Windows. 3. **No `assertInsideDir` before unlink in the character gallery image-delete route.** `join(DATA_DIR, "gallery", image.filePath)` is unlinked without a canonical containment check. The chat gallery delete already uses `assertInsideDir`; the character one does not. 4. **No rollback on upload metadata failure.** The character (`/:id/gallery/upload`) and chat (`/:chatId/upload`) uploads write bytes, then insert the DB row; a failed insert strands an orphan file on disk. (Fixed for persona + global in #2584.) 5. **Silent delete failures (minor UX).** The character gallery tab delete (`CharacterEditor.tsx`) calls `remove.mutate` without awaiting/handling the result, so a failed delete isn't surfaced to the user. (Fixed for the persona tab in #2584.) ## Notes This is a local, CSRF-protected single-user app, so the threat surface is limited — but #1 in particular is a real footgun worth closing (defense in depth). All of these mirror fixes already applied to the new persona/global routes in #2584, so the patches should be straightforward to port across.
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#1276
No description provided.