Fix #1576: DO_NOT_ENCRYPT_METADATA no longer silently disables content encryption#1589
Open
andreasrosdalw wants to merge 1 commit into
Open
Conversation
… embedded-files-only mode EMBEDDED_FILES_ONLY (24) contains the DO_NOT_ENCRYPT_METADATA bit (8), so detecting embedded-files-only mode with (mode & EMBEDDED_FILES_ONLY) != 0 also matched a mode that only sets DO_NOT_ENCRYPT_METADATA. As a result, asking for e.g. ENCRYPTION_AES_128 | DO_NOT_ENCRYPT_METADATA wrote /StmF and /StrF as /Identity, leaving all stream and string content unencrypted; Adobe Reader opens such a file without asking for a password. Require both bits of EMBEDDED_FILES_ONLY to be set instead, so DO_NOT_ENCRYPT_METADATA keeps content encryption and only leaves the /Metadata stream in plaintext, while EMBEDDED_FILES_ONLY (which implies unencrypted metadata) behaves as before. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 154 |
| Duplication | 6 |
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 #1576.
Passing
PdfWriter.DO_NOT_ENCRYPT_METADATAtosetEncryption(...)silently switched the output to embedded-files-only encryption mode:/StmFand/StrFwere written as/Identity, so all stream and string content was left unencrypted, and Adobe Reader opens the resulting file without a password prompt — even though the caller asked for e.g. AES-128 encryption with only the metadata in plaintext.Root cause
EMBEDDED_FILES_ONLY(24) is defined as the combination ofDO_NOT_ENCRYPT_METADATA(bit 3, value 8) and a dedicated embedded-files-only bit (bit 4, value 16). The detection inPdfEncryption.setCryptoModeonly tested for any of those bits:Fix
Require both bits to be set:
Resulting behavior:
AES_128AES_128 | DO_NOT_ENCRYPT_METADATA/StmF /Identity,/StrF /Identity,/EFF)/StmF /StdCF,/StrF /StdCF,/EncryptMetadata false)AES_128 | EMBEDDED_FILES_ONLYTests
New
DoNotEncryptMetadataTestcoverssetCryptoModeflag handling for all three modes plus two end-to-end tests asserting the/Encryptdictionary entries (/StmF,/StrF,/EFF,/AuthEvent,/EncryptMetadata) written byPdfStamperforDO_NOT_ENCRYPT_METADATAandEMBEDDED_FILES_ONLY. The two metadata-mode tests fail on current master and pass with the fix; the embedded-files-only tests guard against regressing that mode. Fullopenpdf-coresuite passes: 2085 tests, 0 failures.