Inspiration

VectorCraft is a vector art app built from scratch in Rust and developed in the open. It imports EMF and WMF files, whose image records can embed a PNG or JPEG. Issue #3600, an audit by PierrunoYT, showed that each embedded bitmap was capped at 2²⁶ pixels (8192 × 8192, 256 MB decoded, from about 65 KB of PNG), but the file as a whole had no cap. A small file with many such records could cost "minutes of CPU". The audit rated this plausible from the source, without running it end to end, and proposed a total budget per file, for example 2²⁸ pixels.

What it does

storytold/vectorcraft#4215 gives every imported metafile one budget of 2²⁸ decoded pixels, four of the largest bitmap a record may hold. Each bitmap's width times height is taken from that budget before it's decoded. Images past the budget are left out with a dedicated warning, "images past the most pixels one file may hold were left out", while the rest of the drawing imports normally. Files under the budget behave exactly as before.

How I built it

The decoder, dib::decode, had no state between calls, and the EMF and WMF importers called it once per record. I gave the importer's per file Player a pixels_left field and passed it into decode as left: &mut u64. A helper does the charging:

*left = left.checked_sub(n).ok_or(OVER_BUDGET)?;

Trimmed to its key line: checked_sub refuses to go below zero, and the early return leaves left unchanged, so a refused bitmap costs nothing. For embedded PNG and JPEG, the charge sits after the header size check and before image::load_from_memory. Raw bitmaps are charged after their data length check, since a 1 bit bitmap still expands 32 times into RGBA. Player::image maps the over budget error to its own warning instead of the generic "couldn't be read" one.

Challenges I ran into

Testing many maximum size images without decoding gigabytes. I added a shared helper, png_claiming, which writes a 1 × 1 PNG and patches its header (and the header's CRC checksum) to claim any size, so the size reads correctly but decoding fails fast. The PNG checksum helper moved from one test module to the shared test file so both use one copy.

Accomplishments that I'm proud of

My regression test, embedded_bitmaps_share_one_pixel_budget, hand builds an EMF holding five StretchDIBits records, and every one claims 8192 × 8192. It fails on main, where all five are attempted, and passes with the fix, where the fifth is refused from its header. A unit test checks exact charging for a small raw bitmap and that a refused one is free. cargo test -p vectorcraft-metafile passed (31 tests), with clippy and fmt clean. About seven minutes after it was opened, bflatastic merged it; their comment afterwards said charging from the header before inflating "is exactly right".

What I learned

Limits per record and limits per file guard different things, and a parser reading untrusted files needs both. Where a check sits matters as much as what it checks: a budget charged after decoding would have arrived too late to save anything.

What's next

CodeRabbit pointed out a fair follow up before the merge: images drawn with raster operations other than a plain copy are skipped only after decoding, so they still spend budget and could crowd out a later normal image. Checking the raster operation before decoding would close that gap.

The full story, with all the code, is on my blog: https://ashokramsi.hashnode.dev/vectorcraft-bitmap-budget

Built With

Share this project:

Updates