Skip to content

Commit 5877313

Browse files
committed
Revert psf#6667 to no longer cache a default SSLContext
1 parent 0e322af commit 5877313

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

Diff for: src/requests/adapters.py

+17-42
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,6 @@ def SOCKSProxyManager(*args, **kwargs):
7474
DEFAULT_POOL_TIMEOUT = None
7575

7676

77-
try:
78-
import ssl # noqa: F401
79-
80-
_preloaded_ssl_context = create_urllib3_context()
81-
_preloaded_ssl_context.load_verify_locations(
82-
extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)
83-
)
84-
except ImportError:
85-
# Bypass default SSLContext creation when Python
86-
# interpreter isn't built with the ssl module.
87-
_preloaded_ssl_context = None
88-
89-
9077
def _urllib3_request_context(
9178
request: "PreparedRequest",
9279
verify: "bool | str | None",
@@ -99,24 +86,11 @@ def _urllib3_request_context(
9986
scheme = parsed_request_url.scheme.lower()
10087
port = parsed_request_url.port
10188

102-
# Determine if we have and should use our default SSLContext
103-
# to optimize performance on standard requests.
104-
poolmanager_kwargs = getattr(poolmanager, "connection_pool_kw", {})
105-
has_poolmanager_ssl_context = poolmanager_kwargs.get("ssl_context")
106-
should_use_default_ssl_context = (
107-
_preloaded_ssl_context is not None and not has_poolmanager_ssl_context
108-
)
109-
11089
cert_reqs = "CERT_REQUIRED"
11190
if verify is False:
11291
cert_reqs = "CERT_NONE"
113-
elif verify is True and should_use_default_ssl_context:
114-
pool_kwargs["ssl_context"] = _preloaded_ssl_context
11592
elif isinstance(verify, str):
116-
if not os.path.isdir(verify):
117-
pool_kwargs["ca_certs"] = verify
118-
else:
119-
pool_kwargs["ca_cert_dir"] = verify
93+
pool_kwargs["ca_certs"] = verify
12094
pool_kwargs["cert_reqs"] = cert_reqs
12195
if client_cert is not None:
12296
if isinstance(client_cert, tuple) and len(client_cert) == 2:
@@ -314,26 +288,27 @@ def cert_verify(self, conn, url, verify, cert):
314288
:param cert: The SSL certificate to verify.
315289
"""
316290
if url.lower().startswith("https") and verify:
317-
conn.cert_reqs = "CERT_REQUIRED"
291+
cert_loc = None
318292

319-
# Only load the CA certificates if 'verify' is a string indicating the CA bundle to use.
320-
# Otherwise, if verify is a boolean, we don't load anything since
321-
# the connection will be using a context with the default certificates already loaded,
322-
# and this avoids a call to the slow load_verify_locations()
293+
# Allow self-specified cert location.
323294
if verify is not True:
324-
# `verify` must be a str with a path then
325295
cert_loc = verify
326296

327-
if not os.path.exists(cert_loc):
328-
raise OSError(
329-
f"Could not find a suitable TLS CA certificate bundle, "
330-
f"invalid path: {cert_loc}"
331-
)
297+
if not cert_loc:
298+
cert_loc = extracted_zipped_paths(DEFAULT_CA_BUNDLE_PATH)
299+
300+
if not cert_loc or not os.path.exists(cert_loc):
301+
raise OSError(
302+
f"Could not find a suitable TLS CA certificate bundle, "
303+
f"invalid path: {cert_loc}"
304+
)
332305

333-
if not os.path.isdir(cert_loc):
334-
conn.ca_certs = cert_loc
335-
else:
336-
conn.ca_cert_dir = cert_loc
306+
conn.cert_reqs = "CERT_REQUIRED"
307+
308+
if not os.path.isdir(cert_loc):
309+
conn.ca_certs = cert_loc
310+
else:
311+
conn.ca_cert_dir = cert_loc
337312
else:
338313
conn.cert_reqs = "CERT_NONE"
339314
conn.ca_certs = None

0 commit comments

Comments
 (0)