[PR #5176] [MERGED] fix(gallery): serve mislabeled raster images by detected MIME type #6660

Closed
opened 2026-10-23 04:12:29 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5176
Author: @Gunterlie
Created: 8/17/2026
Status: ✅ Merged
Merged: 8/17/2026
Merged by: @Gunterlie

Base: staging ← Head: issue-392-marinara-agents


📝 Commits (3)

  • fc70614 fix(gallery): serve mislabeled raster images by detected MIME type
  • a2cfc6e docs(changelog): note gallery MIME fix
  • 89d7fad fix(media): detect mislabeled AVIF images

📊 Changes

4 files changed (+46 additions, -7 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/utils/media-file-security.ts (+4 -5)
📝 packages/server/src/utils/security.ts (+1 -2)
➕ scripts/regressions/gallery-mislabeled-raster.regression.ts (+40 -0)

📄 Description

Why

Since Marinara Engine v2.4.2, the Noodle feed renders generated post images as broken-image icons (reported in Pasta-Devs/Marinara-Agents#392). The main app (Slurp) is unaffected. The file exists on disk, the character_images row exists, and imageUrl points at the file — generation and bookkeeping succeed. The failure is purely on the serve side.

Root cause: v2.4.2 hardened the character-gallery serve route (GET /api/characters/:id/gallery/file/:filename), replacing reply.sendFile(...) with validateImageAssetFile() + sendValidatedMediaFile(). validateImageAssetBuffer rejected any file whose extension disagreed with its magic-byte format. The Noodle package copies generated post images into gallery/characters/<id>/ with a hardcoded .png name, but the qwen-image provider returns WebP/JPEG bytes — so the mislabeled files now 404. Under v2.4.1 reply.sendFile served them and the browser sniffed the content and rendered them. The two suspects in the issue (#4754 gallery sharding, #4763 Noodle packaging) are red herrings.

What

Serve any recognized raster image by its detected MIME type instead of requiring extension == content. Content-Type stays honest. Raster formats are inert, so this is safe; SVG validation stays strict (SVG is scriptable). This heals already-generated broken files and fixes the character-, persona-, and chat-gallery serve routes in one place, since all route through validateImageAssetBuffer.

  • packages/server/src/utils/media-file-security.ts — relax raster ext/content agreement; remove now-unused normalizedRasterExtension.
  • scripts/regressions/gallery-mislabeled-raster.regression.ts — new regression.

Follow-up

The mislabeling originates in the Noodle package. A companion issue is filed on Pasta-Devs/Marinara-Agents to write the correct extension from the response bytes at the source.

Validation

  • pnpm check
  • node ./scripts/run-regressions.mjs --filter scripts/regressions/gallery-mislabeled-raster.regression.ts
  • Manually verify in browser: a WebP file saved as data/gallery/characters/<id>/<name>.png with a matching character_images row serves HTTP 200 with Content-Type: image/webp at /api/characters/<id>/gallery/file/<name>.png (previously 404).
  • Manually verify a correctly-labeled PNG still serves with Content-Type: image/png.

Note: no linked issue exists in this repo; the report lives at Pasta-Devs/Marinara-Agents#392. AI-generated PR — validation boxes are a to-do list for the human reviewer and are intentionally left unchecked.

Summary by CodeRabbit

  • Bug Fixes
    • Gallery routes now detect raster image formats from file contents, allowing valid images to render even when their file extension is incorrect.
    • Invalid or non-image content continues to be rejected.
  • Documentation
    • Added a changelog entry describing improved handling of mislabeled raster images.

🔄 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/5176 **Author:** [@Gunterlie](https://github.com/Gunterlie) **Created:** 8/17/2026 **Status:** ✅ Merged **Merged:** 8/17/2026 **Merged by:** [@Gunterlie](https://github.com/Gunterlie) **Base:** `staging` ← **Head:** `issue-392-marinara-agents` --- ### 📝 Commits (3) - [`fc70614`](https://github.com/Pasta-Devs/Marinara-Engine/commit/fc706142d585025ff21cdbbc534defb25b092f05) fix(gallery): serve mislabeled raster images by detected MIME type - [`a2cfc6e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a2cfc6e89a9379008d2c5bc8d8262ac4c5c665df) docs(changelog): note gallery MIME fix - [`89d7fad`](https://github.com/Pasta-Devs/Marinara-Engine/commit/89d7fad26198f6c93551f4f921dec401a9943e13) fix(media): detect mislabeled AVIF images ### 📊 Changes **4 files changed** (+46 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/utils/media-file-security.ts` (+4 -5) 📝 `packages/server/src/utils/security.ts` (+1 -2) ➕ `scripts/regressions/gallery-mislabeled-raster.regression.ts` (+40 -0) </details> ### 📄 Description ## Why Since Marinara Engine **v2.4.2**, the Noodle feed renders generated post images as broken-image icons (reported in Pasta-Devs/Marinara-Agents#392). The main app (Slurp) is unaffected. The file exists on disk, the `character_images` row exists, and `imageUrl` points at the file — generation and bookkeeping succeed. The failure is purely on the serve side. **Root cause:** v2.4.2 hardened the character-gallery serve route (`GET /api/characters/:id/gallery/file/:filename`), replacing `reply.sendFile(...)` with `validateImageAssetFile()` + `sendValidatedMediaFile()`. `validateImageAssetBuffer` rejected any file whose extension disagreed with its magic-byte format. The Noodle package copies generated post images into `gallery/characters/<id>/` with a hardcoded `.png` name, but the `qwen-image` provider returns WebP/JPEG bytes — so the mislabeled files now 404. Under v2.4.1 `reply.sendFile` served them and the browser sniffed the content and rendered them. The two suspects in the issue (#4754 gallery sharding, #4763 Noodle packaging) are red herrings. ## What Serve any recognized **raster** image by its **detected** MIME type instead of requiring `extension == content`. Content-Type stays honest. Raster formats are inert, so this is safe; SVG validation stays strict (SVG is scriptable). This heals already-generated broken files and fixes the character-, persona-, and chat-gallery serve routes in one place, since all route through `validateImageAssetBuffer`. - `packages/server/src/utils/media-file-security.ts` — relax raster ext/content agreement; remove now-unused `normalizedRasterExtension`. - `scripts/regressions/gallery-mislabeled-raster.regression.ts` — new regression. ## Follow-up The mislabeling originates in the Noodle package. A companion issue is filed on `Pasta-Devs/Marinara-Agents` to write the correct extension from the response bytes at the source. ## Validation - [ ] `pnpm check` - [ ] `node ./scripts/run-regressions.mjs --filter scripts/regressions/gallery-mislabeled-raster.regression.ts` - [ ] Manually verify in browser: a WebP file saved as `data/gallery/characters/<id>/<name>.png` with a matching `character_images` row serves HTTP 200 with `Content-Type: image/webp` at `/api/characters/<id>/gallery/file/<name>.png` (previously 404). - [ ] Manually verify a correctly-labeled PNG still serves with `Content-Type: image/png`. > Note: no linked issue exists in this repo; the report lives at Pasta-Devs/Marinara-Agents#392. AI-generated PR — validation boxes are a to-do list for the human reviewer and are intentionally left unchecked. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Gallery routes now detect raster image formats from file contents, allowing valid images to render even when their file extension is incorrect. * Invalid or non-image content continues to be rejected. * **Documentation** * Added a changelog entry describing improved handling of mislabeled raster images. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:12:29 +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#6660
No description provided.