Skip to content

Commit 92bab7d

Browse files
committed
Merge 'origin/main' into aw/fix-CVE-2024-47081
2 parents db5e9b8 + 7bc4587 commit 92bab7d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/requests/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@ def get_netrc_auth(url, raise_errors=False):
229229
return
230230

231231
ri = urlparse(url)
232+
host = ri.hostname
233+
232234
try:
233-
_netrc = netrc(netrc_path).authenticators(ri.hostname)
235+
_netrc = netrc(netrc_path).authenticators(host)
234236
if _netrc:
235237
# Return with login / password
236238
login_i = 0 if _netrc[0] else 1

tests/test_requests.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import pickle
99
import re
10+
import tempfile
1011
import threading
1112
import warnings
1213
from unittest import mock
@@ -704,6 +705,36 @@ def get_netrc_auth_mock(url):
704705
finally:
705706
requests.sessions.get_netrc_auth = old_auth
706707

708+
def test_basicauth_with_netrc_leak(self, httpbin):
709+
url1 = httpbin("basic-auth", "user", "pass")
710+
url = url1[len("http://") :]
711+
domain = url.split(":")[0]
712+
url = f"http://example.com:@{url}"
713+
714+
netrc_file = ""
715+
with tempfile.NamedTemporaryFile(mode="w", delete=False) as fp:
716+
fp.write("machine example.com\n")
717+
fp.write("login wronguser\n")
718+
fp.write("password wrongpass\n")
719+
fp.write(f"machine {domain}\n")
720+
fp.write("login user\n")
721+
fp.write("password pass\n")
722+
fp.close()
723+
netrc_file = fp.name
724+
725+
old_netrc = os.environ.get("NETRC", "")
726+
os.environ["NETRC"] = netrc_file
727+
728+
try:
729+
# Should use netrc
730+
# Make sure that we don't use the example.com credentails
731+
# for the request
732+
r = requests.get(url)
733+
assert r.status_code == 200
734+
finally:
735+
os.environ["NETRC"] = old_netrc
736+
os.unlink(netrc_file)
737+
707738
def test_DIGEST_HTTP_200_OK_GET(self, httpbin):
708739
for authtype in self.digest_auth_algo:
709740
auth = HTTPDigestAuth("user", "pass")

0 commit comments

Comments
 (0)