ROB: Improve RC4 fallback when cryptography can't provide it (#3398)#3853
ROB: Improve RC4 fallback when cryptography can't provide it (#3398)#3853msylw wants to merge 11 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3853 +/- ##
=======================================
Coverage 97.86% 97.87%
=======================================
Files 57 57
Lines 10738 10777 +39
Branches 2006 2011 +5
=======================================
+ Hits 10509 10548 +39
Misses 127 127
Partials 102 102 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
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. |
|
I gave it another try, with the lazy detection approach. Is a little more complex than I expected, mostly as Just found |
Never mind, it was an old version of cryptography, I can't find it in current docs... |
|
|
||
|
|
||
| def _disable_rc4() -> None: | ||
| global _rc4_supported # noqa: PLW0603 |
There was a problem hiding this comment.
Instead of relying on a global variable, couldn't we make it a private member of CryptRC4 instead, id est using CryptRC4._rc4_supported for storage?
| from cryptography.hazmat.primitives.ciphers.modes import CBC, ECB | ||
| from cryptography.hazmat.primitives.padding import PKCS7 | ||
|
|
||
| from pypdf._crypt_providers import _fallback |
There was a problem hiding this comment.
Are we able to only import this if we really need it? I would assume that the default case is that we do not have issues, thus importing it here should not be required in these cases.
|
I made the suggested modifications, feel free to take a look. |
When
OpenSSLis compiled withoutRC4cryptography will not provide this as well. This breaks the fallback localRC4implementation.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,AESis 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.