[PR #7538] [MERGED] fix(import): accept padded archives and check entry limits before the file list #7782

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

📋 Pull Request Information

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

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


📝 Commits (2)

  • b136334 fix(import): accept padded archives and check entry limits before the file list
  • d682c6b fix(import): refuse an archive with an exact and a padded end record

📊 Changes

5 files changed (+354 additions, -142 deletions)

View changed files

📝 packages/server/src/lib/zip-reader.ts (+159 -120)
📝 packages/server/src/routes/import.routes.ts (+10 -10)
📝 packages/server/src/services/agents/custom-agent-repositories.service.ts (+6 -2)
📝 packages/server/src/services/capability-packages/package-manager.service.ts (+9 -3)
📝 scripts/regressions/zip-reader.regression.ts (+170 -7)

📄 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

Refs #7531 (follow-up to #7532, which is merged)

Why this change

  • The strict archive reader from #7532 refused some honest archives that imported before. libarchive, the tar on macOS and on Windows 10/11, pads a ZIP it writes to a pipe (tar --format zip -cf - … > card.charx) with zero bytes up to a whole block. Some transfers also leave a line break after the file. Those .charx cards, custom Agent repository archives, Agent packages and .marinara packages were refused with "The file is not a readable ZIP archive". adm-zip, Python zipfile and Info-ZIP unzip all read them.
  • Callers that already have an entry limit (.marinara packages: 8, Agent packages and custom repositories: 8,192) checked it only after the reader had built every entry. The reader now checks that limit against the end record before it reads the file list, and it keeps a small index instead of a full entry object per file. As a result, a long file list takes less memory than the archive itself.

What changed

  • Padding after the archive is accepted. An end record still has to close the file, but zero bytes and line breaks after its comment are now allowed. Other bytes after the end record are still refused. Two end records that could each close the file are still refused, and that now includes one record that ends exactly at the end of the file alongside another that is followed only by padding.
  • Entry limits are checked before the file list is read. openZipArchive(buffer, budget?) takes the caller's existing limit and message. The .marinara route passes 8 and keeps ".marinara package has too many entries". Agent package install and custom repositories pass PACKAGE_ARCHIVE_BUDGET (8,192, "Package contains too many files"). validatePackageArchiveEntries keeps its own check. .charx has no entry limit and none is added.
  • The reader keeps an index of names and record positions instead of building every entry up front. Entries are built when getEntry or getEntries asks for them. The overlap check now runs on two sorted Float64Arrays instead of an array of objects. Every structural check is the same and still runs when the archive is opened.
  • No size limit or user-facing message changed. The 256 MB import upload limit, the .marinara 1 GB / 256 MB-per-entry limits, the Agent artifact limits (100 MB artifact, 250 MB expanded, 1 MB manifest, per-file declarations) and the custom repository limits are all unchanged.
  • scripts/regressions/zip-reader.regression.ts adds:
    • padded and line-break archives read byte-identically to adm-zip;
    • libarchive written to a pipe (default, stored and ZIP64) when bsdtar/libarchive tar is installed;
    • padded .charx inspect, .marinara import, custom repository and package archive through their entry points;
    • two refusal cases for end records;
    • entry limits refused before the file list is read, at the reader, the .marinara route and the custom repository entry point;
    • a heap check that opening a 65,535-entry archive holds less memory than the archive.
  • The existing "bytes after the end record" refusal is unchanged. The package-install source guard now also requires the entry limit to be passed.

Claim boundary (risky work: import hardening)

  • Core claim: archives padded with zero bytes or line breaks after the end record are read as they were before #7532. Self-contradictory archives are still refused. Existing entry limits are enforced before the file list is read, with their existing messages. No size limit changed.
  • Entry points touched: installCatalogPackage, parseCustomAgentRepositoryContents, /api/import/marinara-package; .charx import and inspect go through the same reader with no caller change.
  • Not covered: a padded Agent package installed over the network. That path calls the same openZipArchive(archive, PACKAGE_ARCHIVE_BUDGET) that the regression exercises directly.

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 only lint warning is an existing, unrelated client warning.
  • 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, game-ruleset 53/53. game-ruleset-repository, native-export-memory, large-native-json-import, agent-registry-hydration, decision-conditionals, robustness-runtime-diagnostics and game-tool-controls each passed 1/1. --filter charx and --filter custom-agent match no regression file names; that coverage lives in zip-reader.
  • The new zip-reader regression fails against the reader on staging. Its first failure is the zero-padded archive.
  • Local libarchive corpus (macOS bsdtar 3.5.3 and libarchive 3.7.4): a .charx written with tar --format zip -cf - card.json assets > (10,240 bytes, about 9.7 KB of zeros after the end record) is inspected as before. The same card with \n or 512 zero bytes appended is also inspected as before.
  • Memory: after an archive is opened, the reader now holds less heap than the archive's own size. The regression asserts this for 65,535 entries; the reader on staging held several times the archive's size.
  • Local CodeRabbit (coderabbit review --agent --committed --base-commit d115a4406): 1 minor finding. It said one record that ends exactly at the end of the file and another followed only by padding should both count, so the archive is refused. That is fixed in the second commit, with a regression. CodeRabbit was not rerun after that small fix.

Manual verification notes

Needs a human:

  • Manually build a card on macOS with tar --format zip -cf - card.json assets > Card.charx and import it through Import Character. Also inspect it through the multi-file import flow and check the name and icon.
  • Manually add or sync a custom Agent repository from an archive that has padding after it, and confirm the preview and apply.
  • Manually import a normal .marinara package and a normal .charx exported by RisuAI, and confirm nothing changed for ordinary files.

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)

UI evidence (if applicable)

Not applicable: server-side archive reading only.

Summary by CodeRabbit

  • Bug Fixes
    • Improved compatibility when importing ZIP-based packages and repositories created by common archive tools, including archives with valid trailing padding.
    • Archive imports now enforce entry-count limits before processing file lists, helping prevent excessive resource use and returning clearer errors when limits are exceeded.
    • Improved validation of malformed archives, including duplicate end records and overlapping entries.

🔄 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/7538 **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/archive-reader-followup` --- ### 📝 Commits (2) - [`b136334`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b13633484cef5770e046d4ee3ee6b250ddc68a95) fix(import): accept padded archives and check entry limits before the file list - [`d682c6b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d682c6b8912c78bbb73a6a9b89345c71b4bc8724) fix(import): refuse an archive with an exact and a padded end record ### 📊 Changes **5 files changed** (+354 additions, -142 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/lib/zip-reader.ts` (+159 -120) 📝 `packages/server/src/routes/import.routes.ts` (+10 -10) 📝 `packages/server/src/services/agents/custom-agent-repositories.service.ts` (+6 -2) 📝 `packages/server/src/services/capability-packages/package-manager.service.ts` (+9 -3) 📝 `scripts/regressions/zip-reader.regression.ts` (+170 -7) </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 Refs #7531 (follow-up to #7532, which is merged) ## Why this change - The strict archive reader from #7532 refused some honest archives that imported before. libarchive, the `tar` on macOS and on Windows 10/11, pads a ZIP it writes to a pipe (`tar --format zip -cf - … > card.charx`) with zero bytes up to a whole block. Some transfers also leave a line break after the file. Those `.charx` cards, custom Agent repository archives, Agent packages and `.marinara` packages were refused with "The file is not a readable ZIP archive". `adm-zip`, Python `zipfile` and Info-ZIP `unzip` all read them. - Callers that already have an entry limit (`.marinara` packages: 8, Agent packages and custom repositories: 8,192) checked it only after the reader had built every entry. The reader now checks that limit against the end record before it reads the file list, and it keeps a small index instead of a full entry object per file. As a result, a long file list takes less memory than the archive itself. ## What changed - **Padding after the archive is accepted.** An end record still has to close the file, but zero bytes and line breaks after its comment are now allowed. Other bytes after the end record are still refused. Two end records that could each close the file are still refused, and that now includes one record that ends exactly at the end of the file alongside another that is followed only by padding. - **Entry limits are checked before the file list is read.** `openZipArchive(buffer, budget?)` takes the caller's existing limit and message. The `.marinara` route passes 8 and keeps ".marinara package has too many entries". Agent package install and custom repositories pass `PACKAGE_ARCHIVE_BUDGET` (8,192, "Package contains too many files"). `validatePackageArchiveEntries` keeps its own check. `.charx` has no entry limit and none is added. - **The reader keeps an index of names and record positions instead of building every entry up front.** Entries are built when `getEntry` or `getEntries` asks for them. The overlap check now runs on two sorted `Float64Array`s instead of an array of objects. Every structural check is the same and still runs when the archive is opened. - **No size limit or user-facing message changed.** The 256 MB import upload limit, the `.marinara` 1 GB / 256 MB-per-entry limits, the Agent artifact limits (100 MB artifact, 250 MB expanded, 1 MB manifest, per-file declarations) and the custom repository limits are all unchanged. - `scripts/regressions/zip-reader.regression.ts` adds: - padded and line-break archives read byte-identically to `adm-zip`; - libarchive written to a pipe (default, stored and ZIP64) when `bsdtar`/libarchive `tar` is installed; - padded `.charx` inspect, `.marinara` import, custom repository and package archive through their entry points; - two refusal cases for end records; - entry limits refused before the file list is read, at the reader, the `.marinara` route and the custom repository entry point; - a heap check that opening a 65,535-entry archive holds less memory than the archive. - The existing "bytes after the end record" refusal is unchanged. The package-install source guard now also requires the entry limit to be passed. ### Claim boundary (risky work: import hardening) - **Core claim:** archives padded with zero bytes or line breaks after the end record are read as they were before #7532. Self-contradictory archives are still refused. Existing entry limits are enforced before the file list is read, with their existing messages. No size limit changed. - **Entry points touched:** `installCatalogPackage`, `parseCustomAgentRepositoryContents`, `/api/import/marinara-package`; `.charx` import and inspect go through the same reader with no caller change. - **Not covered:** a padded Agent package installed over the network. That path calls the same `openZipArchive(archive, PACKAGE_ARCHIVE_BUDGET)` that the regression exercises directly. ## 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 only lint warning is an existing, unrelated client warning. - `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, `game-ruleset` 53/53. `game-ruleset-repository`, `native-export-memory`, `large-native-json-import`, `agent-registry-hydration`, `decision-conditionals`, `robustness-runtime-diagnostics` and `game-tool-controls` each passed 1/1. `--filter charx` and `--filter custom-agent` match no regression file names; that coverage lives in `zip-reader`. - The new `zip-reader` regression fails against the reader on `staging`. Its first failure is the zero-padded archive. - Local libarchive corpus (macOS `bsdtar` 3.5.3 and libarchive 3.7.4): a `.charx` written with `tar --format zip -cf - card.json assets >` (10,240 bytes, about 9.7 KB of zeros after the end record) is inspected as before. The same card with `\n` or 512 zero bytes appended is also inspected as before. - Memory: after an archive is opened, the reader now holds less heap than the archive's own size. The regression asserts this for 65,535 entries; the reader on `staging` held several times the archive's size. - Local CodeRabbit (`coderabbit review --agent --committed --base-commit d115a4406`): 1 minor finding. It said one record that ends exactly at the end of the file and another followed only by padding should both count, so the archive is refused. That is fixed in the second commit, with a regression. CodeRabbit was not rerun after that small fix. ### Manual verification notes Needs a human: - Manually build a card on macOS with `tar --format zip -cf - card.json assets > Card.charx` and import it through Import Character. Also inspect it through the multi-file import flow and check the name and icon. - Manually add or sync a custom Agent repository from an archive that has padding after it, and confirm the preview and apply. - Manually import a normal `.marinara` package and a normal `.charx` exported by RisuAI, and confirm nothing changed for ordinary files. ## 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) ## UI evidence (if applicable) Not applicable: server-side archive reading only. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved compatibility when importing ZIP-based packages and repositories created by common archive tools, including archives with valid trailing padding. * Archive imports now enforce entry-count limits before processing file lists, helping prevent excessive resource use and returning clearer errors when limits are exceeded. * Improved validation of malformed archives, including duplicate end records and overlapping entries. <!-- 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:28 +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#7782
No description provided.