Skip to content

Commit 4791422

Browse files
authored
Fix empty netrc entry usage (#7205)
1 parent 1b40fdd commit 4791422

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/requests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def get_netrc_auth(url, raise_errors=False):
231231

232232
try:
233233
_netrc = netrc(netrc_path).authenticators(host)
234-
if _netrc:
234+
if _netrc and any(_netrc):
235235
# Return with login / password
236236
login_i = 0 if _netrc[0] else 1
237237
return (_netrc[login_i], _netrc[2])

tests/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ def test_not_vulnerable_to_bad_url_parsing(self, tmp_path, monkeypatch):
170170
auth = get_netrc_auth("http://example.com:@evil.com/'")
171171
assert auth is None
172172

173+
def test_empty_default_credentials_ignored(self, tmp_path, monkeypatch):
174+
"""Empty default credentials should not be returned."""
175+
netrc_path = tmp_path / ".netrc"
176+
monkeypatch.setenv("NETRC", str(netrc_path))
177+
with open(netrc_path, "w") as f:
178+
f.write("machine example.com login user password pass\ndefault\n")
179+
180+
auth = get_netrc_auth("http://httpbin.org/")
181+
assert auth is None
182+
173183

174184
class TestToKeyValList:
175185
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)