Skip to content

Commit 07bed79

Browse files
committed
Fix deprecation warnings for Python 3.10 ssl module
1 parent d725a9b commit 07bed79

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

src/urllib3/contrib/pyopenssl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class UnsupportedExtension(Exception):
7676

7777
from .. import util
7878
from ..packages import six
79+
from ..util.ssl_ import PROTOCOL_TLS_CLIENT
7980

8081
__all__ = ["inject_into_urllib3", "extract_from_urllib3"]
8182

@@ -85,6 +86,7 @@ class UnsupportedExtension(Exception):
8586
# Map from urllib3 to PyOpenSSL compatible parameter-values.
8687
_openssl_versions = {
8788
util.PROTOCOL_TLS: OpenSSL.SSL.SSLv23_METHOD,
89+
PROTOCOL_TLS_CLIENT: OpenSSL.SSL.SSLv23_METHOD,
8890
ssl.PROTOCOL_TLSv1: OpenSSL.SSL.TLSv1_METHOD,
8991
}
9092

src/urllib3/contrib/securetransport.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import six
6868

6969
from .. import util
70+
from ..util.ssl_ import PROTOCOL_TLS_CLIENT
7071
from ._securetransport.bindings import CoreFoundation, Security, SecurityConst
7172
from ._securetransport.low_level import (
7273
_assert_no_error,
@@ -154,7 +155,8 @@
154155
# TLSv1 and a high of TLSv1.2. For everything else, we pin to that version.
155156
# TLSv1 to 1.2 are supported on macOS 10.8+
156157
_protocol_to_min_max = {
157-
util.PROTOCOL_TLS: (SecurityConst.kTLSProtocol1, SecurityConst.kTLSProtocol12)
158+
util.PROTOCOL_TLS: (SecurityConst.kTLSProtocol1, SecurityConst.kTLSProtocol12),
159+
PROTOCOL_TLS_CLIENT: (SecurityConst.kTLSProtocol1, SecurityConst.kTLSProtocol12),
158160
}
159161

160162
if hasattr(ssl, "PROTOCOL_SSLv2"):

src/urllib3/packages/ssl_match_hostname/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import sys
22

33
try:
4-
# Our match_hostname function is the same as 3.5's, so we only want to
4+
# Our match_hostname function is the same as 3.10's, so we only want to
55
# import the match_hostname function if it's at least that good.
6-
if sys.version_info < (3, 5):
6+
# We also fallback on Python 3.10+ because our code doesn't emit
7+
# deprecation warnings and is the same as Python 3.10 otherwise.
8+
if sys.version_info < (3, 5) or sys.version_info >= (3, 10):
79
raise ImportError("Fallback to vendored code")
810

911
from ssl import CertificateError, match_hostname

src/urllib3/util/ssl_.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def _const_compare_digest_backport(a, b):
7171
except ImportError:
7272
PROTOCOL_SSLv23 = PROTOCOL_TLS = 2
7373

74+
try:
75+
from ssl import PROTOCOL_TLS_CLIENT
76+
except ImportError:
77+
PROTOCOL_TLS_CLIENT = PROTOCOL_TLS
78+
7479

7580
try:
7681
from ssl import OP_NO_COMPRESSION, OP_NO_SSLv2, OP_NO_SSLv3
@@ -278,7 +283,11 @@ def create_urllib3_context(
278283
Constructed SSLContext object with specified options
279284
:rtype: SSLContext
280285
"""
281-
context = SSLContext(ssl_version or PROTOCOL_TLS)
286+
# PROTOCOL_TLS is deprecated in Python 3.10
287+
if not ssl_version or ssl_version == PROTOCOL_TLS:
288+
ssl_version = PROTOCOL_TLS_CLIENT
289+
290+
context = SSLContext(ssl_version)
282291

283292
context.set_ciphers(ciphers or DEFAULT_CIPHERS)
284293

@@ -313,13 +322,25 @@ def create_urllib3_context(
313322
) is not None:
314323
context.post_handshake_auth = True
315324

316-
context.verify_mode = cert_reqs
317-
if (
318-
getattr(context, "check_hostname", None) is not None
319-
): # Platform-specific: Python 3.2
320-
# We do our own verification, including fingerprints and alternative
321-
# hostnames. So disable it here
322-
context.check_hostname = False
325+
def disable_check_hostname():
326+
if (
327+
getattr(context, "check_hostname", None) is not None
328+
): # Platform-specific: Python 3.2
329+
# We do our own verification, including fingerprints and alternative
330+
# hostnames. So disable it here
331+
context.check_hostname = False
332+
333+
# The order of the below lines setting verify_mode and check_hostname
334+
# matter due to safe-guards SSLContext has to prevent an SSLContext with
335+
# check_hostname=True, verify_mode=NONE/OPTIONAL. This is made even more
336+
# complex because we don't know whether PROTOCOL_TLS_CLIENT will be used
337+
# or not so we don't know the initial state of the freshly created SSLContext.
338+
if cert_reqs == ssl.CERT_REQUIRED:
339+
context.verify_mode = cert_reqs
340+
disable_check_hostname()
341+
else:
342+
disable_check_hostname()
343+
context.verify_mode = cert_reqs
323344

324345
# Enable logging of TLS session keys via defacto standard environment variable
325346
# 'SSLKEYLOGFILE', if the feature is available (Python 3.8+). Skip empty values.

0 commit comments

Comments
 (0)