Reading a PDF can use unbounded memory: the decoder and the renderer have no limit

Open
#2,482 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
backend, security

Research direction

Start with the PDF reader in pdf.rs, then inspect how hayro 0.9 handles FlateDecode streams, nested Form XObjects, and image decoding; the issue names memory:: limits and progress::check as possible integration points. The issue asks for a direction among three approaches, so first compare their coverage and platform limits. Done means PDF decoding and drawing have enforceable resource limits and can stop safely under the relevant memory and cancellation constraints.

Written by the indexing model from the issue text.

Description

The native PDF reader (#2352) caps the file at 1 GB, but nothing caps what decoding and drawing it takes. A small hostile PDF can make RustGIS ask for gigabytes, and when that allocation fails the process aborts. catch_unwind can't catch an allocation failure.

What I measured on main, with a test allocator capping the heap (pdf.rs and hayro 0.9.0 are unchanged as of e186970d, 2026-11-19):

  • A FlateDecode content or object stream of zeros: a 2 MB file peaks at 4.1 GB. Under a 300 MB cap, a 1 MB file aborts ("memory allocation of 536870912 bytes failed"). hayro-syntax inflates with read_to_end into an unbounded Vec.
  • Form XObjects that each paint the next one twice: every two levels of nesting cost 4× the time and memory (depth 20: 13 s and 450 MB from a 4 KB file). A 9 KB file at depth 50 aborts. hayro caps nesting depth at 50 but not total work, and hayro::render has no cancel hook.

Images are the third case. A 631-byte JPEG whose dictionary declares 65535×65535 makes the decoder ask for 12.9 GB. I tried refusing oversized images in RustGIS before drawing, by walking the page's images and reading each codec's header. I dropped it, because checking from outside hayro kept disagreeing with how hayro reads the same data:

  • it refused maps that open fine today (hayro already shrinks a JPEG header that claims more than its dictionary);
  • it refused pages over images they never draw, or over one damaged thumbnail;
  • it decoded every content stream twice, with no Cancel;
  • it could still be bypassed, for example by JBIG2 page-information order.

So all three cases come down to the same thing: hayro 0.9 has no setting to limit decoded size or drawing work.

Separately, I have fixes for what the reader drops without saying on my fork (branch fix/pdf-reader-says-what-it-drops), and I'll open them as their own PR. They don't bound memory. They:

  • say how often an image couldn't be decoded, or text was drawn in a stand-in font;
  • name fonts that aren't in the file;
  • say when a page's coordinates are in the OGC /LGIDict form, which RustGIS doesn't read;
  • read UTF-16 and PDFDocEncoding coordinate-system text (ISO 32000-1, Table D.2).

Options I can see:

  1. Add a decoded-size budget and an operation or cancel hook to hayro upstream, then set them from memory:: limits and progress::check.
  2. Drive hayro_interpret::interpret_page through our own Device that counts drawing work and checks Cancel. That covers the forms case but not inflation.
  3. Draw in a child process with a memory limit, which can be killed on Cancel. That covers both, but desktop only.

Which way would you like to go? I'm happy to do the work once there's a direction.

Dominant language
Rust
Stars
1.9k
Forks
268
Avg merge
11h 1m
Merged PRs (30d)
942

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from opengeos/RustGIS

All issues in opengeos/RustGIS

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.