Skip to content

Commit 7bc4587

Browse files
authored
Add new test to check netrc auth leak (#6962)
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 96ba401 commit 7bc4587

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

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)