Skip to content

Encrypt strings inside name trees - #1773

Open
klimeryk wants to merge 1 commit into
foliojs:masterfrom
klimeryk:master
Open

Encrypt strings inside name trees#1773
klimeryk wants to merge 1 commit into
foliojs:masterfrom
klimeryk:master

Conversation

@klimeryk

Copy link
Copy Markdown

What kind of change does this PR introduce?

Fixes #1513

Ran into this while trying to add password support to my own project that uses pdfkit (well, a fork of it from @react-pdf/pdfkit): klimeryk/recalendar.js@f62f765. I'll be submitting the fix to them as well, but wanted to give back to the upstream first to verify the fix and make sure it's sound 🙇

In an encrypted document, the strings inside name trees are written unencrypted. Readers decrypt them like
every other string, get garbage, and the lookup fails. Anything that relies on a name tree is broken:

  • internal links to named destinations go nowhere (Encryption breaks GoTo Destinations #1513)
  • attachments cannot be found by name
  • named JavaScript never runs - and both its name and its body leak in plain text out of an encrypted document

The cause is that PDFObject.convert() discards the cipher for every PDFTree, so PDFTree.toString() has nothing to pass on when it serialises /Limits, the keys and the values — and name tree keys are PDF strings.

Checklist:

  • Unit Tests
  • Documentation - N/A, bugfix that does not modify the feature set
  • Update CHANGELOG.md
  • Ready to be merged

Tests:

tests/unit/tree.spec.js:

  • a tree is serialised in plain text without a cipher (to verify there's no regression for the plain text case) and fully encrypted with one
  • a document with different cases for name trees (attachment names, destination, etc.) is checked as well

The five encryption cases fail on master and pass with this change.
Of course, lint, format and full tests pass as well.

Fixes foliojs#1513

In an encrypted document, the strings inside name trees were being written
unencrypted. PDF readers would then try to decrypt them like every
other string, get garbage, and the lookup would fail.
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.

Encryption breaks GoTo Destinations

1 participant