[GH-ISSUE #2705] [Issue]: Import hardening — avatar magic-byte/CharX validation, unbounded avatar proxies, legacy persona profile field loss #1350

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

A few hardening gaps in the import paths: avatars are written to disk trusting an attacker-controlled data-URL MIME for content and file extension (no magic-byte validation), CharX zip assets are decoded into avatars with no image-content validation, the DataCat/Janny avatar proxies have no response-size cap or content-type check (large-image/decompression memory bomb), and a legacy JSON profile import drops persona tags/savedStatusOptions/avatarCrop.

Expected behavior

Imported images should be validated as actual images (by content, not a self-declared MIME), remote image proxies should bound response size and check content-type, and profile import should preserve persona fields.

Actual behavior

  • Avatar magic-byte: character-card import writes the avatar to disk using the data-URL's declared MIME for both content and the file extension (ext = avatarDataUrl.match(/^data:image\/([\w+]+);/)), with no magic-byte check. A card can declare data:image/png;... while carrying arbitrary bytes, written verbatim under that extension.
  • CharX asset: resolveCharXAsset / the fallback paths decode zip-entry bytes into a data:image/... URL with an extension/MIME taken from the asset metadata, with no image-content validation.
  • Avatar proxy memory bomb: the DataCat (and Janny) avatar proxy fetches a remote image with no response-size cap or content-type check, so a hostile/huge response can balloon memory.
  • Persona profile import: a legacy JSON profile import of personas drops tags, savedStatusOptions, and avatarCrop.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • Avatar magic-byte: packages/server/src/services/import/st-character.importer.ts:220-232 (ext from MIME :222, base64 write :229).
  • CharX asset: resolveCharXAsset (st-character.importer.ts:585-610) + fallback (:424-438).
  • Avatar proxy: the DataCat/Janny avatar proxy routes (bot-browser-datacat.routes.ts / bot-browser-janny.routes.ts) — fetch with no size cap / content-type check.
  • Persona profile import: the legacy JSON profile import path (drops tags/savedStatusOptions/avatarCrop).

Steps to reproduce

  • Import a card whose _avatarDataUrl declares image/png but carries non-image bytes → written to disk as a .png unchecked.
  • Point a Bot Browser avatar proxy at a very large / mismatched-content-type URL → unbounded fetch into memory.
  • Import a legacy JSON profile with personas that have tags/status options/avatar crop → those persona fields are missing after import.

Suggested fix

  • Validate imported images by magic bytes (e.g. via sharp metadata) and derive the extension from the detected format, not the declared MIME; apply the same to CharX assets.
  • Add a response-size cap (and content-type check) to the avatar proxies (the codebase already caps LLM fetches at 50 MB — reuse that pattern).
  • Map persona tags/savedStatusOptions/avatarCrop on legacy profile import.

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Found via a systematic multi-agent audit of import/export round-trips. Low-to-medium severity hardening + a minor persona field loss. Part of a set of import/export fidelity issues filed together.

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 @kolacheee on GitHub (Jun 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2705 Originally assigned to: @SpicyMarinara on GitHub. ## Summary A few hardening gaps in the import paths: avatars are written to disk trusting an attacker-controlled data-URL MIME for content and file extension (no magic-byte validation), CharX zip assets are decoded into avatars with no image-content validation, the DataCat/Janny avatar proxies have no response-size cap or content-type check (large-image/decompression memory bomb), and a legacy JSON profile import drops persona `tags`/`savedStatusOptions`/`avatarCrop`. ## Expected behavior Imported images should be validated as actual images (by content, not a self-declared MIME), remote image proxies should bound response size and check content-type, and profile import should preserve persona fields. ## Actual behavior - **Avatar magic-byte:** character-card import writes the avatar to disk using the data-URL's declared MIME for both content and the file extension (`ext = avatarDataUrl.match(/^data:image\/([\w+]+);/)`), with no magic-byte check. A card can declare `data:image/png;...` while carrying arbitrary bytes, written verbatim under that extension. - **CharX asset:** `resolveCharXAsset` / the fallback paths decode zip-entry bytes into a `data:image/...` URL with an extension/MIME taken from the asset metadata, with no image-content validation. - **Avatar proxy memory bomb:** the DataCat (and Janny) avatar proxy fetches a remote image with no response-size cap or content-type check, so a hostile/huge response can balloon memory. - **Persona profile import:** a legacy JSON profile import of personas drops `tags`, `savedStatusOptions`, and `avatarCrop`. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - Avatar magic-byte: `packages/server/src/services/import/st-character.importer.ts:220-232` (ext from MIME `:222`, base64 write `:229`). - CharX asset: `resolveCharXAsset` (`st-character.importer.ts:585-610`) + fallback (`:424-438`). - Avatar proxy: the DataCat/Janny avatar proxy routes (`bot-browser-datacat.routes.ts` / `bot-browser-janny.routes.ts`) — fetch with no size cap / content-type check. - Persona profile import: the legacy JSON profile import path (drops `tags`/`savedStatusOptions`/`avatarCrop`). ## Steps to reproduce - Import a card whose `_avatarDataUrl` declares `image/png` but carries non-image bytes → written to disk as a `.png` unchecked. - Point a Bot Browser avatar proxy at a very large / mismatched-content-type URL → unbounded fetch into memory. - Import a legacy JSON profile with personas that have tags/status options/avatar crop → those persona fields are missing after import. ## Suggested fix - Validate imported images by magic bytes (e.g. via `sharp` metadata) and derive the extension from the detected format, not the declared MIME; apply the same to CharX assets. - Add a response-size cap (and content-type check) to the avatar proxies (the codebase already caps LLM fetches at 50 MB — reuse that pattern). - Map persona `tags`/`savedStatusOptions`/`avatarCrop` on legacy profile import. ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Found via a systematic multi-agent audit of import/export round-trips. Low-to-medium severity hardening + a minor persona field loss. Part of a set of import/export fidelity issues filed together. ## 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.
Author
Owner

@kolacheee commented on GitHub (Jun 21, 2026):

Veracity self-review — correction / clarification
Automated post-filing review of this issue against the current working tree. The reported bug remains valid; the notes below refine its scope, accuracy, or suggested fix for whoever implements it.

Two scope clarifications and one fix refinement for actionability:

  1. Avatar-proxy scope — the same unbounded-fetch defect also affects two proxies not listed in this issue: Chartavern (packages/server/src/routes/bot-browser-chartavern.routes.ts:257-281, raw arrayBuffer() at :272/:275) and Chub (packages/server/src/routes/bot-browser.routes.ts:159-183, raw arrayBuffer() at :175/:178). Both use plain fetch with only a 15s timeout — no size cap, no content-type check. Any fix should cover all four (DataCat, Janny, Chartavern, Chub). For reference, Pygmalion (bot-browser-pygmalion.routes.ts:254) and Wyvern (bot-browser-wyvern.routes.ts:121) are ALREADY hardened via safeFetch({ maxResponseBytes: 10*1024*1024, allowedContentTypes: ["image/"] }) + isAllowedImageBuffer — replicate that exact pattern (note the established avatar-proxy cap is 10 MB, not the 50 MB LLM cap mentioned in the Suggested fix).

  2. Persona field-loss location — the offending path is packages/server/src/routes/backup.routes.ts:1971-1994 (the legacy profile-import fallback createPersona call; it runs in the else branch when isProfileStorageSnapshot(data.fileStorage) is false at :1887, not the modern snapshot path). Add tags, savedStatusOptions, and avatarCrop to that extra object — createPersona already accepts them (packages/server/src/services/storage/characters.storage.ts:374, fields at :392-394). Do NOT "fix" the other two persona-import paths: ImportPersonaModal.tsx:115-117 and marinara.importer.ts:370-378 already map all three correctly.

  3. Image-validation fix — prefer the existing synchronous isAllowedImageBuffer() helper in packages/server/src/utils/security.ts:82 (already used across ~10 image-write paths) over a new sharp call: decode the base64, reject when isAllowedImageBuffer(buf) returns null, and derive the file extension from its .ext. Apply the same helper to the CharX asset paths (resolveCharXAsset :607-609 and the fallback :432-434). The st-character importer does not currently import this helper at all.

<!-- gh-comment-id:4761102467 --> @kolacheee commented on GitHub (Jun 21, 2026): > **Veracity self-review — correction / clarification** > _Automated post-filing review of this issue against the current working tree. The reported bug remains valid; the notes below refine its scope, accuracy, or suggested fix for whoever implements it._ Two scope clarifications and one fix refinement for actionability: 1) Avatar-proxy scope — the same unbounded-fetch defect also affects two proxies not listed in this issue: Chartavern (`packages/server/src/routes/bot-browser-chartavern.routes.ts:257-281`, raw `arrayBuffer()` at :272/:275) and Chub (`packages/server/src/routes/bot-browser.routes.ts:159-183`, raw `arrayBuffer()` at :175/:178). Both use plain `fetch` with only a 15s timeout — no size cap, no content-type check. Any fix should cover all four (DataCat, Janny, Chartavern, Chub). For reference, Pygmalion (`bot-browser-pygmalion.routes.ts:254`) and Wyvern (`bot-browser-wyvern.routes.ts:121`) are ALREADY hardened via `safeFetch({ maxResponseBytes: 10*1024*1024, allowedContentTypes: ["image/"] })` + `isAllowedImageBuffer` — replicate that exact pattern (note the established avatar-proxy cap is 10 MB, not the 50 MB LLM cap mentioned in the Suggested fix). 2) Persona field-loss location — the offending path is `packages/server/src/routes/backup.routes.ts:1971-1994` (the legacy profile-import fallback `createPersona` call; it runs in the `else` branch when `isProfileStorageSnapshot(data.fileStorage)` is false at :1887, not the modern snapshot path). Add `tags`, `savedStatusOptions`, and `avatarCrop` to that `extra` object — `createPersona` already accepts them (`packages/server/src/services/storage/characters.storage.ts:374`, fields at :392-394). Do NOT "fix" the other two persona-import paths: `ImportPersonaModal.tsx:115-117` and `marinara.importer.ts:370-378` already map all three correctly. 3) Image-validation fix — prefer the existing synchronous `isAllowedImageBuffer()` helper in `packages/server/src/utils/security.ts:82` (already used across ~10 image-write paths) over a new `sharp` call: decode the base64, reject when `isAllowedImageBuffer(buf)` returns null, and derive the file extension from its `.ext`. Apply the same helper to the CharX asset paths (`resolveCharXAsset` :607-609 and the fallback :432-434). The st-character importer does not currently import this helper at all.
Author
Owner

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

