fix(certs): warn before retrying load_pem_private_key with password=None - #8345
Open
citizen204 wants to merge 1 commit into
Open
fix(certs): warn before retrying load_pem_private_key with password=None#8345citizen204 wants to merge 1 commit into
citizen204 wants to merge 1 commit into
Conversation
…its configured password Fixes mitmproxy#8138
lups2000
requested changes
Aug 6, 2026
lups2000
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR 🙌 I just left a % of nits
lups2000
requested changes
Aug 6, 2026
Comment on lines
739
to
744
| if password is not None: | ||
| logger.warning( | ||
| "A password was configured for a private key that is not encrypted. " | ||
| "Ignoring the password and loading the key without one." | ||
| ) | ||
| return load_pem_private_key(data, None) |
Member
There was a problem hiding this comment.
just noticed this: #8251 (review)
can you apply what he suggested (2)?
| assert cert.crl_distribution_points == crls | ||
|
|
||
|
|
||
| class TestLoadPemPrivateKey: |
Member
There was a problem hiding this comment.
Instead of defining a new test class, could we add a test like this to the TestCert class?
def test_unencrypted_private_key_with_password_logs_warning(self, tdata, caplog):
raw = Path(tdata.path("mitmproxy/data/testkey.pem")).read_bytes()
key = certs.load_pem_private_key(raw, b"password")
assert isinstance(key, rsa.RSAPrivateKey)
assert caplog.messages == [
"A password was specified, but the provided private key did not require a "
"password."
]This should cover everything.
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.
load_pem_private_key()inmitmproxy/certs.pycatchesTypeErrorfromcryptography'sload_pem_private_keyand silently retries withpassword=Nonewhenever the key turns out to be unencrypted. If a user configured a password for a certificate/key that isn't actually encrypted, this hides the misconfiguration entirely — the key loads fine and nothing indicates the password was ignored.This adds a
logger.warning(...)call before the fallback retry, so the mismatch is visible in the logs instead of being silently swallowed. Behavior is otherwise unchanged (the key still loads successfully).Fixes #8138
Changes
mitmproxy/certs.py: log a warning before falling back topassword=Noneinload_pem_private_keytest/mitmproxy/test_certs.py: addTestLoadPemPrivateKeycovering the no-password case and the password-on-unencrypted-key fallback (asserting the warning is logged viacaplog)