Skip to content

fix(pdf): stop taking the bytes /JS in image data for JavaScript - #31

Merged
stephen-masters merged 1 commit into
mainfrom
fix/pdf-script-key-by-chance
Nov 17, 2026
Merged

stephen-masters merged 1 commit into
mainfrom
fix/pdf-script-key-by-chance

Conversation

@stephen-masters

Copy link
Copy Markdown
Contributor

sillybos refused ordinary scanned PDFs as "a PDF with JavaScript" when they held none. This stops it.

The flaw

Besides asking pdfinfo, the check scans the file's bytes for the names /JavaScript and /JS, because poppler before 26 does not report a script on an open-document action (21fd789). /JS is three bytes, and compressed image data spells any three bytes by chance:

Data Spells /JS about once in
Random bytes (a JPEG or Flate stream) 24 MB
ASCII85 text (how ReportLab writes its streams) 2 MB

Of 689 ordinary PDFs on one machine, 27 were refused for JavaScript. All 27 matched through /JS alone, and pdfinfo found a script in none. They include scanned books, digitised newspapers and searchable PDFs written with ReportLab. The chance grows with the size of the file, so the longer the scan, the likelier the refusal.

The fix

/JS now counts only in a file that also spells /Rendition. /JavaScript alone still counts, as before.

  • A script needs the name /JavaScript in one of two places: as the type of the action that carries it, or as the key of the document's script name tree.
  • ISO 32000-1 gives a JS entry to exactly two dictionaries: the JavaScript action (Table 217) and the rendition action (Table 214). The rendition action's type is /Rendition, so /JavaScript would not find its script. That is the one case /JS adds.

Skipping the bytes of streams would have been the obvious fix, and it would have opened a hole. A reader finds an object where the cross-reference table says it is, and the table may point into the middle of another object's stream. poppler 26.04 reads a JavaScript action placed there and reports it. The corpus now holds that file, so the scan still has to find it.

What it changes

  • All 27 files are no longer refused for JavaScript; 26 now pass, and one is still refused for a form it does carry. No other verdict among the 689 changed.
  • One kind of file that 6.0.1 refused now passes on a poppler before 26: an action whose /JS key is in plain bytes while its type name is reached through a reference into a compressed object stream. A file could already hide the whole action in such a stream, which the README says is not seen, so this gives a hostile file nothing it lacked.
  • Typed as a fix, like the commit that introduced the scan. It refuses nothing new.

Tests

  • A scan whose image data spells /JS( passes under DEFAULT and STRICT. It fails on main.
  • A script on a rendition action, and a script inside another object's stream, are refused, with and without pdfinfo reporting them.
  • Eleven spellings of the names through the scan itself, escaped names and near misses (/Renditions, /JSON) among them.
  • 520 tests at 99% coverage; ruff, mypy, uv lock --check, the docs build and the advisory record check pass.

🤖 Generated with Claude Code

The name scan added in 21fd789 took either /JavaScript or /JS anywhere
in the file for a script. /JS is three bytes, and compressed image
data spells any three bytes by chance: about once in 24 MB of random
bytes, and about once in 2 MB of the ASCII85 text ReportLab writes its
streams in. Of 689 ordinary PDFs, 27 were refused as "a PDF with
JavaScript", scanned books and newspapers among them, and pdfinfo
found a script in none.

/JS now counts only in a file that also spells /Rendition. A script
needs /JavaScript as the type of its action or as the key of the
document's script table; the rendition action is the one other
dictionary in ISO 32000-1 with a JS entry (Table 214), and its type
is not /JavaScript. All 27 files are no longer refused for
JavaScript, and no other verdict among the 689 changed.

Skipping the bytes of streams was not an option: the cross-reference
table may point into another object's stream, and poppler reads an
action placed there. The corpus gains that file, a script on a
rendition action, and a scan whose image data spells /JS(.

Co-authored-by: Yvette Mankerian <yvettekm@gmail.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@stephen-masters
stephen-masters merged commit 88874a3 into main Nov 17, 2026
11 checks passed
@stephen-masters
stephen-masters deleted the fix/pdf-script-key-by-chance 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