Skip to content

PYTHON-6148 Handle malformed OCSP responses without raising from the TLS handshake - #3264

Open
xKrishnaSaxena wants to merge 1 commit into
mongodb:mainfrom
xKrishnaSaxena:PYTHON-6148-ocsp-malformed-response
Open

xKrishnaSaxena wants to merge 1 commit into
mongodb:mainfrom
xKrishnaSaxena:PYTHON-6148-ocsp-malformed-response

Conversation

@xKrishnaSaxena

Copy link
Copy Markdown

PYTHON-6148

Changes in this PR

_ocsp_callback runs inside the TLS handshake. It called load_der_ocsp_response without a guard on two paths: the stapled response, and the reply from the OCSP responder. When the bytes are not valid DER, cryptography raises ValueError. pyOpenSSL catches it in its C callback wrapper and re-raises it from do_handshake(). _wrap_socket_tls only catches OSError and SSL errors, so the raw ValueError reached the caller, and the socket was not closed.

Following the ticket's definition of done:

  • Unparseable stapled bytes are treated as no staple. The staple is now parsed before the no-staple branch, and a ValueError leaves response as None. So a must-staple cert still hard-fails. Otherwise the driver queries the OCSP responder and soft-fails if it gets no definitive status. A staple that parses but is invalid still hard-fails as before (unsuccessful status, failed verification, CertID mismatch).
  • An unparseable responder reply soft-fails. _get_ocsp_response logs it at debug level and returns None, the same as for a non-200 reply. The URI is skipped and nothing is cached.
  • Catch-all, fail closed. The body of _ocsp_callback moved unchanged into _check_ocsp. _ocsp_callback is now a small wrapper: it logs any unexpected exception at debug level (with traceback) and returns False. So nothing can escape into OpenSSL, and the handshake fails cleanly with "invalid status response", which the driver already turns into a connection failure. BLE is already ignored in the ruff config for intentional broad catches like this one. pyOpenSSL itself catches Exception in the same place.

Splitting the function keeps the diff small: the alternative was to re-indent the whole body under try. No public API changes.

Test Plan

New unit tests in test/test_ocsp_support.py:

  • TestGetOcspResponse.test_malformed_response: the responder returns 200 with bytes that are not OCSP. Result: None, nothing cached.
  • TestOcspCallback.test_stapled_malformed_soft_fail: malformed staple, no must-staple. The driver queries the responder, and with no definitive status the check soft-fails.
  • TestOcspCallback.test_stapled_malformed_must_staple_hard_fail: malformed staple on a must-staple cert. Hard fail.
  • TestOcspCallback.test_unexpected_error_hard_fail: an unexpected exception on a path that would otherwise soft-fail. The callback returns False and does not raise.
  • test_stapled_no_issuer now mocks _load_der_ocsp_response, because the staple is now parsed before the issuer check.

On main, all four new tests fail. With each part of the fix removed on its own, the test for that part fails: no responder guard, no staple guard, no catch-all, or a staple guard that skips the must-staple check. One case passes on purpose: with only the catch-all, the must-staple test still passes, because both paths hard-fail.

I also ran a real pyOpenSSL handshake in memory. The cert's AIA points to a local HTTP responder that replies 200 with bytes that are not OCSP. On main, do_handshake() raises ValueError. With this change the handshake completes (soft fail). With only the catch-all, it fails cleanly with SSL.Error: invalid status response.

Commands:

  • uv run --extra test --extra ocsp python -m pytest test/test_ocsp_support.py -m ocsp --deselect test/test_ocsp_support.py::TestOCSP: 67 passed. TestOCSP needs the Evergreen OCSP setup.
  • uv run --extra test --extra ocsp python -m pytest test/test_ocsp_cache.py: 3 passed.
  • uvx pre-commit run --all-files --hook-stage manual (just lint-manual): all hooks passed.
  • The just typing commands: mypy found no issues in the library, test, or typing-test runs, and pyright reported 0 errors.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

AI use: I used Claude Code (Claude Opus 5.5) to trace how the exception escapes the handshake, draft the change and tests, and run the checks.

@xKrishnaSaxena
xKrishnaSaxena requested a review from a team as a code owner November 29, 2026 19:15

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.

1 participant