Repository navigation
Shared SSLContext can race between HTTP/1.1 and HTTP/2 connections via ALPN mutation #1123
Replies: 1 comment
|
With the current code, I would use a separate I checked this with actual local TLS and HTTP requests on your commit
So this can affect the protocol actually used, not only the list observed by a fake backend. In the sync connection handler, negotiated A practical workaround is to build equivalent contexts separately when creating the pools. from contextlib import ExitStack
import ssl
import httpcore
def make_context():
return ssl.create_default_context()
with ExitStack() as stack:
h1 = stack.enter_context(
httpcore.ConnectionPool(ssl_context=make_context(), http2=False)
)
h2 = stack.enter_context(
httpcore.ConnectionPool(ssl_context=make_context(), http2=True)
)
# Send requests through the pool with the intended protocol policy.If your existing context has custom trust roots, a client certificate or other TLS settings, apply those same settings in the builder. Create the contexts once per pool rather than once per request. Pre-setting ALPN on a shared context will not help because This is a mitigation for the observed behavior, not a statement of the maintainers' intended sharing contract. The runtime check covered synchronous direct connections on Python 3.14.6 with certificate and hostname verification enabled. It used controlled scheduling and did not test async or proxy connections. |
Uh oh!
There was an error while loading. Please reload this page.
Summary
The current
mastermutates a caller-providedssl.SSLContextinHTTPConnection._connect():When two concurrent connections share the same context, one configured with
http2=Falseand one withhttp2=True, the ALPN configuration can race.Reproduction
Using current master at
10a658221deb38a4c5b16db55ab554b0bf731707, a deterministicfake network backend synchronizes both
start_tls()calls.Observed:
The HTTP/1.1 connection receives the ALPN list written by the concurrent HTTP/2
connection.
The same behavior reproduces in:
Expected behavior
One connection should not change the TLS protocol configuration observed by
another connection merely because they share a caller-provided
SSLContext.Question
Is sharing a caller-provided
SSLContextbetween connections with differenthttp2settings supported? If so, should httpcore isolate the ALPN configurationper connection instead of mutating the shared context?
All reactions