Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,9 +851,11 @@ def get_proxy(key: str) -> str | None:
host_with_port += f":{parsed.port}"

for host in no_proxy_hosts:
host = host.lstrip(".")
if hostname == host or host_with_port == host:
return True
host = "." + host
if hostname.endswith(host) or host_with_port.endswith(host):
# The URL does match something in no_proxy, so we don't want
# to apply the proxies on this URL.
return True

with set_environ("no_proxy", no_proxy_arg):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,29 @@ def test_should_bypass_proxies_no_proxy(url, expected, monkeypatch):
assert should_bypass_proxies(url, no_proxy=no_proxy) == expected


@pytest.mark.parametrize(
"url, expected",
(
("http://localhost/", True),
("http://anotherdomain.com:8888/", True),
("http://newdomain.com:1234/", True),
("http://www.newdomain.com:1234/", True),
("http://foo.d.o.t/", True),
("http://d.o.t/", True),
("http://prelocalhost/", False),
("http://newdomain.com/", False),
("http://newdomain.com:1235/", False),
),
)
def test_should_bypass_proxies_no_proxy_domain_boundary(url, expected):
"""Ensure no_proxy matching respects domain boundaries and does not
greedily match domains that merely endswith the no_proxy entry.
See CPython bpo-39057.
"""
no_proxy = "localhost, anotherdomain.com, newdomain.com:1234, .d.o.t"
assert should_bypass_proxies(url, no_proxy=no_proxy) == expected


@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows")
@pytest.mark.parametrize(
"url, expected, override",
Expand Down
Loading