Skip to content

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

Open

Description

@flight1973

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 e186970, 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.

Activity

  1. Mavrick-F commented on Nov 21, 2026

    @Mavrick-F
    Member

    Sorry I missed this for a couple of days, and thanks for the careful investigation, including working out why checking images from outside hayro wasn't reliable.

    I'd prefer limits inside hayro (option 1), so it can reject excessive decoding or drawing work with a useful error. That support isn't available yet, though. Its maintainer wants to design budgets and cancellation across the library (upstream discussion), and hasn't given a timetable.

    Could you start with a short assessment of how the existing upstream proposals cover your three cases, and what RustGIS would still need? If there's no practical route through that work, let's look at a scoped proposal for option 3 before writing much code. PDF import currently supports the desktop, command line and Python, so a native-only solution fits the surfaces we need to fix today.

    I wouldn't pursue option 2 on its own, for the reasons below.

    Please go ahead with the loss-reporting PR; those fixes are worth having independently.

    Implementation notes

    We need to bound both memory and work, and return a clear error on cancellation or exhaustion rather than accept a partial page.

    For option 1, limits need to cover parsing and intermediate decoded buffers before large allocations, aggregate retained memory, and repeated work across nested forms and patterns. Cancel checks between drawing operations won't interrupt a single long decode or rasterization.

    Upstream resource limits and cancellation are unmerged proposals. The cancellation proposal doesn't interrupt an active rasterization.

    For option 2, Device callbacks and interpret_page return (). A device can discard drawings but cannot ask the interpreter to stop walking nested forms. Earlier decoding allocations also remain outside its control.

    For option 3, the child needs a hard OS memory limit before parsing, plus parent-controlled Cancel and a deadline that kill and reap it. Transfers and concurrent helpers must be bounded, with cleanup on failure. The proposal should explain platform support and how the helper ships with the desktop app, CLI and Python package; Python's running executable isn't RustGIS. A child without a hard memory limit can still exhaust the machine. Any new crate or dependency needs review as part of that proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions