Repository navigation
PYTHON-6148 Handle malformed OCSP responses without raising from the TLS handshake - #3264
Open
xKrishnaSaxena wants to merge 1 commit into
Open
xKrishnaSaxena wants to merge 1 commit into
xKrishnaSaxena wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
PYTHON-6148
Changes in this PR
_ocsp_callbackruns inside the TLS handshake. It calledload_der_ocsp_responsewithout a guard on two paths: the stapled response, and the reply from the OCSP responder. When the bytes are not valid DER, cryptography raisesValueError. pyOpenSSL catches it in its C callback wrapper and re-raises it fromdo_handshake()._wrap_socket_tlsonly catchesOSErrorand SSL errors, so the rawValueErrorreached the caller, and the socket was not closed.Following the ticket's definition of done:
ValueErrorleavesresponseasNone. 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)._get_ocsp_responselogs it at debug level and returnsNone, the same as for a non-200 reply. The URI is skipped and nothing is cached._ocsp_callbackmoved unchanged into_check_ocsp._ocsp_callbackis now a small wrapper: it logs any unexpected exception at debug level (with traceback) and returnsFalse. So nothing can escape into OpenSSL, and the handshake fails cleanly with "invalid status response", which the driver already turns into a connection failure.BLEis already ignored in the ruff config for intentional broad catches like this one. pyOpenSSL itself catchesExceptionin 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 returnsFalseand does not raise.test_stapled_no_issuernow 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()raisesValueError. With this change the handshake completes (soft fail). With only the catch-all, it fails cleanly withSSL.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.TestOCSPneeds 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.just typingcommands: mypy found no issues in the library, test, or typing-test runs, and pyright reported 0 errors.Checklist
Checklist for Author
Checklist for Reviewer
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.