mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[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
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#1628
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 @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/uploadvalidates thechatIdonly withisValidChatId— 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 achat_imagesrow for that chatId. So an upload addressed to a path-safe but non-existent chatId succeeds: it createsdata/gallery/<phantom>/<id>.pngplus a DB row for a chat that does not exist.The sibling read routes in the same file —
GET /api/gallery/asset/:chatId/*andGET /api/gallery/assets/:chatId— both load the chat withchats.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)
POSTa multipart upload with a.pngfile 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).data/gallery/and thechat_imagestable.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 achat_imagesrow is inserted — all for a chat that does not exist. By contrast,GET /api/gallery/assets/this-chat-does-not-existreturns 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:POST /:chatId/uploadlines 372-420 — validates onlyisValidChatId(chatId)(lines 373-375), thenensureDir(chatId)(387),await pipeline(...)(396), andstorage.create({ chatId, ... })(406). No chat-existence check anywhere in the handler.GET /asset/:chatId/*lines 243-244 andGET /assets/:chatIdlines 273-274 both doconst chat = await chats.getById(chatId); if (!chat) return reply.status(404).send({ error: "Chat not found" });.chatsstorage 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
isValidChatIdguard in the upload handler (after line 375, beforereq.file()):This mirrors lines 243-244 / 273-274 exactly and uses the
chatsstorage 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(), thepipelinewrite, andstorage.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 itsfolderId(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.
@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
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Fixed in PR #3064 on branch
codex/issue-sweep-3054-3063(commit3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.