Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tolerate bad registry entries in Windows proxy settings #6149

Merged
merged 1 commit into from Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion requests/utils.py
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
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