Enforce that Session is only re-used with the Context it came from#1512
Merged
Conversation
OpenSSL requires that a session passed to SSL_set_session originate from a compatible SSL_CTX, but neither pyOpenSSL nor OpenSSL verified this (rust-openssl marks SslRef::set_session unsafe for this reason). Make the contract checkable: Session now pins the Context of the Connection it was obtained from, and Connection.set_session raises ValueError if it doesn't match the Connection's Context. This is stricter than the C API, but makes the API sound. https://claude.ai/code/session_01UewVp2VriZJPGm96VFVZ8k
reaperhulk
approved these changes
Jun 8, 2026
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.
SSL_set_sessionrequires that the session originate from a compatibleSSL_CTX, but neither pyOpenSSL nor OpenSSL verifies this — rust-openssl marksSslRef::set_sessionasunsafe fnfor precisely this reason.This makes the contract checkable instead of deprecating the API:
Sessionnow pins theContextof theConnectionit was obtained from (Connection.get_sessionsets it).Connection.set_sessionraisesValueErrorif the session's pinnedContextis not the same object as the connection's currentContext. Identity is checked against the current context, so it remains correct acrossset_context.This is stricter than the C API (it requires the same
Contextobject, not merely a "compatible" one), but sinceContextwraps itsSSL_CTX1:1, object identity is the only check that's trivially sound.Test changes:
test_client_set_sessionpreviously created a fresh clientContextper connection — exactly the unverifiable pattern this change forbids. It now reuses one clientContextacross both connections, making it a real resumption test.test_set_session_wrong_methodbecametest_set_session_wrong_context: the mismatch is now rejected eagerly withValueErrorrather than provoking anErrordeep in OpenSSL during a second handshake.Changelog entry added under 26.3.0 backward-incompatible changes.
https://claude.ai/code/session_01UewVp2VriZJPGm96VFVZ8k
Generated by Claude Code