Repository navigation
BUG: Preserve merged content streams during encryption - #4477
Open
SyedTauhidUllahShah wants to merge 1 commit into
Open
SyedTauhidUllahShah wants to merge 1 commit into
SyedTauhidUllahShah wants to merge 1 commit into
Conversation
Serialize ContentStream operations before encrypting their bytes, while preserving the encoded data of other streams. Add regression coverage for merged watermarks and compressed streams with RC4 and AES. Fixes py-pdf#4476. The watermark regression test is based on MartinThoma’s reproducer in that issue. Developed with assistance from OpenAI Codex (GPT-6).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4477 +/- ##
=======================================
Coverage 99.21% 99.21%
=======================================
Files 59 59
Lines 12425 12426 +1
Branches 2368 2368
=======================================
+ Hits 12328 12329 +1
Misses 42 42
Partials 55 55 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
This pull request fixes #4476.
Problem description
When a watermark is merged into a page, saving the PDF with encryption can silently drop the watermark. This happens because the merged
ContentStreamstores drawing operations and only builds their byte representation when needed. In the current settings, encryption reads_datadirectly, which can still be empty at that point, so the watermark’s drawing instructions never reach the output.Fix
To address this, the change calls
get_data()forContentStreamobjects before encrypting them, so their drawing instructions are converted into bytes first. Other streams keep their existing encoded bytes and filters, because decoding a compressed stream without removing its filter would produce incorrect output.Regression tests
The tests check that the watermark survives whether encryption is configured before or after merging, using both RC4 and AES. They also verify that the original page text and the stamp’s text remain intact, and that compressed streams retain their encoded bytes and filter declarations.
Validation
All four watermark cases fail before the fix and pass afterward. I also checked the related encryption, page, writer, and stream tests, which report 652 passed, 4 skipped, and 83 deselected.
Acknowledgment
Thanks to MartinThoma for identifying the cause and sharing a reproducer, which the watermark regression test is based on.