Skip to content

Commit 8dd3b26

Browse files
authored
Merge pull request #6302 from flysee/main
Fix the proxy_bypass_registry function all returning true in some cases.
2 parents eeafb6a + 13d892b commit 8dd3b26

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/requests/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ def proxy_bypass_registry(host):
9797
# '<local>' string by the localhost entry and the corresponding
9898
# canonical entry.
9999
proxyOverride = proxyOverride.split(";")
100+
# filter out empty strings to avoid re.match return true in the following code.
101+
proxyOverride = filter(None, proxyOverride)
100102
# now check if we match one of the registry values.
101103
for test in proxyOverride:
102104
if test == "<local>":

tests/test_utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,3 +924,35 @@ def test_set_environ_raises_exception():
924924
raise Exception("Expected exception")
925925

926926
assert "Expected exception" in str(exception.value)
927+
928+
929+
@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows")
930+
def test_should_bypass_proxies_win_registry_ProxyOverride_value(monkeypatch):
931+
"""Tests for function should_bypass_proxies to check if proxy
932+
can be bypassed or not with Windows ProxyOverride registry value ending with a semicolon.
933+
"""
934+
import winreg
935+
936+
class RegHandle:
937+
def Close(self):
938+
pass
939+
940+
ie_settings = RegHandle()
941+
942+
def OpenKey(key, subkey):
943+
return ie_settings
944+
945+
def QueryValueEx(key, value_name):
946+
if key is ie_settings:
947+
if value_name == "ProxyEnable":
948+
return [1]
949+
elif value_name == "ProxyOverride":
950+
return [
951+
"192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1;<-loopback>;"
952+
]
953+
954+
monkeypatch.setenv("NO_PROXY", "")
955+
monkeypatch.setenv("no_proxy", "")
956+
monkeypatch.setattr(winreg, "OpenKey", OpenKey)
957+
monkeypatch.setattr(winreg, "QueryValueEx", QueryValueEx)
958+
assert should_bypass_proxies("http://example.com/", None) is False

0 commit comments

Comments
 (0)