[PR #2532] [MERGED] fix(import): cap .charx entry count and decompressed size to prevent zip-bomb OOM #5638

Closed
opened 2026-10-22 04:18:02 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2532
Author: @Romuromylus
Created: 6/9/2026
Status: ✅ Merged
Merged: 6/9/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/charx-zip-bomb-cap


📝 Commits (2)

  • 54dd4b7 fix(import): cap .charx entry count and decompressed size to prevent zip-bomb OOM
  • 62a3b19 Merge branch 'staging' into fix/charx-zip-bomb-cap

📊 Changes

1 file changed (+37 additions, -0 deletions)

View changed files

📝 packages/server/src/services/import/st-character.importer.ts (+37 -0)

📄 Description

Linked issue

Closes #2523

Why this change

.charx import (RisuAI Character Card V3 zip) decompressed entries with no bound on entry count or uncompressed size. adm-zip's getData() allocates the full uncompressed entry as a single Buffer, and the 256 MB multipart cap in app.ts bounds only the compressed upload. DEFLATE reaches ~1000:1 on repetitive data, so a few-MB .charx can expand to multiple GB and OOM the shared server process — a denial-of-service against every user on the instance.

What changed

  • Added assertCharXWithinLimits(zip) in st-character.importer.ts. It reads each entry's central-directory header.size (the uncompressed size, available without decompressing) and the entry count, rejecting before any getData() materializes a buffer:
    • max 512 entries (a .charx legitimately carries card.json plus many asset images),
    • max 64 MB per uncompressed entry,
    • max 256 MB total decompressed (matching the compressed upload cap).
  • Call it immediately after new AdmZip(buf) in both importCharX and inspectCharX. Because it validates up front, it transitively guards all three downstream getData() sites (readCharXCardJson, the asset fallback loop, resolveCharXAsset) without touching them.
  • Failures flow through the existing try/catch in both functions to { success: false, error } — no server crash, no new logging.

This mirrors the existing /marinara-package bounds in import.routes.ts (MAX_PACKAGE_ENTRIES + the header.size check before getData()).

Not a regression: the .charx paths are byte-identical on main; this is a long-standing shared-code hardening.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

pnpm check passes locally. I also added a local-only node:test (charx-bomb.test.ts, gitignored per repo convention) covering: a legitimate small .charx accepted (positive control), exactly 512 entries accepted (boundary, not off-by-one), and rejection of too-many-entries (513), an oversize entry (>64 MB uncompressed), and total-budget overflow (>256 MB). Local run: 5/5 pass.

Still to verify manually in a real browser against the deploy:

  • Import a normal exported .charx and confirm the character appears (happy path unaffected).
  • Import a benign .charx with 600+ trivial entries and confirm the UI shows a graceful import error and the server stays up (no restart).
  • Do not upload an actual decompression bomb against a shared deploy — the unit test is the canonical proof for the OOM path.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

🔄 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/2532 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/9/2026 **Status:** ✅ Merged **Merged:** 6/9/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/charx-zip-bomb-cap` --- ### 📝 Commits (2) - [`54dd4b7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/54dd4b7f603fb7a8f0fc2a46482348d353a69938) fix(import): cap .charx entry count and decompressed size to prevent zip-bomb OOM - [`62a3b19`](https://github.com/Pasta-Devs/Marinara-Engine/commit/62a3b19429f49eafd69baf7fd7804c07de1b93f1) Merge branch 'staging' into fix/charx-zip-bomb-cap ### 📊 Changes **1 file changed** (+37 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/import/st-character.importer.ts` (+37 -0) </details> ### 📄 Description ## Linked issue Closes #2523 ## Why this change `.charx` import (RisuAI Character Card V3 zip) decompressed entries with no bound on entry count or uncompressed size. adm-zip's `getData()` allocates the full uncompressed entry as a single Buffer, and the 256 MB multipart cap in `app.ts` bounds only the *compressed* upload. DEFLATE reaches ~1000:1 on repetitive data, so a few-MB `.charx` can expand to multiple GB and OOM the shared server process — a denial-of-service against every user on the instance. ## What changed - Added `assertCharXWithinLimits(zip)` in `st-character.importer.ts`. It reads each entry's central-directory `header.size` (the *uncompressed* size, available without decompressing) and the entry count, rejecting before any `getData()` materializes a buffer: - max 512 entries (a `.charx` legitimately carries `card.json` plus many asset images), - max 64 MB per uncompressed entry, - max 256 MB total decompressed (matching the compressed upload cap). - Call it immediately after `new AdmZip(buf)` in both `importCharX` and `inspectCharX`. Because it validates up front, it transitively guards all three downstream `getData()` sites (`readCharXCardJson`, the asset fallback loop, `resolveCharXAsset`) without touching them. - Failures flow through the existing try/catch in both functions to `{ success: false, error }` — no server crash, no new logging. This mirrors the existing `/marinara-package` bounds in `import.routes.ts` (`MAX_PACKAGE_ENTRIES` + the `header.size` check before `getData()`). Not a regression: the `.charx` paths are byte-identical on `main`; this is a long-standing shared-code hardening. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes `pnpm check` passes locally. I also added a local-only node:test (`charx-bomb.test.ts`, gitignored per repo convention) covering: a legitimate small `.charx` accepted (positive control), exactly 512 entries accepted (boundary, not off-by-one), and rejection of too-many-entries (513), an oversize entry (>64 MB uncompressed), and total-budget overflow (>256 MB). Local run: **5/5 pass**. Still to verify manually in a real browser against the deploy: - Import a normal exported `.charx` and confirm the character appears (happy path unaffected). - Import a benign `.charx` with 600+ trivial entries and confirm the UI shows a graceful import error and the server stays up (no restart). - Do **not** upload an actual decompression bomb against a shared deploy — the unit test is the canonical proof for the OOM path. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:18:02 +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#5638
No description provided.