Skip to content

Commit f72c4d1

Browse files
committed
test: Add new test to check netrc auth leak
This patch adds a new test that reproduces the security issue reported here: https://seclists.org/oss-sec/2025/q2/204 Doing a request to a malicious url with a prefix like "domain.com:@" will use the "domain.com" netrc credentials in the request to other domain.
1 parent 7341690 commit f72c4d1

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

tests/test_requests.py

Lines changed: 30 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,35 @@ 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 = tempfile.NamedTemporaryFile()
715+
netrc_file.write(b"machine example.com\n")
716+
netrc_file.write(b"login wronguser\n")
717+
netrc_file.write(b"password wrongpass\n")
718+
netrc_file.write(f"machine {domain}\n".encode("utf8"))
719+
netrc_file.write(b"login user\n")
720+
netrc_file.write(b"password pass\n")
721+
netrc_file.seek(0)
722+
netrc_file.read()
723+
724+
old_netrc = os.environ.get("NETRC", "")
725+
os.environ["NETRC"] = netrc_file.name
726+
727+
try:
728+
# Should use netrc
729+
# Make sure that we don't use the example.com credentails
730+
# for the request
731+
r = requests.get(url)
732+
assert r.status_code == 200
733+
finally:
734+
os.environ["NETRC"] = old_netrc
735+
netrc_file.close()
736+
707737
def test_DIGEST_HTTP_200_OK_GET(self, httpbin):
708738
for authtype in self.digest_auth_algo:
709739
auth = HTTPDigestAuth("user", "pass")

0 commit comments

Comments
 (0)