Skip to content
Myr

Tolerate bad registry entries in Windows proxy settings (#6149)

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

authored by Nate Prewitt · committed by GitHub · parent a5e7169 · Browse files · 2 files+34 added−1 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+34 added−1 removed

  • tests:+33 added−0 removed
  • other:+1 added−1 removed

2 files+34 added−1 removed

modified:requests/utils.py+1 added-1 removed
81 unmodified lines
82 # ProxyEnable could be REG_SZ or REG_DWORD, normalizing it83 proxyEnable = int(winreg.QueryValueEx(internetSettings, "ProxyEnable")[0])84 # ProxyOverride is almost always a string85 proxyOverride = winreg.QueryValueEx(internetSettings, "ProxyOverride")[0]86 except OSError:86 except (OSError, ValueError):87 return False88 if not proxyEnable or not proxyOverride:89 return False90
999 unmodified lines
modified:tests/test_utils.py+33 added
862 unmodified lines
863 monkeypatch.setattr(winreg, "QueryValueEx", QueryValueEx)864 assert should_bypass_proxies(url, None) == expected865866867@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows")868def test_should_bypass_proxies_win_registry_bad_values(monkeypatch):869 """Tests for function should_bypass_proxies to check if proxy870 can be bypassed or not with Windows invalid registry settings.871 """872 import winreg873874 class RegHandle:875 def Close(self):876 pass877878 ie_settings = RegHandle()879880 def OpenKey(key, subkey):881 return ie_settings882883 def QueryValueEx(key, value_name):884 if key is ie_settings:885 if value_name == "ProxyEnable":886 # Invalid response; Should be an int or int-y value887 return [""]888 elif value_name == "ProxyOverride":889 return ["192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1"]890891 monkeypatch.setenv("http_proxy", "")892 monkeypatch.setenv("https_proxy", "")893 monkeypatch.setenv("no_proxy", "")894 monkeypatch.setenv("NO_PROXY", "")895 monkeypatch.setattr(winreg, "OpenKey", OpenKey)896 monkeypatch.setattr(winreg, "QueryValueEx", QueryValueEx)897 assert should_bypass_proxies("http://172.16.1.1/", None) is False898899900@pytest.mark.parametrize(901 "env_name, value",902 (903 ("no_proxy", "192.168.0.0/24,127.0.0.1,localhost.localdomain"),
22 unmodified lines