Skip to content

ROB: Improve RC4 fallback when cryptography can't provide it (#3398) - #3853

Merged
stefan6419846 merged 15 commits into
py-pdf:mainfrom
msylw:add-rc4-fallback-when-missing-in-openssl
Aug 20, 2026
Merged

stefan6419846 merged 15 commits into
py-pdf:mainfrom
msylw:add-rc4-fallback-when-missing-in-openssl

Conversation

@msylw

@msylw msylw commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

When OpenSSL is compiled without RC4 cryptography will not provide this as well. This breaks the fallback local RC4 implementation.

This change adds import time verification to cryptography's ability to handle RC4. If it fails, the existing fallback is used.

Only Applies to RC4, AES is not affected by this change.

AI (claude/sonnet) was used to debug the issue and draft the code. It was later edited for scope/simplicity.

@codecov

codecov Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.96%. Comparing base (11a0f77) to head (d24e2ae).
⚠️ Report is 50 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3853      +/-   ##
==========================================
+ Coverage   97.87%   97.96%   +0.09%     
==========================================
  Files          57       57              
  Lines       10760    11096     +336     
  Branches     2014     2078      +64     
==========================================
+ Hits        10531    10870     +339     
+ Misses        127      126       -1     
+ Partials      102      100       -2     

☔ 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.

@stefan6419846

Copy link
Copy Markdown
Collaborator

Thanks for the PR.

I am tempted to not touch the central imports for this, but detect this specific error when actually doing the problematic calls in the cryptography-based implementation (after having it detected, we can store the state in a suitable manner for mass processing). This avoids the import-time overhead of the availability check. Additionally, I guess we should issue a warning at least once when we use the fallback.

@msylw

msylw commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

I gave it another try, with the lazy detection approach. Is a little more complex than I expected, mostly as Cipher won't trigger the exception itself making CryptRC4 a little more complex than I hoped.

Just found cipher_supported . It could be used to initialize the _rc4_supported at init time, which could be a nice middle ground between the 2 solutions - probably lower overhead than the first version, but slightly higher than the 2nd one. Would make for much cleaner code, as there would be no need for the try blocks.

@msylw

msylw commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Just found cipher_supported . It could be used to initialize the _rc4_supported at init time, which could be a nice middle ground between the 2 solutions - probably lower overhead than the first version, but slightly higher than the 2nd one. Would make for much cleaner code, as there would be no need for the try blocks.

Never mind, it was an old version of cryptography, I can't find it in current docs...

@msylw
msylw marked this pull request as draft June 16, 2026 14:24
@msylw
msylw marked this pull request as ready for review June 17, 2026 09:28
Comment thread pypdf/_crypt_providers/_cryptography.py Resolved
Comment thread pypdf/_crypt_providers/_cryptography.py Resolved
@msylw

msylw commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

I made the suggested modifications, feel free to take a look.

@msylw
msylw requested a review from stefan6419846 July 20, 2026 10:11
Comment thread pypdf/_crypt_providers/_cryptography.py Resolved
Comment thread pypdf/_crypt_providers/_cryptography.py Resolved
Comment thread pypdf/_crypt_providers/_cryptography.py Resolved
Comment thread tests/test_encryption.py Resolved

@stefan6419846 stefan6419846 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience. Looks good to me now.

@stefan6419846
stefan6419846 merged commit c646ef2 into py-pdf:main Aug 20, 2026
25 of 34 checks passed
stefan6419846 added a commit that referenced this pull request Aug 23, 2026
## What's new

### Bug Fixes (BUG)
- Make remove_from_tree() work on outline items (#3939) by @RISHIKKASULA
- Be more lenient about space width in layout mode text extraction (#3992) by @PJBrs
- Do not scale the text leading by the font size (#3987) by @matthewkogan
- Update annotation appearance stream matrix on page transform (#3968) by @BashfulHippo
- Allow repeated page references (#3959) by @yuming-long

### Robustness (ROB)
- Improve RC4 fallback when cryptography can't provide it (#3853) by @msylw

[Full Changelog](6.16.1...6.16.2)
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.

2 participants