Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 1 addition & 9 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,8 @@ def get_netrc_auth(url, raise_errors=False):
return

ri = urlparse(url)

# Strip port numbers from netloc. This weird `if...encode`` dance is
# used for Python 3.2, which doesn't support unicode literals.
splitstr = b":"
if isinstance(url, str):
splitstr = splitstr.decode("ascii")
host = ri.netloc.split(splitstr)[0]

try:
_netrc = netrc(netrc_path).authenticators(host)
_netrc = netrc(netrc_path).authenticators(ri.hostname)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fixed now in main: 96ba401

if _netrc:
# Return with login / password
login_i = 0 if _netrc[0] else 1
Expand Down
19 changes: 19 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
get_encoding_from_headers,
get_encodings_from_content,
get_environ_proxies,
get_netrc_auth,
guess_filename,
guess_json_utf,
is_ipv4_address,
Expand Down Expand Up @@ -152,6 +153,24 @@ def test_super_len_with_no_matches(self):
assert super_len(object()) == 0


class TestGetNetrcAuth:
def test_works(self, tmp_path, monkeypatch):
netrc_path = tmp_path / ".netrc"
monkeypatch.setenv("NETRC", str(netrc_path))
with open(netrc_path, "w") as f:
f.write("machine example.com login aaaa password bbbb\n")
auth = get_netrc_auth("http://example.com/thing")
assert auth == ("aaaa", "bbbb")

def test_not_vulnerable_to_bad_url_parsing(self, tmp_path, monkeypatch):
netrc_path = tmp_path / ".netrc"
monkeypatch.setenv("NETRC", str(netrc_path))
with open(netrc_path, "w") as f:
f.write("machine example.com login aaaa password bbbb\n")
auth = get_netrc_auth("http://example.com:@evil.com/'")
assert auth is None


class TestToKeyValList:
@pytest.mark.parametrize(
"value, expected",
Expand Down