Skip to content

BUG: Preserve merged content streams during encryption - #4477

Open
SyedTauhidUllahShah wants to merge 1 commit into
py-pdf:mainfrom
SyedTauhidUllahShah:codex/pypdf-encrypted-watermarks
Open

SyedTauhidUllahShah wants to merge 1 commit into
py-pdf:mainfrom
SyedTauhidUllahShah:codex/pypdf-encrypted-watermarks

Conversation

@SyedTauhidUllahShah

Copy link
Copy Markdown

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 ContentStream stores drawing operations and only builds their byte representation when needed. In the current settings, encryption reads _data directly, 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() for ContentStream objects 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.

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

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.21%. Comparing base (d7f50bf) to head (809592d).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Encrypting a PDF fails to watermark / Stamp

1 participant