[GH-ISSUE #3060] [Issue]: Chat-gallery upload never verifies the chat exists, so an upload to a non-existent chatId writes a permanent orphan gallery and DB row #1628

Closed
opened 2026-10-21 05:41:03 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

The chat-gallery upload route POST /api/gallery/:chatId/upload validates the chatId only with isValidChatId — a pure path-safety check (rejects empty, .., /, \, null byte) that does NOT verify the chat actually exists. It then creates the gallery directory, streams the file to disk, and inserts a chat_images row for that chatId. So an upload addressed to a path-safe but non-existent chatId succeeds: it creates data/gallery/<phantom>/<id>.png plus a DB row for a chat that does not exist.

The sibling read routes in the same file — GET /api/gallery/asset/:chatId/* and GET /api/gallery/assets/:chatId — both load the chat with chats.getById(chatId) and return 404 "Chat not found" when it is missing, so the write route is inconsistent with its own siblings. The orphaned gallery is unreachable from the UI (there is no chat to open), and gallery-recovery only re-adopts files for chats that exist, so it becomes a permanent leak the API cannot surface or clean up.

How to reproduce (try it yourself)

  1. Start the server.
  2. POST a multipart upload with a .png file part to /api/gallery/this-chat-does-not-exist/upload (any string that passes the path-safety check but is not a real chat id).
  3. Inspect data/gallery/ and the chat_images table.

You'll see: the request returns 200 with an image record, a directory data/gallery/this-chat-does-not-exist/ is created with the file, and a chat_images row is inserted — all for a chat that does not exist. By contrast, GET /api/gallery/assets/this-chat-does-not-exist returns 404 "Chat not found".

Expected: the upload route rejects a non-existent chat with 404 "Chat not found", matching its sibling read routes, so no orphan gallery directory or DB row is created.

Where it is

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

  • Upload handler POST /:chatId/upload lines 372-420 — validates only isValidChatId(chatId) (lines 373-375), then ensureDir(chatId) (387), await pipeline(...) (396), and storage.create({ chatId, ... }) (406). No chat-existence check anywhere in the handler.
  • Contrast siblings: GET /asset/:chatId/* lines 243-244 and GET /assets/:chatId lines 273-274 both do const chat = await chats.getById(chatId); if (!chat) return reply.status(404).send({ error: "Chat not found" });.
  • chats storage is instantiated at line 186 (const chats = createChatsStorage(app.db);) and already used by both sibling routes.

What's happening

isValidChatId (lines 41-49) is a path-traversal guard, not an existence check. The upload route relies on it alone, so any well-formed chatId — including one with no corresponding chat — is accepted and persisted. The sibling read routes enforce existence; the write route does not, so it can create gallery data for chats that never existed.

Suggested fix

Add the same existence check the sibling routes use, immediately after the isValidChatId guard in the upload handler (after line 375, before req.file()):

const chat = await chats.getById(chatId);
if (!chat) {
  return reply.status(404).send({ error: "Chat not found" });
}

This mirrors lines 243-244 / 273-274 exactly and uses the chats storage already instantiated at line 186.

Regression avoided: legitimate gallery uploads always target the chat currently open in the client (the chatId comes from an existing, persisted chat), and the sibling asset routes already assume the chat exists, so adding the check rejects only orphan-creating uploads. Place the check FIRST — before req.file(), the pipeline write, and storage.create — so a rejected upload leaves nothing on disk or in the DB. This is the chat-gallery route only; the global-gallery upload has no chat and instead already validates its folderId (coercing a missing folder to root), so it needs no change.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3060 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The chat-gallery upload route `POST /api/gallery/:chatId/upload` validates the `chatId` only with `isValidChatId` — a pure path-safety check (rejects empty, `..`, `/`, `\`, null byte) that does NOT verify the chat actually exists. It then creates the gallery directory, streams the file to disk, and inserts a `chat_images` row for that chatId. So an upload addressed to a path-safe but non-existent chatId succeeds: it creates `data/gallery/<phantom>/<id>.png` plus a DB row for a chat that does not exist. The sibling read routes in the same file — `GET /api/gallery/asset/:chatId/*` and `GET /api/gallery/assets/:chatId` — both load the chat with `chats.getById(chatId)` and return 404 "Chat not found" when it is missing, so the write route is inconsistent with its own siblings. The orphaned gallery is unreachable from the UI (there is no chat to open), and gallery-recovery only re-adopts files for chats that exist, so it becomes a permanent leak the API cannot surface or clean up. **How to reproduce (try it yourself)** 1. Start the server. 2. `POST` a multipart upload with a `.png` file part to `/api/gallery/this-chat-does-not-exist/upload` (any string that passes the path-safety check but is not a real chat id). 3. Inspect `data/gallery/` and the `chat_images` table. You'll see: the request returns 200 with an image record, a directory `data/gallery/this-chat-does-not-exist/` is created with the file, and a `chat_images` row is inserted — all for a chat that does not exist. By contrast, `GET /api/gallery/assets/this-chat-does-not-exist` returns 404 "Chat not found". Expected: the upload route rejects a non-existent chat with 404 "Chat not found", matching its sibling read routes, so no orphan gallery directory or DB row is created. **Where it is** `packages/server/src/routes/gallery.routes.ts`: - Upload handler `POST /:chatId/upload` lines 372-420 — validates only `isValidChatId(chatId)` (lines 373-375), then `ensureDir(chatId)` (387), `await pipeline(...)` (396), and `storage.create({ chatId, ... })` (406). No chat-existence check anywhere in the handler. - Contrast siblings: `GET /asset/:chatId/*` lines 243-244 and `GET /assets/:chatId` lines 273-274 both do `const chat = await chats.getById(chatId); if (!chat) return reply.status(404).send({ error: "Chat not found" });`. - `chats` storage is instantiated at line 186 (`const chats = createChatsStorage(app.db);`) and already used by both sibling routes. **What's happening** `isValidChatId` (lines 41-49) is a path-traversal guard, not an existence check. The upload route relies on it alone, so any well-formed chatId — including one with no corresponding chat — is accepted and persisted. The sibling read routes enforce existence; the write route does not, so it can create gallery data for chats that never existed. **Suggested fix** Add the same existence check the sibling routes use, immediately after the `isValidChatId` guard in the upload handler (after line 375, before `req.file()`): ```ts const chat = await chats.getById(chatId); if (!chat) { return reply.status(404).send({ error: "Chat not found" }); } ``` This mirrors lines 243-244 / 273-274 exactly and uses the `chats` storage already instantiated at line 186. Regression avoided: legitimate gallery uploads always target the chat currently open in the client (the chatId comes from an existing, persisted chat), and the sibling asset routes already assume the chat exists, so adding the check rejects only orphan-creating uploads. Place the check FIRST — before `req.file()`, the `pipeline` write, and `storage.create` — so a rejected upload leaves nothing on disk or in the DB. This is the chat-gallery route only; the global-gallery upload has no chat and instead already validates its `folderId` (coercing a missing folder to root), so it needs no change. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@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

<!-- gh-comment-id:4835429573 --> @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
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in PR #3064 on branch codex/issue-sweep-3054-3063 (commit 3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.

<!-- gh-comment-id:4835579295 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in PR #3064 on branch `codex/issue-sweep-3054-3063` (commit 3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.
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#1628
No description provided.