[PR #7532] [MERGED] fix(import): read outside archives strictly while keeping existing size limits #7781

Closed
opened 2026-10-24 04:07:27 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7532
Author: @SpicyMarinara
Created: 10/13/2026
Status: ✅ Merged
Merged: 10/13/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/strict-archive-reader


📝 Commits (5)

  • ac76e23 fix(import): read outside archives strictly while keeping existing size limits
  • 5e760d7 Merge branch 'staging' into fix/strict-archive-reader
  • 166d62f test(import): cover the strict archive reader and its entry points
  • 6f3f82e Merge remote-tracking branch 'origin/fix/strict-archive-reader' into fix/strict-archive-reader
  • 4ee06ce Merge branch 'staging' into fix/strict-archive-reader

📊 Changes

8 files changed (+1154 additions, -35 deletions)

View changed files

➕ packages/server/src/lib/zip-reader.ts (+305 -0)
📝 packages/server/src/routes/import.routes.ts (+8 -7)
📝 packages/server/src/services/agents/custom-agent-repositories.service.ts (+8 -8)
📝 packages/server/src/services/capability-packages/package-manager.service.ts (+11 -10)
📝 packages/server/src/services/import/st-character.importer.ts (+11 -8)
📝 scripts/regressions/capability-package-lifecycle.regression.ts (+3 -1)
📝 scripts/regressions/open-issues.regression.ts (+1 -1)
➕ scripts/regressions/zip-reader.regression.ts (+807 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7531

Why this change

  • Marinara opens ZIP archives it did not create in four places: Agent package installs, custom Agent repositories, .charx cards and .marinara packages. These now go through one strict reader, so every existing size limit applies to the bytes that are actually read, and an archive whose own records disagree with each other is refused instead of being interpreted.

What changed

  • A .charx whose card.json or icon unpacks to more than 256 MB can no longer be imported. This is the only new limit. It is the import upload limit, taken from the same constant (IMPORT_BODY_LIMIT_BYTES), so raising the upload limit raises it too.
  • Archives whose records contradict each other are now refused with a plain message. This covers a missing or duplicated end record, bytes after the end record, a file list that does not fit or fill its stated size, a local header that is not where the file list says or names a different file or compression method, overlapping entries, stored entries with two different sizes, a data descriptor that disagrees with the file list, and split archives.
  • No honest archive that imported before is refused. ZIP64 (end records and extra fields) and data descriptors are accepted. Stored and deflate entries read exactly as before. Encrypted entries and other compression methods are refused when that entry is read, as they were before. The compatibility proof is under Validation.
  • Every existing size limit is unchanged: import upload 256 MB; .marinara upload 1 GB and 256 MB per entry; Agent artifact 100 MB, expanded package 250 MB, manifest 1 MB and the per-file declarations; custom repository archive 10 MB, expanded 20 MB, agents.json 1 MB, ruleset files at their existing limit.
  • Every read now names its limit. An entry declared larger than that limit is refused before any of it is read. Decompression stops at the declared size, and the result must match the declared length and CRC-32.
  • Existing user-facing messages are kept: "Could not read .marinara package", "data.json is not valid JSON", ".marinara package entry is too large", "Package file size mismatch for …", "agents.json is too large", the ruleset "over the … limit" row, and the Agent package messages. One small difference: a .marinara file that cannot be read as an archive at all now always gets the existing 400 "Could not read .marinara package". Before, some of those failures came back as a server error.
  • New packages/server/src/lib/zip-reader.ts. The package manager, custom Agent repositories, the .charx importer and the .marinara route use it instead of adm-zip. Full-backup restore, the NovelAI reader and the sidecar download are unchanged and still use adm-zip.
  • New regression scripts/regressions/zip-reader.regression.ts. Two existing regressions changed only to call the new reader: the case-collision fixture in capability-package-lifecycle now goes through it, and the open-issues .marinara guard now names the bounded read. Their assertions and messages are unchanged.

Claim boundary (risky work: import / storage hardening)

  • Core claim: honest archives read byte-for-byte as before; self-contradictory archives are refused; no existing size limit changed; the only new limit (the .charx card and icon) equals the upload limit.
  • Entry points touched: Agent package install (installCatalogPackage, validatePackageArchiveEntries), custom Agent repository preview/sync (parseCustomAgentRepositoryContents), .charx import and inspect (importCharX, inspectCharX), /api/import/marinara-package.
  • Positive rows: the CI corpus and local corpus below; .charx and .marinara imports through the real routes, with avatar or icon bytes checked.
  • Negative controls: one regression case per refusal; a limit refusal shown to happen before the entry is read; CRC and length mismatches.
  • Not covered end to end: an Agent package install through the network download. It is covered by a source guard plus validatePackageArchiveEntries over the new reader, and by all 337 committed Agent artifacts reading identically. See the manual list below.

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

Agent-run checks (for the human reviewer to confirm, not ticked):

  • pnpm check: exit 0. The one client lint warning, in HomeProfessorMariChat.tsx, was already there and is unrelated.
  • node ./scripts/run-regressions.mjs --filter …: zip-reader 1/1, import 12/12, package 6/6, capability 19/19, agent-activation 2/2, spatial-context 1/1, open-issues 1/1. Also game-ruleset 53/53, agent 19/19, decision-conditionals, archive, bulk-export, native-export-memory, agent-import 1/1 each, profile-import 3/3 and custom-tool 2/2. --filter charx and --filter custom-agent match no regression file names; that coverage is in zip-reader, game-ruleset-repository and open-issues.
  • Local CodeRabbit (coderabbit review --agent --committed): 0 findings across all 8 changed files.

Compatibility corpus in CI (zip-reader.regression.ts; each archive must read byte-identically to the previous reader):

  • Node-built: classic, comment, signed, unsigned and ZIP64 data descriptors, ZIP64 end records and extra fields, and the descriptor flag set with no descriptor.
  • Marinara's in-memory and streamed export writers, and a .marinara package built from a real native export.
  • git archive, streamed and stored.
  • Python zipfile: seekable, force_zip64, unseekable streams with and without ZIP64, and 65,536 entries (ZIP64 end record).
  • Info-ZIP default, -fz, and piped from stdin, when installed. JSZip when installed; it is not a dependency, so it was checked locally once.

Local proof (not in CI):

Corpus Archives Entries Result
Every committed Marinara-Agents artifact (artifacts/*.zip) 337 1,512 all identical
GitHub downloads (custom-repository format): Marinara-Agents HEAD and staging, Marinara-Engine staging 3 11,902 (4.58 GB unpacked) all identical
git archive of this branch: default, streamed (core.bigFileThreshold), stored 3 11,355 all identical
Info-ZIP 3.0: default, -0, -9, -fz, -y, with extras, comment, pipe/stdin variants 10 all identical
macOS ditto -c -k: default, --sequesterRsrc --keepParent, level 0, --norsrc 4 all identical
Python 3.11 zipfile: deflated, stored, force_zip64, unseekable, unseekable + ZIP64, 70,000 entries 6 all identical
JSZip 3.10 ×3, fflate 0.8 ×4, archiver 7 ×3 (incl. forceZip64), yazl 3 ×3 (incl. forceZip64Format), Marinara writers ×2 15 all identical
Tool corpus total 38 91,110 all identical
Marinara's own ZIP exports on a scratch data dir: characters bulk (native, compatible), personas bulk, lorebooks bulk (native, compatible), presets bulk, chats bulk, sprites, compatible profile, full backup (217 entries) 10 all identical
.marinara packages built from Marinara's native character and persona exports with each of 17 writers, imported via /api/import/marinara-package 34 all imported, avatar bytes identical, character gallery (3) restored
.charx cards built from Marinara's compatible export with each of 17 writers, inspected and imported via /api/import/st-character 17 all imported, icon bytes identical

7-Zip was not installed, so it is not in the corpus. Marinara does not write .charx or .marinara ZIPs itself: its native export is .marinara.json, and the .marinara ZIP is a legacy format. Those rows were therefore built from Marinara's own exports.

Manual verification notes

Needs a human:

  • Manually import a large real-world .charx (for example one exported from RisuAI) through Import Character, and through the multi-file inspect flow; check the card and icon.
  • Manually install an Agent package from the official catalog (Agents browser), and update one, then confirm it activates.
  • Manually add or sync a custom Agent repository (a public GitHub repository with agents.json and/or rulesets/*.json) and confirm the preview and apply.
  • Manually import a .marinara package (a ZIP with data.json and an avatar) for a character and for a persona.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

No docs are touched; there are no user-visible settings or limits to document beyond the .charx note above.

UI evidence (if applicable)

Not applicable: server-only change.

Summary by CodeRabbit

  • Bug Fixes
    • ZIP-based imports now validate archive structure and verify extracted file sizes and checksums, helping prevent incomplete or inconsistent files from being imported.
    • Imports reject malformed, encrypted, unsupported, or oversized archive contents instead of processing them.
  • Compatibility
    • ZIP archives using stored or deflated compression, data descriptors, and ZIP64 records are supported across package, repository, character, and Marinara imports.

🔄 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/7532 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/13/2026 **Status:** ✅ Merged **Merged:** 10/13/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/strict-archive-reader` --- ### 📝 Commits (5) - [`ac76e23`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ac76e23b50569402159fed7179d6a4b049412011) fix(import): read outside archives strictly while keeping existing size limits - [`5e760d7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5e760d7904c3b020a11804eee46318cadfda8617) Merge branch 'staging' into fix/strict-archive-reader - [`166d62f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/166d62f920279e53c6ce709208a5ee677e2bd1a4) test(import): cover the strict archive reader and its entry points - [`6f3f82e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6f3f82e236fd6be902f7400635bb036d23d85891) Merge remote-tracking branch 'origin/fix/strict-archive-reader' into fix/strict-archive-reader - [`4ee06ce`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4ee06cee2ccf23e2be37ad183031c913e2322ed8) Merge branch 'staging' into fix/strict-archive-reader ### 📊 Changes **8 files changed** (+1154 additions, -35 deletions) <details> <summary>View changed files</summary> ➕ `packages/server/src/lib/zip-reader.ts` (+305 -0) 📝 `packages/server/src/routes/import.routes.ts` (+8 -7) 📝 `packages/server/src/services/agents/custom-agent-repositories.service.ts` (+8 -8) 📝 `packages/server/src/services/capability-packages/package-manager.service.ts` (+11 -10) 📝 `packages/server/src/services/import/st-character.importer.ts` (+11 -8) 📝 `scripts/regressions/capability-package-lifecycle.regression.ts` (+3 -1) 📝 `scripts/regressions/open-issues.regression.ts` (+1 -1) ➕ `scripts/regressions/zip-reader.regression.ts` (+807 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #7531 ## Why this change - Marinara opens ZIP archives it did not create in four places: Agent package installs, custom Agent repositories, `.charx` cards and `.marinara` packages. These now go through one strict reader, so every existing size limit applies to the bytes that are actually read, and an archive whose own records disagree with each other is refused instead of being interpreted. ## What changed - **A `.charx` whose `card.json` or icon unpacks to more than 256 MB can no longer be imported.** This is the only new limit. It is the import upload limit, taken from the same constant (`IMPORT_BODY_LIMIT_BYTES`), so raising the upload limit raises it too. - **Archives whose records contradict each other are now refused** with a plain message. This covers a missing or duplicated end record, bytes after the end record, a file list that does not fit or fill its stated size, a local header that is not where the file list says or names a different file or compression method, overlapping entries, stored entries with two different sizes, a data descriptor that disagrees with the file list, and split archives. - **No honest archive that imported before is refused.** ZIP64 (end records and extra fields) and data descriptors are accepted. Stored and deflate entries read exactly as before. Encrypted entries and other compression methods are refused when that entry is read, as they were before. The compatibility proof is under Validation. - **Every existing size limit is unchanged:** import upload 256 MB; `.marinara` upload 1 GB and 256 MB per entry; Agent artifact 100 MB, expanded package 250 MB, manifest 1 MB and the per-file declarations; custom repository archive 10 MB, expanded 20 MB, `agents.json` 1 MB, ruleset files at their existing limit. - Every read now names its limit. An entry declared larger than that limit is refused before any of it is read. Decompression stops at the declared size, and the result must match the declared length and CRC-32. - Existing user-facing messages are kept: "Could not read .marinara package", "data.json is not valid JSON", ".marinara package entry is too large", "Package file size mismatch for …", "agents.json is too large", the ruleset "over the … limit" row, and the Agent package messages. One small difference: a `.marinara` file that cannot be read as an archive at all now always gets the existing 400 "Could not read .marinara package". Before, some of those failures came back as a server error. - New `packages/server/src/lib/zip-reader.ts`. The package manager, custom Agent repositories, the `.charx` importer and the `.marinara` route use it instead of `adm-zip`. Full-backup restore, the NovelAI reader and the sidecar download are unchanged and still use `adm-zip`. - New regression `scripts/regressions/zip-reader.regression.ts`. Two existing regressions changed only to call the new reader: the case-collision fixture in `capability-package-lifecycle` now goes through it, and the `open-issues` `.marinara` guard now names the bounded read. Their assertions and messages are unchanged. ### Claim boundary (risky work: import / storage hardening) - **Core claim:** honest archives read byte-for-byte as before; self-contradictory archives are refused; no existing size limit changed; the only new limit (the `.charx` card and icon) equals the upload limit. - **Entry points touched:** Agent package install (`installCatalogPackage`, `validatePackageArchiveEntries`), custom Agent repository preview/sync (`parseCustomAgentRepositoryContents`), `.charx` import and inspect (`importCharX`, `inspectCharX`), `/api/import/marinara-package`. - **Positive rows:** the CI corpus and local corpus below; `.charx` and `.marinara` imports through the real routes, with avatar or icon bytes checked. - **Negative controls:** one regression case per refusal; a limit refusal shown to happen before the entry is read; CRC and length mismatches. - **Not covered end to end:** an Agent package install through the network download. It is covered by a source guard plus `validatePackageArchiveEntries` over the new reader, and by all 337 committed Agent artifacts reading identically. See the manual list below. ## 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` Agent-run checks (for the human reviewer to confirm, not ticked): - `pnpm check`: exit 0. The one client lint warning, in `HomeProfessorMariChat.tsx`, was already there and is unrelated. - `node ./scripts/run-regressions.mjs --filter …`: `zip-reader` 1/1, `import` 12/12, `package` 6/6, `capability` 19/19, `agent-activation` 2/2, `spatial-context` 1/1, `open-issues` 1/1. Also `game-ruleset` 53/53, `agent` 19/19, `decision-conditionals`, `archive`, `bulk-export`, `native-export-memory`, `agent-import` 1/1 each, `profile-import` 3/3 and `custom-tool` 2/2. `--filter charx` and `--filter custom-agent` match no regression file names; that coverage is in `zip-reader`, `game-ruleset-repository` and `open-issues`. - Local CodeRabbit (`coderabbit review --agent --committed`): 0 findings across all 8 changed files. **Compatibility corpus in CI** (`zip-reader.regression.ts`; each archive must read byte-identically to the previous reader): - Node-built: classic, comment, signed, unsigned and ZIP64 data descriptors, ZIP64 end records and extra fields, and the descriptor flag set with no descriptor. - Marinara's in-memory and streamed export writers, and a `.marinara` package built from a real native export. - `git archive`, streamed and stored. - Python `zipfile`: seekable, `force_zip64`, unseekable streams with and without ZIP64, and 65,536 entries (ZIP64 end record). - Info-ZIP default, `-fz`, and piped from stdin, when installed. JSZip when installed; it is not a dependency, so it was checked locally once. **Local proof (not in CI):** | Corpus | Archives | Entries | Result | | --- | ---: | ---: | --- | | Every committed Marinara-Agents artifact (`artifacts/*.zip`) | 337 | 1,512 | all identical | | GitHub downloads (custom-repository format): Marinara-Agents `HEAD` and `staging`, Marinara-Engine `staging` | 3 | 11,902 (4.58 GB unpacked) | all identical | | `git archive` of this branch: default, streamed (`core.bigFileThreshold`), stored | 3 | 11,355 | all identical | | Info-ZIP 3.0: default, `-0`, `-9`, `-fz`, `-y`, with extras, comment, pipe/stdin variants | 10 | | all identical | | macOS `ditto -c -k`: default, `--sequesterRsrc --keepParent`, level 0, `--norsrc` | 4 | | all identical | | Python 3.11 `zipfile`: deflated, stored, `force_zip64`, unseekable, unseekable + ZIP64, 70,000 entries | 6 | | all identical | | JSZip 3.10 ×3, fflate 0.8 ×4, archiver 7 ×3 (incl. `forceZip64`), yazl 3 ×3 (incl. `forceZip64Format`), Marinara writers ×2 | 15 | | all identical | | Tool corpus total | 38 | 91,110 | all identical | | Marinara's own ZIP exports on a scratch data dir: characters bulk (native, compatible), personas bulk, lorebooks bulk (native, compatible), presets bulk, chats bulk, sprites, compatible profile, full backup (217 entries) | 10 | | all identical | | `.marinara` packages built from Marinara's native character and persona exports with each of 17 writers, imported via `/api/import/marinara-package` | 34 | | all imported, avatar bytes identical, character gallery (3) restored | | `.charx` cards built from Marinara's compatible export with each of 17 writers, inspected and imported via `/api/import/st-character` | 17 | | all imported, icon bytes identical | 7-Zip was not installed, so it is not in the corpus. Marinara does not write `.charx` or `.marinara` ZIPs itself: its native export is `.marinara.json`, and the `.marinara` ZIP is a legacy format. Those rows were therefore built from Marinara's own exports. ### Manual verification notes Needs a human: - Manually import a large real-world `.charx` (for example one exported from RisuAI) through Import Character, and through the multi-file inspect flow; check the card and icon. - Manually install an Agent package from the official catalog (Agents browser), and update one, then confirm it activates. - Manually add or sync a custom Agent repository (a public GitHub repository with `agents.json` and/or `rulesets/*.json`) and confirm the preview and apply. - Manually import a `.marinara` package (a ZIP with `data.json` and an avatar) for a character and for a persona. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) No docs are touched; there are no user-visible settings or limits to document beyond the `.charx` note above. ## UI evidence (if applicable) Not applicable: server-only change. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * ZIP-based imports now validate archive structure and verify extracted file sizes and checksums, helping prevent incomplete or inconsistent files from being imported. * Imports reject malformed, encrypted, unsupported, or oversized archive contents instead of processing them. * **Compatibility** * ZIP archives using stored or deflated compression, data descriptors, and ZIP64 records are supported across package, repository, character, and Marinara imports. <!-- 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-24 04:07:27 +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#7781
No description provided.