Skip to content

Commit

Permalink
Merge pull request #6302 from flysee/main
Browse files Browse the repository at this point in the history
Fix the proxy_bypass_registry function all returning true in some cases.
  • Loading branch information
sigmavirus24 authored Mar 18, 2024
2 parents eeafb6a + 13d892b commit 8dd3b26
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def proxy_bypass_registry(host):
# '<local>' string by the localhost entry and the corresponding
# canonical entry.
proxyOverride = proxyOverride.split(";")
# filter out empty strings to avoid re.match return true in the following code.
proxyOverride = filter(None, proxyOverride)
# now check if we match one of the registry values.
for test in proxyOverride:
if test == "<local>":
Expand Down
32 changes: 32 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,3 +924,35 @@ def test_set_environ_raises_exception():
raise Exception("Expected exception")

assert "Expected exception" in str(exception.value)


@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows")
def test_should_bypass_proxies_win_registry_ProxyOverride_value(monkeypatch):
"""Tests for function should_bypass_proxies to check if proxy
can be bypassed or not with Windows ProxyOverride registry value ending with a semicolon.
"""
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":
return [1]
elif value_name == "ProxyOverride":
return [
"192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1;<-loopback>;"
]

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

0 comments on commit 8dd3b26

Please sign in to comment.