[GH-ISSUE #2616] [Issue]: PNG character-card parser reads chunk length as signed int32 — crafted card freezes the browser tab #1289

Closed
opened 2026-10-21 05:30:44 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Medium — crash / DoS. The client PNG character-card parser reads the 4-byte chunk length with signed 32-bit bitwise ops. A crafted card whose chunk-length field has the high bit set decodes to a negative length, sending the chunk-walk cursor far negative and pinning the JS main thread for ~13-16 seconds (empirically reproduced), freezing the tab while importing the card.

Expected behavior

Parsing a malformed/crafted PNG card should fail fast (reject the file), not block the main thread.

Actual behavior

// packages/client/src/lib/png-parser.ts (~41-106)
const length = (bytes[offset] << 24) | (bytes[offset+1] << 16) | (bytes[offset+2] << 8) | bytes[offset+3]; // ~43 signed
// ...
offset += length + 4; // ~102 — negative length sends offset deep negative

A chunk length of 0x80000000 decodes to a negative number. From then on, out-of-bounds reads return undefined (coerced to 0 by <<), so length stays 0, the type never matches IEND, and offset only advances +12 per iteration inside while (offset < bytes.length). Empirically: a 100-byte buffer whose first chunk length is 0x80000000 started offset at ~-2.1 billion and ran 66M+ iterations in 5s, still at offset -1.35B — a guaranteed multi-second full main-thread freeze. parsePngCharacterCard runs on remote/attacker-controlled bytes from the bot-browser import paths (BotBrowserView.tsx:1542, BotBrowserModal.tsx:200), matching the malicious-imported-content threat model. (Not literally infinite — offset eventually climbs back past bytes.length and exits — but the freeze is severe.)

Steps to reproduce

  1. Craft a PNG whose first chunk's 4-byte length field has the high bit set (e.g. 0x80000000); no valid character data is needed.
  2. Import it as a character card (or have the bot browser fetch it).
  3. Observe the tab freeze for ~13-16s during the chunk-walk.

Environment

  • Marinara Engine version: staging @ 56f700cd (v2.0.0)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

Logs, screenshots, or video

Static source audit, cited to file:line above. Empirically reproduced in Node (66M+ loop iterations on a crafted 100-byte buffer).

Additional context

Fix sketch. Read the length unsigned (>>> 0, or DataView.getUint32(offset, false)) and add loop guards: bail if length < 0, if length is absurdly large, or if offset + 8 + length overruns bytes.length; and break if offset does not strictly increase. The in-repo reference is read-zip-text.ts:9-15, whose readUint32 already reads unsigned via >>> 0 — png-parser.ts does not.

Regression notes. Not a regression — png-parser.ts is byte-identical on main (same signed read, same offset += length + 4). Shared pre-existing bug on both branches.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 18, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2616 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Medium — crash / DoS.** The client PNG character-card parser reads the 4-byte chunk length with **signed** 32-bit bitwise ops. A crafted card whose chunk-length field has the high bit set decodes to a negative length, sending the chunk-walk cursor far negative and pinning the JS main thread for ~13-16 seconds (empirically reproduced), freezing the tab while importing the card. ## Expected behavior Parsing a malformed/crafted PNG card should fail fast (reject the file), not block the main thread. ## Actual behavior ```ts // packages/client/src/lib/png-parser.ts (~41-106) const length = (bytes[offset] << 24) | (bytes[offset+1] << 16) | (bytes[offset+2] << 8) | bytes[offset+3]; // ~43 signed // ... offset += length + 4; // ~102 — negative length sends offset deep negative ``` A chunk length of `0x80000000` decodes to a negative number. From then on, out-of-bounds reads return `undefined` (coerced to `0` by `<<`), so `length` stays `0`, the type never matches `IEND`, and `offset` only advances `+12` per iteration inside `while (offset < bytes.length)`. Empirically: a 100-byte buffer whose first chunk length is `0x80000000` started `offset` at ~-2.1 billion and ran 66M+ iterations in 5s, still at offset -1.35B — a guaranteed multi-second full main-thread freeze. `parsePngCharacterCard` runs on remote/attacker-controlled bytes from the bot-browser import paths (`BotBrowserView.tsx:1542`, `BotBrowserModal.tsx:200`), matching the malicious-imported-content threat model. (Not literally infinite — `offset` eventually climbs back past `bytes.length` and exits — but the freeze is severe.) ## Steps to reproduce 1. Craft a PNG whose first chunk's 4-byte length field has the high bit set (e.g. `0x80000000`); no valid character data is needed. 2. Import it as a character card (or have the bot browser fetch it). 3. Observe the tab freeze for ~13-16s during the chunk-walk. ## Environment - Marinara Engine version: `staging @ 56f700cd` (v2.0.0) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## Logs, screenshots, or video Static source audit, cited to file:line above. Empirically reproduced in Node (66M+ loop iterations on a crafted 100-byte buffer). ## Additional context **Fix sketch.** Read the length unsigned (`>>> 0`, or `DataView.getUint32(offset, false)`) and add loop guards: bail if `length < 0`, if `length` is absurdly large, or if `offset + 8 + length` overruns `bytes.length`; and break if `offset` does not strictly increase. The in-repo reference is `read-zip-text.ts:9-15`, whose `readUint32` already reads unsigned via `>>> 0` — `png-parser.ts` does not. **Regression notes.** Not a regression — `png-parser.ts` is byte-identical on `main` (same signed read, same `offset += length + 4`). Shared pre-existing bug on both branches. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:44 +07:00
  • closed this issue
  • added the
    client
    bug
    labels
Author
Owner

@Romuromylus commented on GitHub (Jun 18, 2026):

Fixed in #2623, merged to staging (e1132e5b). Closing manually since the PR targeted staging rather than the default branch.

<!-- gh-comment-id:4742295850 --> @Romuromylus commented on GitHub (Jun 18, 2026): Fixed in #2623, merged to `staging` (e1132e5b). Closing manually since the PR targeted `staging` rather than the default branch.
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#1289
No description provided.