Skip to content

Process abort (SIGABRT) on a valid PDF containing an inline image - lopdf 0.42.0 unwrap panic #189

Open

Description

@mangjaun80-hue

Summary

anydoc aborts the entire host process when converting a PDF whose content stream contains an inline image (BI ... ID ... EI). The PDF is valid — it is not malformed, encrypted, or scanned. Instead of returning Malformed (exit 1) or NeedsOcr (exit 3), the conversion path panics inside lopdf 0.42.0 and the non-unwinding panic calls abort(). There is no catchable error: node.exe is killed outright.

The panic is an unwrap() on a DictKey("ColorSpace") lookup in lopdf's parser:

thread '<unnamed>' panicked at ...\lopdf-0.42.0\src\parser\mod.rs:672:61:
called `Result::unwrap()` on an `Err` value: DictKey("ColorSpace")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread '<unnamed>' panicked at ...\library\core\src\panicking.rs:225:5:
panic in a function that cannot unwind
thread caused non-unwinding panic. aborting.

Environment

  • @firecrawl/anydoc 0.2.4 (napi addon, @firecrawl/anydoc-win32-x64-msvc)
  • Node.js v26.5.1
  • Windows 11 x64 (lopdf-0.42.0, i.e. the version that already carries the RUSTSEC-2026-0187 fix)

Reproduction

The generator below produces a 551-byte, single-page PDF. It is fully valid: pypdf parses it and reports pages = 1, extractable text length = 0.

#!/usr/bin/env python3
"""Minimal reproducer: anydoc aborts on a PDF containing an inline image."""
import subprocess
import sys

CONTENT = (
    b"q 8 0 0 8 0 0 cm "
    b"BI /W 8 /H 8 /BPC 8 /G true ID " + b"\x00" * 64 + b" EI Q\n"
)


def build_pdf() -> bytes:
    objs = [
        b"<< /Type /Catalog /Pages 2 0 R >>",
        b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
        b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] "
        b"/Resources << >> /Contents 4 0 R >>",
    ]
    out = bytearray(b"%PDF-1.4\n")
    offsets = []
    for i, obj in enumerate(objs, 1):
        offsets.append(len(out))
        out += b"%d 0 obj\n" % i + obj + b"\nendobj\n"
    offsets.append(len(out))
    out += b"4 0 obj\n<< /Length %d >>\nstream\n" % len(CONTENT)
    out += CONTENT
    out += b"\nendstream\nendobj\n"
    xref = len(out)
    out += b"xref\n0 5\n0000000000 65535 f \n"
    for off in offsets:
        out += b"%010d 00000 n \n" % off
    out += b"trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n" % xref
    return bytes(out)


if __name__ == "__main__":
    path = sys.argv[1] if len(sys.argv) > 1 else "inline-image.pdf"
    with open(path, "wb") as fh:
        fh.write(build_pdf())

Then:

npx -y @firecrawl/anydoc inline-image.pdf

The exact same 551 bytes, base64, if you would rather not run Python:

base64 of inline-image.pdf (551 bytes)
JVBERi0xLjQKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZyAvUGFnZXMgMiAwIFIgPj4KZW5kb2JqCjIgMCBvYmoKPDwgL1R5cGUgL1BhZ2VzIC9LaWRzIFszIDAgUl0gL0NvdW50IDEgPj4KZW5kb2JqCjMgMCBvYmoKPDwgL1R5cGUgL1BhZ2UgL1BhcmVudCAyIDAgUiAvTWVkaWFCb3ggWzAgMCAyMDAgMjAwXSAvUmVzb3VyY2VzIDw8ID4+IC9Db250ZW50cyA0IDAgUiA+PgplbmRvYmoKNCAwIG9iago8PCAvTGVuZ3RoIDExOCA+PgpzdHJlYW0KcSA4IDAgMCA4IDAgMCBjbSBCSSAvVyA4IC9IIDggL0JQQyA4IC9HIHRydWUgSUQgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACBFSSBRCgplbmRzdHJlYW0KZW5kb2JqCnhyZWYKMCA1CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDAwOSAwMDAwMCBuIAowMDAwMDAwMDU4IDAwMDAwIG4gCjAwMDAwMDAxMTUgMDAwMDAgbiAKMDAwMDAwMDIxOSAwMDAwMCBuIAp0cmFpbGVyCjw8IC9TaXplIDUgL1Jvb3QgMSAwIFIgPj4Kc3RhcnR4cmVmCjM4OAolJUVPRgo=

Observed exit codes

Platform Raw Meaning
Windows (cmd, PowerShell) -1073740791 0xC0000409 STATUS_STACK_BUFFER_OVERRUN (abort)
Python subprocess (unsigned) 3221226505 same value

Neither is in the documented set {0, 1, 2, 3}.

Narrowing: the inline image is the trigger, not the resources

I built five variants that differ only in page /Resources and content stream, to isolate the cause:

# /Resources content stream result
A << >> inline image abort
B << /ColorSpace [/DeviceGray] >> inline image abort
C << >> re f (rect, no image) exit 3 NeedsOcr — correct
D << >> BT ... Tj ET (text) exit 0 — correct
E key omitted entirely inline image abort

Two things worth noting from this matrix:

  • B declares /ColorSpace on the page and still aborts, so the trigger is not a missing ColorSpace declaration. The failing lookup is on the inline-image path itself.
  • The same document without the inline image converts normally (exit 3, page 1 of 1 needs OCR), so the parser is fine with the rest of the file.

Reproduced 5/5 runs, stable.

Expected behaviour

Per the README and the documented exit-code contract, a document that cannot be converted should return an error the caller can handle — Malformed (exit 1) for structurally unusable input, or NeedsOcr (exit 3) where applicable. Either would be fine here. A process abort is not handleable by any caller.

Relationship to #67

This looks distinct from the (closed) #67 / RUSTSEC-2026-0187:

  • transitive DoS via pdf-inspector -> lopdf 0.41.0 (RUSTSEC-2026-0187) #67 was an uncatchable SIGABRT from a stack overflow on a deeply nested catalog array, in lopdf 0.41.0, fixed by moving to lopdf 0.42.0.
  • This report is an unwrap() panic on DictKey("ColorSpace") in lopdf 0.42.0 — the version that carries the fix — from an ordinary inline image, with no nesting involved.

Worth flagging also that the concern raised in #71 applies directly here: a native (napi/cgo) binding has no containment, so the SIGABRT-class failure kills the host process. That is exactly what happens above.

Impact

anydoc's stated purpose is running arbitrary, untrusted office documents and PDFs through this code path. If the PDF parser can abort the process, then a single crafted (or merely unusual) PDF is a denial-of-service against the host: no exception, no error code, no cleanup — the process just dies. Inline images are legal PDF and do appear in real files (generated PDFs, receipts, some print/export pipelines), so this is not purely theoretical.

I have not checked whether this also reproduces through pdf-inspector directly or whether the fix belongs in lopdf, but the panic originates in lopdf's parser, so it may need an upstream report as well.

Happy to test a patch or provide more variants if useful.

Activity

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