[PR #1640] [MERGED] Preflight upload sizes before reading whole files into memory #5173

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/1640
Author: @Romuromylus
Created: 5/30/2026
Status: ✅ Merged
Merged: 5/30/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/upload-size-preflight


📝 Commits (2)

  • afbf2c6 Preflight upload sizes before reading whole files into memory
  • 85c8e8a Format file-payload test to repository Prettier style

📊 Changes

6 files changed (+100 additions, -15 deletions)

View changed files

📝 src/engine/contracts/constants/defaults.ts (+1 -0)
📝 src/features/modes/game/components/GameInput.tsx (+3 -1)
📝 src/shared/api/assets-api.ts (+6 -2)
📝 src/shared/api/file-payload.test.ts (+58 -1)
📝 src/shared/api/file-payload.ts (+11 -3)
📝 src/shared/api/import-api.ts (+21 -8)

📄 Description

Why this change

fileToUploadPayload already short-circuits oversized files before file.arrayBuffer() when given a maxBytes, but several callers invoked it (or FileReader) with no limit, so the entire file was read and base64-encoded before any size check ran:

  • formDataToJson is the funnel for every import-api file path but passed no limit, so chat imports read the whole file first.
  • uploadGameAsset read the whole asset before sending; the API layer never enforced a limit (only one of its two callers, the Settings panel, had an inline UI check).
  • GameInput.handleFileUpload carried an inline 20 * 1024 * 1024 magic number before FileReader.readAsDataURL.

What changed

  • file-payload.ts: formDataToJson gains an optional FilePayloadOptions (maxBytes/tooLargeMessage) and threads it to fileToUploadPayload, so a File entry is rejected before its bytes are read. Exported FilePayloadOptions + two message constants.
  • import-api.ts: filePayload/filesPayload accept and forward the options; the chat import paths (stChat, stChatIntoGroup) pass the CHAT_JSONL (50 MB) limit.
  • assets-api.ts: uploadGameAsset preflights against a new MAX_FILE_SIZES.GAME_ASSET (50 MB), matching the existing Settings upload gate and staying under the 75 MB Rust MAX_MEDIA_ASSET_BYTES server ceiling.
  • GameInput.tsx: replace the inline 20 MB magic number with MAX_FILE_SIZES.IMAGE_UPLOAD (same value, now named).

Verification

  • pnpm exec tsc -b clean.
  • New file-payload.test.ts cases prove formDataToJson rejects an oversized File before arrayBuffer is called, encodes a within-limit file while preserving string fields, and stays backward-compatible with no limit.
  • Full src/shared/api suite (92 tests) green.

The end-user upload triggers (game-asset upload, ST chat import) run through the native file picker, which the automated tiers don't drive; the size-preflight behavior they depend on is covered by the unit tests against formDataToJson/fileToUploadPayload.

Follow-ups (deferred, not blocking this PR)

The two remaining named import paths are intentionally left uncapped here because choosing a safe whole-file limit needs a maintainer decision, and a wrong value would reject valid imports:

  • stCharacterFile / stCharacterBatch / stCharacterInspect accept PNG character cards as well as JSON. The declared CHARACTER_JSON limit (5 MB) is too small for PNG cards, and the Rust import_st_character path enforces no whole-file cap, so applying 5 MB would reject valid cards.
  • marinaraFile uploads a .marinara ZIP. The Rust side caps data.json at 5 MB and the avatar at 20 MB per entry after unzip; the whole-package size has no server cap, so a tight whole-file limit would reject valid packages.

The mechanism to cap them (formDataToJson maxBytes) is now in place, so the follow-up is a one-line change per caller once the intended limits are chosen.

Linked issue

Closes #1538.


🔄 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/1640 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/30/2026 **Status:** ✅ Merged **Merged:** 5/30/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/upload-size-preflight` --- ### 📝 Commits (2) - [`afbf2c6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/afbf2c6a1dc8a56e8865f9d8b97b144b3e3d261f) Preflight upload sizes before reading whole files into memory - [`85c8e8a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/85c8e8adf921212e59351df39e50c4264a91e0e2) Format file-payload test to repository Prettier style ### 📊 Changes **6 files changed** (+100 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `src/engine/contracts/constants/defaults.ts` (+1 -0) 📝 `src/features/modes/game/components/GameInput.tsx` (+3 -1) 📝 `src/shared/api/assets-api.ts` (+6 -2) 📝 `src/shared/api/file-payload.test.ts` (+58 -1) 📝 `src/shared/api/file-payload.ts` (+11 -3) 📝 `src/shared/api/import-api.ts` (+21 -8) </details> ### 📄 Description ## Why this change `fileToUploadPayload` already short-circuits oversized files before `file.arrayBuffer()` when given a `maxBytes`, but several callers invoked it (or `FileReader`) with no limit, so the entire file was read and base64-encoded before any size check ran: - `formDataToJson` is the funnel for every import-api file path but passed no limit, so chat imports read the whole file first. - `uploadGameAsset` read the whole asset before sending; the API layer never enforced a limit (only one of its two callers, the Settings panel, had an inline UI check). - `GameInput.handleFileUpload` carried an inline `20 * 1024 * 1024` magic number before `FileReader.readAsDataURL`. ## What changed - `file-payload.ts`: `formDataToJson` gains an optional `FilePayloadOptions` (`maxBytes`/`tooLargeMessage`) and threads it to `fileToUploadPayload`, so a File entry is rejected before its bytes are read. Exported `FilePayloadOptions` + two message constants. - `import-api.ts`: `filePayload`/`filesPayload` accept and forward the options; the chat import paths (`stChat`, `stChatIntoGroup`) pass the `CHAT_JSONL` (50 MB) limit. - `assets-api.ts`: `uploadGameAsset` preflights against a new `MAX_FILE_SIZES.GAME_ASSET` (50 MB), matching the existing Settings upload gate and staying under the 75 MB Rust `MAX_MEDIA_ASSET_BYTES` server ceiling. - `GameInput.tsx`: replace the inline 20 MB magic number with `MAX_FILE_SIZES.IMAGE_UPLOAD` (same value, now named). ## Verification - `pnpm exec tsc -b` clean. - New `file-payload.test.ts` cases prove `formDataToJson` rejects an oversized File before `arrayBuffer` is called, encodes a within-limit file while preserving string fields, and stays backward-compatible with no limit. - Full `src/shared/api` suite (92 tests) green. The end-user upload triggers (game-asset upload, ST chat import) run through the native file picker, which the automated tiers don't drive; the size-preflight behavior they depend on is covered by the unit tests against `formDataToJson`/`fileToUploadPayload`. ## Follow-ups (deferred, not blocking this PR) The two remaining named import paths are intentionally left uncapped here because choosing a safe whole-file limit needs a maintainer decision, and a wrong value would reject valid imports: - **`stCharacterFile` / `stCharacterBatch` / `stCharacterInspect`** accept PNG character cards as well as JSON. The declared `CHARACTER_JSON` limit (5 MB) is too small for PNG cards, and the Rust `import_st_character` path enforces no whole-file cap, so applying 5 MB would reject valid cards. - **`marinaraFile`** uploads a `.marinara` ZIP. The Rust side caps `data.json` at 5 MB and the avatar at 20 MB *per entry after unzip*; the whole-package size has no server cap, so a tight whole-file limit would reject valid packages. The mechanism to cap them (`formDataToJson` `maxBytes`) is now in place, so the follow-up is a one-line change per caller once the intended limits are chosen. ## Linked issue Closes #1538. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:13:56 +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#5173
No description provided.