session.http: fix custom SSLContext + verify=False #6205
Merged
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.
Resolves #6204
Really not sure if this is the right approach to solve this. This might just be a bug in
urllib3
.When
--http-no-ssl-verify
is set,verify
is set toFalse
on theHTTPSession(requests.Session)
. This leads tocert_reqs=ssl.CERT_NONE
being set on theHTTPSConnectionPool
. Eventually,urllib3
will make the TLS connection and verify it. Nothing unusual so far.However, when the user has also set
--http-disable-dh
, we mount a customHTTPAdapter
on thehttps://
scheme, which includes a customSSLContext
where the Diffie-Hellman key exchange is disabled. ThisSSLContext
has all default values set otherwise. It will of course also be passed to theHTTPSConnectionPool
and taken into account while validating the TLS connection.The issue here is that
urllib3
does the following:https://github.com/urllib3/urllib3/blob/2.2.3/src/urllib3/connection.py#L860-L871
It tries to set the context's
verify_mode
attribute again, based on thecert_reqs
parameter which was set on theHTTPSConnectionPool
viaverify=False
. This however raises aValueError
, because our customSSLContext
from our customHTTPAdapter
is being used, where the default values are set, includingcheck_hostname=True
.So when
check_hostname is True
andverify_mode
is attempted to be set tossl.CERT_NONE
, this won't work.Hence the
HTTPAdapter.send()
override of this PR, which alters the attributes of theSSLContext
based on theverify
value before sending the HTTPS request.master
PR