Skip to content

fix(image): web images have their header dimensions checked before the browser decodes them - #1559

Merged
Hona merged 1 commit into
devfrom
fix/1422-web-image-side-limit
Nov 23, 2026
Merged

Hona merged 1 commit into
devfrom
fix/1422-web-image-side-limit

Conversation

@Hona

@Hona Hona commented Nov 23, 2026

Copy link
Copy Markdown
Member

Fixes #1422

What was wrong

  • crates/rune-image/src/from_url.js (blobRgba) called createImageBitmap first and checked maxSide only afterwards. A small PNG, JPEG, GIF or WebP that declares a huge raster made the browser decode the whole raster before Rune refused it.
  • This affected both web entry points: ImageCache::url/file (fetchImage) and ImageCache::load (decodeImage).

What changed

flowchart LR
  A[fetchBytes / load bytes] --> B["header::check (Rust)"]
  B -- over 8192 a side, over 256 MiB, malformed, unknown --> E[Image::error]
  B -- ok --> C[createImageBitmap]
  C --> D["post-decode maxSide check (kept)"]
Loading
  • New crates/rune-image/src/header.rs reads the declared size from the header, without decoding, for every format the web path gives to the browser:
  • Bytes that the check cannot recognize or bound are refused. They are not passed to the browser unchecked.
  • MAX_SIDE (8192) and MAX_ALLOC (256 MiB) are now defined once in lib.rs. The native image::Limits, the AVIF limits, animations, mips, pixels and the web check all use these constants, so the web and native limits cannot drift apart.
  • from_url.js: fetchBytes returns the raw bytes to Rust, which checks them before decodeImage calls createImageBitmap. The check after decoding stays as a second guard.

Notes

  • header_tests.rs builds each header from a few bytes in the test: valid sizes, the limit, one pixel over the limit, zero sizes, and attempts to hide a large frame inside a small container (a GIF frame, WebP VP8X with VP8L/ANMF, ICO payloads, AVIF with a small ispe but a large AV1 header). It also encodes small real PNG, JPEG, GIF, WebP, BMP and ICO images with image and checks that they pass.
  • These tests run on native. The browser fetch path was compiled for wasm32-unknown-unknown (rune-image and rune --no-default-features) but was not run in a browser.

@Hona
Hona merged commit 3bd2406 into dev Nov 23, 2026
1 check passed
@Hona
Hona deleted the fix/1422-web-image-side-limit branch November 23, 2026 06:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Web: image side limit is checked after createImageBitmap has decoded the raster

1 participant