fix(pdfkit): encrypt strings inside name trees - #3508
Closed
klimeryk wants to merge 1 commit into
Closed
Conversation
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. Based on fix submitted and merged upstream: foliojs/pdfkit#1773
🦋 Changeset detectedLatest commit: be261c9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Owner
Author
|
Hahahaha, indeed bad timing! 😆 But that's super good news overall, congrats on that big milestone, I know it's been in the works for a while 🎉 And this fix has been already merged upstream (foliojs/pdfkit#1773) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since encryption landed in #3344, setting
userPasswordon a<Document>silently breaks two things:<Link src="#chapter-2">to a node withid="chapter-2"no longer navigates. Firefox's viewer logsgoToDestination: "null" is not a valid destination array, for dest="chapter-2".ctx.file()embeds the file, but its name comes back as garbage, so it cannot be looked up.Both are the same bug, inherited from upstream pdfkit (foliojs/pdfkit#1513). I've ran into this while trying to add password support to my own project that uses react-pdf: klimeryk/recalendar.js@f62f765.
I've submitted this patch and it got merged upstream already: foliojs/pdfkit#1773 🙇
Cause
Strings inside name trees are written unencrypted in an otherwise encrypted document. Readers decrypt them like every other string, get garbage, and the lookup fails.
PDFObject.convert()discards the cipher for everyPDFTree, soPDFTree.toString()has nothing to pass on when it serialises/Limits, the keys and the values — and name tree keys are PDF strings.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:
Reproduction
null[ 'v*\x8E(äì\x91ı' ][ { num: 12, gen: 0 }, { name: 'XYZ' }, 72, 720, null ][ 'data.txt' ]Without a password both cases already printed the correct values.
Tests
packages/pdfkit/tests/tree.test.ts:PDFNameTreeandPDFNumberTree(whose keys are integers, so only its values are encrypted)(heading),(data.txt)andapp.alertwhen unencrypted, and none of them when encrypted — checked across RC4, AES-128 and AES-256The eleven encryption cases fail on
masterand pass with this change.