Fixed on the issue sweep branch. Character-card avatar writes now validate decoded image bytes with isAllowedImageBuffer and derive the stored extension from the detected image type; CharX asset and fallback avatar paths also validate zip bytes before building data URLs. Chub, CharacterTavern, DataCat, and Janny avatar proxies now use safeFetch with a 10 MB cap plus content-type/magic-byte validation before returning images. The legacy backup profile persona fallback now preserves tags, savedStatusOptions, and avatarCrop. Pygmalion/Wyvern and the newer persona import paths were already hardened/mapped, so I left those unchanged. Final pnpm validation is deferred until the full sweep is complete.

<!-- gh-comment-id:4761589468 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Fixed on the issue sweep branch. Character-card avatar writes now validate decoded image bytes with isAllowedImageBuffer and derive the stored extension from the detected image type; CharX asset and fallback avatar paths also validate zip bytes before building data URLs. Chub, CharacterTavern, DataCat, and Janny avatar proxies now use safeFetch with a 10 MB cap plus content-type/magic-byte validation before returning images. The legacy backup profile persona fallback now preserves tags, savedStatusOptions, and avatarCrop. Pygmalion/Wyvern and the newer persona import paths were already hardened/mapped, so I left those unchanged. Final pnpm validation is deferred until the full sweep is complete.
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#1350
No description provided.