[PR #2533] [MERGED] fix(gallery): block path traversal in upload/delete handlers #5636

Closed
opened 2026-10-22 04:18:01 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2533
Author: @Romuromylus
Created: 6/9/2026
Status: ✅ Merged
Merged: 6/9/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/gallery-path-traversal


📝 Commits (2)

  • 614fbc2 fix(gallery): block path traversal in upload/delete via chatId guard + assertInsideDir
  • 9445fa4 Merge branch 'staging' into fix/gallery-path-traversal

📊 Changes

1 file changed (+32 additions, -5 deletions)

View changed files

📝 packages/server/src/routes/gallery.routes.ts (+32 -5)

📄 Description

Linked issue

Closes #2524

Why this change

The chat-gallery upload and delete handlers built on-disk paths from request-controlled input with no traversal sanitization and no assertInsideDir backstop — unlike the sibling avatars/game-assets routes.

  • Upload joined req.params.chatId straight into the write path. Fastify percent-decodes the param, so POST /api/gallery/%2e%2e%2f%2e%2e%2fevil/upload yields chatId = "../../evil" and writes attacker bytes outside GALLERY_DIR.
  • Delete unlinked join(GALLERY_DIR, image.filePath), where filePath was persisted from the same unsanitized chatId at upload time — so a poisoned row could unlink a file outside the gallery root.

What changed

packages/server/src/routes/gallery.routes.ts:

  • Added isValidChatId() (rejects .., /, \, empty, and NUL byte) and check it at the top of the upload handler, before ensureDir runs mkdirSync — so a traversal chatId never creates a directory outside the root.
  • Wrapped the upload write target in assertInsideDir(GALLERY_DIR, ...) as defense-in-depth (400 on escape).
  • Wrapped the delete unlink target in assertInsideDir(GALLERY_DIR, ...); on escape it logs an err-first Pino warn and still falls through to storage.remove(id) so a poisoned/orphan row is cleaned up rather than left undeletable.

Mirrors avatars.routes.ts (isValidFilename) and game-assets.routes.ts (assertInsideDir). The serve route already guarded chatId but omitted backslash; the new helper includes it.

Not a regression: both handlers are byte-identical on main; long-standing shared bug.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

pnpm check passes locally. Added a local-only node:test (gallery-path-traversal.test.ts, gitignored): isValidChatId rejects traversal/separators/empty/NUL and accepts legitimate nanoids (negative control), and assertInsideDir returns the resolved path for an in-root target (negative control) and throws on an escaping/poisoned filePath. Local run: 4/4 pass.

Still to verify manually against the deploy:

  • Upload to an encoded-traversal path (/api/gallery/%2e%2e%2f%2e%2e%2fevil/upload) now returns 400 Invalid chatId.
  • Upload to a real chatId still succeeds and the image appears in that chat's gallery (happy path unaffected).
  • Deleting a normally-uploaded image still unlinks the file and removes the row.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2533 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/9/2026 **Status:** ✅ Merged **Merged:** 6/9/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/gallery-path-traversal` --- ### 📝 Commits (2) - [`614fbc2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/614fbc2dec86bb89ffa99a6d51c0abf6463df5ee) fix(gallery): block path traversal in upload/delete via chatId guard + assertInsideDir - [`9445fa4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9445fa48072e1eee97cefa6ec54cc7faf361b13a) Merge branch 'staging' into fix/gallery-path-traversal ### 📊 Changes **1 file changed** (+32 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/gallery.routes.ts` (+32 -5) </details> ### 📄 Description ## Linked issue Closes #2524 ## Why this change The chat-gallery upload and delete handlers built on-disk paths from request-controlled input with no traversal sanitization and no `assertInsideDir` backstop — unlike the sibling avatars/game-assets routes. - **Upload** joined `req.params.chatId` straight into the write path. Fastify percent-decodes the param, so `POST /api/gallery/%2e%2e%2f%2e%2e%2fevil/upload` yields `chatId = "../../evil"` and writes attacker bytes outside `GALLERY_DIR`. - **Delete** unlinked `join(GALLERY_DIR, image.filePath)`, where `filePath` was persisted from the same unsanitized `chatId` at upload time — so a poisoned row could unlink a file outside the gallery root. ## What changed `packages/server/src/routes/gallery.routes.ts`: - Added `isValidChatId()` (rejects `..`, `/`, `\`, empty, and NUL byte) and check it at the top of the upload handler, **before** `ensureDir` runs `mkdirSync` — so a traversal `chatId` never creates a directory outside the root. - Wrapped the upload write target in `assertInsideDir(GALLERY_DIR, ...)` as defense-in-depth (400 on escape). - Wrapped the delete unlink target in `assertInsideDir(GALLERY_DIR, ...)`; on escape it logs an err-first Pino warn and still falls through to `storage.remove(id)` so a poisoned/orphan row is cleaned up rather than left undeletable. Mirrors `avatars.routes.ts` (`isValidFilename`) and `game-assets.routes.ts` (`assertInsideDir`). The serve route already guarded `chatId` but omitted backslash; the new helper includes it. Not a regression: both handlers are byte-identical on `main`; long-standing shared bug. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes `pnpm check` passes locally. Added a local-only node:test (`gallery-path-traversal.test.ts`, gitignored): `isValidChatId` rejects traversal/separators/empty/NUL and accepts legitimate nanoids (negative control), and `assertInsideDir` returns the resolved path for an in-root target (negative control) and throws on an escaping/poisoned `filePath`. Local run: **4/4 pass**. Still to verify manually against the deploy: - Upload to an encoded-traversal path (`/api/gallery/%2e%2e%2f%2e%2e%2fevil/upload`) now returns 400 `Invalid chatId`. - Upload to a real `chatId` still succeeds and the image appears in that chat's gallery (happy path unaffected). - Deleting a normally-uploaded image still unlinks the file and removes the row. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:18:01 +07:00
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#5636
No description provided.