Skip to content
Myr

Merge pull request #6716 from sigmavirus24/bug/6715

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nate Prewitt · committed by GitHub · parents 88dce9d, b1d73dd · Browse files · 2 files+81 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+81 added−10 removed

  • other:+81 added−10 removed

2 files+81 added−10 removed

modified:src/requests/adapters.py+74 added-10 removed
81 unmodified lines
82def _urllib3_request_context(83 request: "PreparedRequest",84 verify: "bool | str | None",85 client_cert: "typing.Tuple[str, str] | str | None",86 poolmanager: "PoolManager",87) -> "(typing.Dict[str, typing.Any], typing.Dict[str, typing.Any])":88 host_params = {}89 pool_kwargs = {}90 parsed_request_url = urlparse(request.url)91 scheme = parsed_request_url.scheme.lower()92 port = parsed_request_url.port93 poolmanager_kwargs = getattr(poolmanager, "connection_pool_kw", {})94 has_poolmanager_ssl_context = poolmanager_kwargs.get("ssl_context")9596 cert_reqs = "CERT_REQUIRED"97 if verify is False:98 cert_reqs = "CERT_NONE"95 elif verify is True:99 elif verify is True and not has_poolmanager_ssl_context:100 pool_kwargs["ssl_context"] = _preloaded_ssl_context101 elif isinstance(verify, str):102 if not os.path.isdir(verify):103 pool_kwargs["ca_certs"] = verify
274 unmodified lines
378 response.connection = self379380 return response381382 def build_connection_pool_key_attributes(self, request, verify, cert=None):383 """Build the PoolKey attributes used by urllib3 to return a connection.384385 This looks at the PreparedRequest, the user-specified verify value,386 and the value of the cert parameter to determine what PoolKey values387 to use to select a connection from a given urllib3 Connection Pool.388389 The SSL related pool key arguments are not consistently set. As of390 this writing, use the following to determine what keys may be in that391 dictionary:392393 * If ``verify`` is ``True``, ``"ssl_context"`` will be set and will be the394 default Requests SSL Context395 * If ``verify`` is ``False``, ``"ssl_context"`` will not be set but396 ``"cert_reqs"`` will be set397 * If ``verify`` is a string, (i.e., it is a user-specified trust bundle)398 ``"ca_certs"`` will be set if the string is not a directory recognized399 by :py:func:`os.path.isdir`, otherwise ``"ca_certs_dir"`` will be400 set.401 * If ``"cert"`` is specified, ``"cert_file"`` will always be set. If402 ``"cert"`` is a tuple with a second item, ``"key_file"`` will also403 be present404405 To override these settings, one may subclass this class, call this406 method and use the above logic to change parameters as desired. For407 example, if one wishes to use a custom :py:class:`ssl.SSLContext` one408 must both set ``"ssl_context"`` and based on what else they require,409 alter the other keys to ensure the desired behaviour.410411 :param request:412 The PreparedReqest being sent over the connection.413 :type request:414 :class:`~requests.models.PreparedRequest`415 :param verify:416 Either a boolean, in which case it controls whether417 we verify the server's TLS certificate, or a string, in which case it418 must be a path to a CA bundle to use.419 :param cert:420 (optional) Any user-provided SSL certificate for client421 authentication (a.k.a., mTLS). This may be a string (i.e., just422 the path to a file which holds both certificate and key) or a423 tuple of length 2 with the certificate file path and key file424 path.425 :returns:426 A tuple of two dictionaries. The first is the "host parameters"427 portion of the Pool Key including scheme, hostname, and port. The428 second is a dictionary of SSLContext related parameters.429 """430 return _urllib3_request_context(request, verify, cert, self.poolmanager)431432 def get_connection_with_tls_context(self, request, verify, proxies=None, cert=None):433 """Returns a urllib3 connection for the given request and TLS settings.434 This should not be called from user code, and is only exposed for use435 when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.436383 :param request: The :class:`PreparedRequest <PreparedRequest>` object384 to be sent over the connection.385 :param verify: Either a boolean, in which case it controls whether386 we verify the server's TLS certificate, or a string, in which case it387 must be a path to a CA bundle to use.388 :param proxies: (optional) The proxies dictionary to apply to the request.389 :param cert: (optional) Any user-provided SSL certificate to be trusted.390 :rtype: urllib3.ConnectionPool437 :param request:438 The :class:`PreparedRequest <PreparedRequest>` object to be sent439 over the connection.440 :param verify:441 Either a boolean, in which case it controls whether we verify the442 server's TLS certificate, or a string, in which case it must be a443 path to a CA bundle to use.444 :param proxies:445 (optional) The proxies dictionary to apply to the request.446 :param cert:447 (optional) Any user-provided SSL certificate to be used for client448 authentication (a.k.a., mTLS).449 :rtype:450 urllib3.ConnectionPool451 """452 proxy = select_proxy(request.url, proxies)453 try:394 host_params, pool_kwargs = _urllib3_request_context(request, verify, cert)454 host_params, pool_kwargs = self.build_connection_pool_key_attributes(455 request,456 verify,457 cert,458 )459 except ValueError as e:460 raise InvalidURL(e, request=request)461 if proxy:462 proxy = prepend_scheme_if_needed(proxy, "http")
243 unmodified lines
modified:HISTORY.md+7 added
4 unmodified lines
5---67- \[Short description of non-trivial change.\]892.32.3 (2024-05-24)10-------------------1112**Bugfixes**13- Fix bug breaking the ability to specify custom SSLContexts in sub-classes of14 HTTPAdapter. (#6716)15162.32.2 (2024-05-21)17-------------------1819**Deprecations**
1961 unmodified lines