Skip to content

Commit a4f9a59

Browse files
authored
Port bpo-39057 to Requests (#7427)
1 parent 3816cfa commit a4f9a59

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/requests/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,11 @@ def get_proxy(key: str) -> str | None:
851851
host_with_port += f":{parsed.port}"
852852

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

859861
with set_environ("no_proxy", no_proxy_arg):

tests/test_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,29 @@ def test_should_bypass_proxies_no_proxy(url, expected, monkeypatch):
844844
assert should_bypass_proxies(url, no_proxy=no_proxy) == expected
845845

846846

847+
@pytest.mark.parametrize(
848+
"url, expected",
849+
(
850+
("http://localhost/", True),
851+
("http://anotherdomain.com:8888/", True),
852+
("http://newdomain.com:1234/", True),
853+
("http://www.newdomain.com:1234/", True),
854+
("http://foo.d.o.t/", True),
855+
("http://d.o.t/", True),
856+
("http://prelocalhost/", False),
857+
("http://newdomain.com/", False),
858+
("http://newdomain.com:1235/", False),
859+
),
860+
)
861+
def test_should_bypass_proxies_no_proxy_domain_boundary(url, expected):
862+
"""Ensure no_proxy matching respects domain boundaries and does not
863+
greedily match domains that merely endswith the no_proxy entry.
864+
See CPython bpo-39057.
865+
"""
866+
no_proxy = "localhost, anotherdomain.com, newdomain.com:1234, .d.o.t"
867+
assert should_bypass_proxies(url, no_proxy=no_proxy) == expected
868+
869+
847870
@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows")
848871
@pytest.mark.parametrize(
849872
"url, expected, override",

0 commit comments

Comments
 (0)