Skip to content

Commit

Permalink
Tolerate bad registry entries in Windows proxy settings (#6149)
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt authored Jun 1, 2022
1 parent a5e7169 commit 210095f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def proxy_bypass_registry(host):
proxyEnable = int(winreg.QueryValueEx(internetSettings, "ProxyEnable")[0])
# ProxyOverride is almost always a string
proxyOverride = winreg.QueryValueEx(internetSettings, "ProxyOverride")[0]
except OSError:
except (OSError, ValueError):
return False
if not proxyEnable or not proxyOverride:
return False
Expand Down
33 changes: 33 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,39 @@ def QueryValueEx(key, value_name):
assert should_bypass_proxies(url, None) == expected


@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows")
def test_should_bypass_proxies_win_registry_bad_values(monkeypatch):
"""Tests for function should_bypass_proxies to check if proxy
can be bypassed or not with Windows invalid registry settings.
"""
import winreg

class RegHandle:
def Close(self):
pass

ie_settings = RegHandle()

def OpenKey(key, subkey):
return ie_settings

def QueryValueEx(key, value_name):
if key is ie_settings:
if value_name == "ProxyEnable":
# Invalid response; Should be an int or int-y value
return [""]
elif value_name == "ProxyOverride":
return ["192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1"]

monkeypatch.setenv("http_proxy", "")
monkeypatch.setenv("https_proxy", "")
monkeypatch.setenv("no_proxy", "")
monkeypatch.setenv("NO_PROXY", "")
monkeypatch.setattr(winreg, "OpenKey", OpenKey)
monkeypatch.setattr(winreg, "QueryValueEx", QueryValueEx)
assert should_bypass_proxies("http://172.16.1.1/", None) is False


@pytest.mark.parametrize(
"env_name, value",
(
Expand Down

0 comments on commit 210095f

Please sign in to comment.