[GH-ISSUE #2524] [Issue]: Gallery upload/delete path traversal — unsanitized chatId/filePath, no assertInsideDir #1251

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

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 assertInsideDir backstop. The upload handler joins an unsanitized chatId URL param into the write path, and the delete handler unlinks join(GALLERY_DIR, image.filePath) for a filePath that the upload can poison. Both deviate from the sibling avatars/game-assets handlers, which validate the segment and wrap the target in assertInsideDir.

Expected behavior

The upload write target and the delete unlink target should both be validated (reject .., /, \, empty, null bytes on chatId) and wrapped in assertInsideDir(GALLERY_DIR, ...) before any filesystem op, mirroring avatars.routes.ts and every game-assets.routes.ts mutating handler. The gallery serve route already rejects traversal on chatId (gallery.routes.ts:114) — upload and delete should match it.

Actual behavior

Upload (packages/server/src/routes/gallery.routes.ts:17-23, 69-85):

function ensureDir(chatId: string) {
  const dir = join(GALLERY_DIR, chatId);
  if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
  return dir;
}
// ...
app.post<{ Params: { chatId: string } }>("/:chatId/upload", async (req, reply) => {
  const { chatId } = req.params;          // no '..'/absolute/null-byte check
  const data = await req.file();
  const dir = ensureDir(chatId);          // join(GALLERY_DIR, chatId)
  const filename = `${newId()}${ext}`;
  const filePath = join(dir, filename);    // no assertInsideDir
  await pipeline(data.file, createWriteStream(filePath));

Fastify decodes a percent-encoded chatId, so POST /api/gallery/%2e%2e%2f%2e%2e%2fevil/upload resolves chatId to ../../evil and writes attacker-controlled image bytes into a recursively-created directory outside GALLERY_DIR.

Delete (gallery.routes.ts:127-142):

app.delete<{ Params: { id: string } }>("/:id", async (req, reply) => {
  const image = await storage.getById(id);
  const filePath = join(GALLERY_DIR, image.filePath);  // filePath can be '../../evil/<id>.png'
  if (existsSync(filePath)) unlinkSync(filePath);       // no assertInsideDir

The stored filePath is built from the same unsanitized chatId, so an upload with a traversal chatId persists a row whose filePath escapes GALLERY_DIR; deleting that row then unlinkSyncs outside the gallery root.

Steps to reproduce

  1. As an authenticated same-origin user, POST /api/gallery/%2e%2e%2f%2e%2e%2fevil/upload with a multipart file field (any allowed image ext). The bytes land in a directory outside GALLERY_DIR, and the stored chatImages.filePath is ../../evil/<newId>.png.
  2. DELETE /api/gallery/<that row id> → unlinkSync(join(GALLERY_DIR, "../../evil/<newId>.png")), deleting a file outside the gallery root.

Environment

  • Marinara Engine version: 1.6.1 (staging @ a9fdae8)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

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 in assertInsideDir(GALLERY_DIR, join(GALLERY_DIR, chatId, filename)); on delete, run assertInsideDir(GALLERY_DIR, join(GALLERY_DIR, image.filePath)) before unlinkSync for defense-in-depth. The reference helper assertInsideDir lives in utils/security.ts:59-73 and is already used by avatars.routes.ts:83/114, backgrounds.routes.ts, and every mutating game-assets.routes.ts handler (e.g. :643-647, :928-931). The in-file serve route's own chatId check (: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 a main→staging regression.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
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 `assertInsideDir` backstop. The upload handler joins an unsanitized `chatId` URL param into the write path, and the delete handler unlinks `join(GALLERY_DIR, image.filePath)` for a `filePath` that the upload can poison. Both deviate from the sibling avatars/game-assets handlers, which validate the segment and wrap the target in `assertInsideDir`. ## Expected behavior The upload write target and the delete unlink target should both be validated (reject `..`, `/`, `\`, empty, null bytes on `chatId`) and wrapped in `assertInsideDir(GALLERY_DIR, ...)` before any filesystem op, mirroring `avatars.routes.ts` and every `game-assets.routes.ts` mutating handler. The gallery serve route already rejects traversal on `chatId` (`gallery.routes.ts:114`) — upload and delete should match it. ## Actual behavior Upload (`packages/server/src/routes/gallery.routes.ts:17-23, 69-85`): ```ts function ensureDir(chatId: string) { const dir = join(GALLERY_DIR, chatId); if (!existsSync(dir)) mkdirSync(dir, { recursive: true }); return dir; } // ... app.post<{ Params: { chatId: string } }>("/:chatId/upload", async (req, reply) => { const { chatId } = req.params; // no '..'/absolute/null-byte check const data = await req.file(); const dir = ensureDir(chatId); // join(GALLERY_DIR, chatId) const filename = `${newId()}${ext}`; const filePath = join(dir, filename); // no assertInsideDir await pipeline(data.file, createWriteStream(filePath)); ``` Fastify decodes a percent-encoded `chatId`, so `POST /api/gallery/%2e%2e%2f%2e%2e%2fevil/upload` resolves `chatId` to `../../evil` and writes attacker-controlled image bytes into a recursively-created directory outside `GALLERY_DIR`. Delete (`gallery.routes.ts:127-142`): ```ts app.delete<{ Params: { id: string } }>("/:id", async (req, reply) => { const image = await storage.getById(id); const filePath = join(GALLERY_DIR, image.filePath); // filePath can be '../../evil/<id>.png' if (existsSync(filePath)) unlinkSync(filePath); // no assertInsideDir ``` The stored `filePath` is built from the same unsanitized `chatId`, so an upload with a traversal `chatId` persists a row whose `filePath` escapes `GALLERY_DIR`; deleting that row then `unlinkSync`s outside the gallery root. ## Steps to reproduce 1. As an authenticated same-origin user, `POST /api/gallery/%2e%2e%2f%2e%2e%2fevil/upload` with a multipart `file` field (any allowed image ext). The bytes land in a directory outside `GALLERY_DIR`, and the stored `chatImages.filePath` is `../../evil/<newId>.png`. 2. `DELETE /api/gallery/<that row id>` → `unlinkSync(join(GALLERY_DIR, "../../evil/<newId>.png"))`, deleting a file outside the gallery root. ## Environment - Marinara Engine version: 1.6.1 (`staging @ a9fdae8`) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## 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 in `assertInsideDir(GALLERY_DIR, join(GALLERY_DIR, chatId, filename))`; on delete, run `assertInsideDir(GALLERY_DIR, join(GALLERY_DIR, image.filePath))` before `unlinkSync` for defense-in-depth. The reference helper `assertInsideDir` lives in `utils/security.ts:59-73` and is already used by `avatars.routes.ts:83/114`, `backgrounds.routes.ts`, and every mutating `game-assets.routes.ts` handler (e.g. `:643-647`, `:928-931`). The in-file serve route's own `chatId` check (`: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 a `main`→`staging` regression. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:29:59 +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#1251
No description provided.