Skip to content

Commit 210095f

Browse files
authored
Tolerate bad registry entries in Windows proxy settings (#6149)
1 parent a5e7169 commit 210095f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

requests/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def proxy_bypass_registry(host):
8383
proxyEnable = int(winreg.QueryValueEx(internetSettings, "ProxyEnable")[0])
8484
# ProxyOverride is almost always a string
8585
proxyOverride = winreg.QueryValueEx(internetSettings, "ProxyOverride")[0]
86-
except OSError:
86+
except (OSError, ValueError):
8787
return False
8888
if not proxyEnable or not proxyOverride:
8989
return False

tests/test_utils.py

+33
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,39 @@ def QueryValueEx(key, value_name):
864864
assert should_bypass_proxies(url, None) == expected
865865

866866

867+
@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows")
868+
def test_should_bypass_proxies_win_registry_bad_values(monkeypatch):
869+
"""Tests for function should_bypass_proxies to check if proxy
870+
can be bypassed or not with Windows invalid registry settings.
871+
"""
872+
import winreg
873+
874+
class RegHandle:
875+
def Close(self):
876+
pass
877+
878+
ie_settings = RegHandle()
879+
880+
def OpenKey(key, subkey):
881+
return ie_settings
882+
883+
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 value
887+
return [""]
888+
elif value_name == "ProxyOverride":
889+
return ["192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1"]
890+
891+
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 False
898+
899+
867900
@pytest.mark.parametrize(
868901
"env_name, value",
869902
(

0 commit comments

Comments
 (0)