Fix #1585: handle /P permissions written as unsigned int32 in encrypted PDFs#1592
Open
andreasrosdalw wants to merge 1 commit into
Open
Fix #1585: handle /P permissions written as unsigned int32 in encrypted PDFs#1592andreasrosdalw wants to merge 1 commit into
andreasrosdalw wants to merge 1 commit into
Conversation
PDFs encrypted with RC4 40-bit by producers like mPDF store the /P permissions entry as an unsigned 32-bit integer (e.g. 4294963412) instead of the signed value required by ISO 32000 (-3884). PdfNumber stores values as double, and the direct double-to-int cast in intValue() clamps values above Integer.MAX_VALUE to Integer.MAX_VALUE (JLS 5.1.3) instead of preserving the bit pattern. The wrong /P value was fed into the MD5 that derives the RC4 key, so PdfReader threw BadPasswordException even though the user password was empty and every other reader (Acrobat, qpdf, PDFBox, Evince) opens the file. Convert through long to get two's complement wrapping, matching how other readers interpret out-of-range integers. Values in the signed 32-bit range are unaffected. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
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 #1585.
PDFs encrypted with RC4 40-bit (R=2, V=1) by producers like mPDF threw
BadPasswordException: Bad user passwordeven with an empty user password, while Acrobat, qpdf, PDFBox and Evince all open the same files fine.Root cause
mPDF writes the
/Ppermissions entry as an unsigned 32-bit integer (e.g.4294963412) instead of the signed value required by ISO 32000 (-3884) — the bit pattern is identical.PdfNumberstores values asdouble, andintValue()did a direct(int) valuecast. Per JLS §5.1.3, a double-to-int cast clamps values aboveInteger.MAX_VALUEto2147483647rather than wrapping, so thepValuefed into the MD5 that derives the RC4 key was wrong and no longer matched the/Uentry.Fix
4294963412now converts to-3884as other readers do. All values in the signed 32-bit range are unaffected (including fractional truncation semantics).Tests
New
PdfNumberTest:intValueWrapsUnsignedInt32— the mPDF value4294963412 → -3884, plus4294967292 → -4and2147483648 → Integer.MIN_VALUE. Fails on master, passes with the fix.intValueUnchangedForSignedRange— guards existing behavior for normal, boundary and fractional values.Full
openpdf-coresuite passes: 2086 tests, 0 failures — confirming nothing relied on the old clamping behavior.