Skip to content

fix(pdf): see a name in a PDF wherever a chunk boundary falls - #32

Merged
stephen-masters merged 1 commit into
mainfrom
fix/pdf-name-across-chunks
Nov 17, 2026
Merged

stephen-masters merged 1 commit into
mainfrom
fix/pdf-name-across-chunks

Conversation

@stephen-masters

Copy link
Copy Markdown
Contributor

The scan that finds names in a PDF's bytes could miss one that straddles the boundary between two reads, and could miscount one that ends exactly on it.

The two flaws

The scan reads the file a megabyte at a time and carries the end of each chunk into the next, so that a name across the boundary is seen whole.

  • A name missed. It carried 32 bytes, "longer than the longest escaped spelling of the name". That held for /Launch, the only name when the line was written. With every letter written as a #xx escape, /JBIG2Decode is 34 bytes and /CCITTFaxDecode is 43. One that starts 33 bytes before a boundary is whole in neither window, so a policy that refuses JBIG2 passed the file.
  • A name miscounted. A name that ended exactly where a chunk did was counted before the next byte was known. /Launcher, split after the h, was reported as a launch action.

The fix

  • What is carried is as long as the longest pattern being searched for. A pattern is longer than any spelling it matches, so the length cannot fall behind when a name is added.
  • It is taken from the window, not the chunk, so a name longer than a chunk is seen too.
  • A match that ends where the window does waits for the next chunk. If the file ends there, it counts.

Who was exposed

Only a policy that refuses an image codec (pdf_image_codecs), which the default does not. The names the default looks for (/Launch, /JavaScript, /JS, /Rendition) all fit in 32 bytes however they are escaped, so no launch action or script was ever missed this way.

Typed as a fix. It refuses a crafted file that passed before, but only under a policy that had already asked for that file to be refused.

Tests

  • Each name, plain and fully escaped, at every position across a boundary, both mid-file and as the last bytes of the file. The JBIG2 and CCITT cases fail on main.
  • A name longer than a chunk; /Launch followed across a boundary by er, 2 and #65r; several names found in different chunks.
  • All 689 real PDFs measured for 6.0.2 get the same verdict.
  • 532 tests at 99% coverage; ruff, mypy, uv lock --check and the docs build pass.

🤖 Generated with Claude Code

The scan for names reads the file a megabyte at a time and carried
the last 32 bytes of each chunk into the next, "longer than the
longest escaped spelling of the name". That was true of /Launch, the
only name when it was written. /JBIG2Decode with every letter escaped
is 34 bytes and /CCITTFaxDecode 43, so one that started 33 bytes
before a boundary was in neither window whole and went unseen: a
policy that refuses JBIG2 passed the file. What is carried is now as
long as the longest pattern, which is longer than any spelling it
matches, and comes from the window, so a name longer than a chunk is
seen too.

A name that ended exactly where a chunk did was also counted before
the next byte was known, so /Launcher split after the h was a launch
action. Such a match now waits for the next chunk, and counts if the
file ends there.

Only a policy that refuses a codec could be got round; the names the
default looks for all fit in 32 bytes. All 689 PDFs measured for
6.0.2 get the same verdict.

Co-authored-by: Yvette Mankerian <yvettekm@gmail.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@stephen-masters
stephen-masters merged commit 115a02f into main Nov 17, 2026
11 checks passed
@stephen-masters
stephen-masters deleted the fix/pdf-name-across-chunks branch November 18, 2026 09:36
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.

1 participant