Fix #1588: treat unresolvable /Encrypt trailer reference as unencrypted#1593
Open
andreasrosdalw wants to merge 1 commit into
Open
Fix #1588: treat unresolvable /Encrypt trailer reference as unencrypted#1593andreasrosdalw wants to merge 1 commit into
andreasrosdalw wants to merge 1 commit into
Conversation
…nencrypted When the trailer /Encrypt entry is an indirect reference to an object that has no usable xref entry (free or missing), getPdfObject() returns null and readDecryptedDocObj() dereferenced it, crashing with a bare NullPointerException on the partial-read path and an InvalidPdfException wrapping the same NPE on the full-read path. Other readers (poppler, qpdf) ignore such a dangling reference and open the document as unencrypted; do the same. Also covers the case where the /Encrypt entry resolves to a non-dictionary object, which previously died with a ClassCastException. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 4 |
| Duplication | 2 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
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.



Summary
Fixes #1588.
PdfReadercrashed on malformed PDFs whose trailer/Encryptentry is an indirect reference to an object with no usable xref entry (produced by some home-grown PDF generators). The partial-read constructor (new PdfReader(RandomAccessFileOrArray, byte[])) surfaced a bareNullPointerExceptionfromreadDecryptedDocObj(); the byte-array/filename constructors wrapped the same NPE in anInvalidPdfExceptionafter a failed rebuild.Root cause
getPdfObjectresolves the/Encryptreference through the xref table and returnsnullwhen the referenced object number has a free or missing entry.encwas never null-checked.Fix
If the
/Encryptentry does not resolve to a dictionary, treat the document as unencrypted — matching poppler and qpdf, which open this class of file without error and report it as not encrypted. The instanceof check also covers/Encryptresolving to a non-dictionary object, which previously died with aClassCastException.Tests
New
DanglingEncryptReferenceTestbuilds the minimal malformed PDF from the issue report (/Size 5,/Encrypt 4 0 R, object 4 has only a free xref entry) and asserts that both the partial-read and full-read paths open the document, reportisEncrypted() == falseand see the correct page count. Both tests fail on master (2/2) and pass with the fix. Fullopenpdf-coresuite passes.