Skip to content

fix(certs): warn before retrying load_pem_private_key with password=None - #8345

Open
citizen204 wants to merge 1 commit into
mitmproxy:mainfrom
citizen204:fix-8138-warn-password-fallback
Open

fix(certs): warn before retrying load_pem_private_key with password=None#8345
citizen204 wants to merge 1 commit into
mitmproxy:mainfrom
citizen204:fix-8138-warn-password-fallback

Conversation

@citizen204

Copy link
Copy Markdown

load_pem_private_key() in mitmproxy/certs.py catches TypeError from cryptography's load_pem_private_key and silently retries with password=None whenever 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 to password=None in load_pem_private_key
  • test/mitmproxy/test_certs.py: add TestLoadPemPrivateKey covering the no-password case and the password-on-unencrypted-key fallback (asserting the warning is logged via caplog)

@lups2000 lups2000 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR 🙌 I just left a % of nits

Comment thread mitmproxy/certs.py
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)

@lups2000 lups2000 Aug 6, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just noticed this: #8251 (review)

can you apply what he suggested (2)?

assert cert.crl_distribution_points == crls


class TestLoadPemPrivateKey:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

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.

load_pem_private_key retries with password=None on TypeError

2 participants