|
| 1 | +From 645c871ad0578ac7fc79ea84e64380984423af0c Mon Sep 17 00:00:00 2001 |
| 2 | +From: Kanishk-Bansal < [email protected]> |
| 3 | +Date: Thu, 19 Dec 2024 06:10:36 +0000 |
| 4 | +Subject: [PATCH] Fix CVE-2024-35195 |
| 5 | + |
| 6 | +--- |
| 7 | + .../site-packages/requests/adapters.py | 58 ++++++++++++++++++- |
| 8 | + 1 file changed, 57 insertions(+), 1 deletion(-) |
| 9 | + |
| 10 | +diff --git a/pypi_requests/site-packages/requests/adapters.py b/pypi_requests/site-packages/requests/adapters.py |
| 11 | +index 78e3bb6..03cd069 100644 |
| 12 | +--- a/pypi_requests/site-packages/requests/adapters.py |
| 13 | ++++ b/pypi_requests/site-packages/requests/adapters.py |
| 14 | +@@ -8,6 +8,7 @@ and maintain connections. |
| 15 | + |
| 16 | + import os.path |
| 17 | + import socket # noqa: F401 |
| 18 | ++import typing |
| 19 | + |
| 20 | + from urllib3.exceptions import ClosedPoolError, ConnectTimeoutError |
| 21 | + from urllib3.exceptions import HTTPError as _HTTPError |
| 22 | +@@ -61,12 +62,38 @@ except ImportError: |
| 23 | + raise InvalidSchema("Missing dependencies for SOCKS support.") |
| 24 | + |
| 25 | + |
| 26 | ++if typing.TYPE_CHECKING: |
| 27 | ++ from .models import PreparedRequest |
| 28 | ++ |
| 29 | ++ |
| 30 | + DEFAULT_POOLBLOCK = False |
| 31 | + DEFAULT_POOLSIZE = 10 |
| 32 | + DEFAULT_RETRIES = 0 |
| 33 | + DEFAULT_POOL_TIMEOUT = None |
| 34 | + |
| 35 | + |
| 36 | ++def _urllib3_request_context( |
| 37 | ++ request: "PreparedRequest", verify: "bool | str | None" |
| 38 | ++) -> "(typing.Dict[str, typing.Any], typing.Dict[str, typing.Any])": |
| 39 | ++ host_params = {} |
| 40 | ++ pool_kwargs = {} |
| 41 | ++ parsed_request_url = urlparse(request.url) |
| 42 | ++ scheme = parsed_request_url.scheme.lower() |
| 43 | ++ port = parsed_request_url.port |
| 44 | ++ cert_reqs = "CERT_REQUIRED" |
| 45 | ++ if verify is False: |
| 46 | ++ cert_reqs = "CERT_NONE" |
| 47 | ++ if isinstance(verify, str): |
| 48 | ++ pool_kwargs["ca_certs"] = verify |
| 49 | ++ pool_kwargs["cert_reqs"] = cert_reqs |
| 50 | ++ host_params = { |
| 51 | ++ "scheme": scheme, |
| 52 | ++ "host": parsed_request_url.hostname, |
| 53 | ++ "port": port, |
| 54 | ++ } |
| 55 | ++ return host_params, pool_kwargs |
| 56 | ++ |
| 57 | ++ |
| 58 | + class BaseAdapter: |
| 59 | + """The Base Transport Adapter""" |
| 60 | + |
| 61 | +@@ -328,6 +355,35 @@ class HTTPAdapter(BaseAdapter): |
| 62 | + |
| 63 | + return response |
| 64 | + |
| 65 | ++ def _get_connection(self, request, verify, proxies=None): |
| 66 | ++ # Replace the existing get_connection without breaking things and |
| 67 | ++ # ensure that TLS settings are considered when we interact with |
| 68 | ++ # urllib3 HTTP Pools |
| 69 | ++ proxy = select_proxy(request.url, proxies) |
| 70 | ++ try: |
| 71 | ++ host_params, pool_kwargs = _urllib3_request_context(request, verify) |
| 72 | ++ except ValueError as e: |
| 73 | ++ raise InvalidURL(e, request=request) |
| 74 | ++ if proxy: |
| 75 | ++ proxy = prepend_scheme_if_needed(proxy, "http") |
| 76 | ++ proxy_url = parse_url(proxy) |
| 77 | ++ if not proxy_url.host: |
| 78 | ++ raise InvalidProxyURL( |
| 79 | ++ "Please check proxy URL. It is malformed " |
| 80 | ++ "and could be missing the host." |
| 81 | ++ ) |
| 82 | ++ proxy_manager = self.proxy_manager_for(proxy) |
| 83 | ++ conn = proxy_manager.connection_from_host( |
| 84 | ++ **host_params, pool_kwargs=pool_kwargs |
| 85 | ++ ) |
| 86 | ++ else: |
| 87 | ++ # Only scheme should be lower case |
| 88 | ++ conn = self.poolmanager.connection_from_host( |
| 89 | ++ **host_params, pool_kwargs=pool_kwargs |
| 90 | ++ ) |
| 91 | ++ |
| 92 | ++ return conn |
| 93 | ++ |
| 94 | + def get_connection(self, url, proxies=None): |
| 95 | + """Returns a urllib3 connection for the given URL. This should not be |
| 96 | + called from user code, and is only exposed for use when subclassing the |
| 97 | +@@ -451,7 +507,7 @@ class HTTPAdapter(BaseAdapter): |
| 98 | + """ |
| 99 | + |
| 100 | + try: |
| 101 | +- conn = self.get_connection(request.url, proxies) |
| 102 | ++ conn = self._get_connection(request, verify, proxies) |
| 103 | + except LocationValueError as e: |
| 104 | + raise InvalidURL(e, request=request) |
| 105 | + |
| 106 | +-- |
| 107 | +2.45.2 |
| 108 | + |
0 commit comments