diff --git a/tests/test_proxy_environment.py b/tests/test_proxy_environment.py index ade7b35002..6a3e42d2a7 100644 --- a/tests/test_proxy_environment.py +++ b/tests/test_proxy_environment.py @@ -188,6 +188,24 @@ def test_all_proxy_set(self, mock_getproxies: Mock) -> None: # proxy manager and a plain pool manager self.assert_pool_managers(env, ["http://localhost:8888", None]) + @patch("tuf.ngclient._internal.proxy.getproxies") + def test_proxy_without_scheme(self, mock_getproxies: Mock) -> None: + # Proxy variables commonly omit the scheme, e.g. "proxy.example:3128": + # this should be treated as a http proxy, not raise + mock_getproxies.return_value = { + "https": "localhost:8888", + "all": "user:secret@localhost:9999", + } + + env = ProxyEnvironment() + env.get_pool_manager("https", "example.com") + env.get_pool_manager("http", "example.com") + + # https proxy variable and all_proxy fallback, both as http:// proxies + self.assert_pool_managers( + env, ["http://localhost:8888", "http://user:secret@localhost:9999"] + ) + @patch("tuf.ngclient._internal.proxy.getproxies") def test_all_proxy_and_no_proxy_set(self, mock_getproxies: Mock) -> None: mock_getproxies.return_value = { diff --git a/tuf/ngclient/_internal/proxy.py b/tuf/ngclient/_internal/proxy.py index f863428318..aef8acad55 100644 --- a/tuf/ngclient/_internal/proxy.py +++ b/tuf/ngclient/_internal/proxy.py @@ -12,6 +12,18 @@ from urllib3.util.url import parse_url +def _with_scheme(proxy_url: str) -> str: + """Return ``proxy_url`` with "http://" added if it has no scheme. + + Proxy variables often omit the scheme (e.g. ``https_proxy=proxy:3128``) + and curl and requests treat that as a http proxy, but urllib3 raises + ``ProxySchemeUnknown`` for it. + """ + if "://" in proxy_url: + return proxy_url + return f"http://{proxy_url}" + + # TODO: ProxyEnvironment could implement the whole PoolManager.RequestMethods # Mixin: We only need request() so nothing else is currently implemented class ProxyEnvironment: @@ -60,9 +72,9 @@ def _get_proxy(self, scheme: str | None, host: str | None) -> str | None: return None if scheme in self._proxies: - return self._proxies[scheme] + return _with_scheme(self._proxies[scheme]) if self._all_proxy is not None: - return self._all_proxy + return _with_scheme(self._all_proxy) return None