Skip to content
Myr

Add new test to check netrc auth leak (#6962)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by danigm · committed by GitHub · parent 96ba401 · Browse files · 1 file+31 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+31 added−0 removed

  • tests:+31 added−0 removed

1 file+31 added−0 removed

modified:tests/test_requests.py+31 added
5 unmodified lines
6import json7import os8import pickle9import re10import tempfile11import threading12import warnings13from unittest import mock14
689 unmodified lines
704 assert r.status_code == 401705 finally:706 requests.sessions.get_netrc_auth = old_auth707708 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}"713714 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.name724725 old_netrc = os.environ.get("NETRC", "")726 os.environ["NETRC"] = netrc_file727728 try:729 # Should use netrc730 # Make sure that we don't use the example.com credentails731 # for the request732 r = requests.get(url)733 assert r.status_code == 200734 finally:735 os.environ["NETRC"] = old_netrc736 os.unlink(netrc_file)737738 def test_DIGEST_HTTP_200_OK_GET(self, httpbin):739 for authtype in self.digest_auth_algo:740 auth = HTTPDigestAuth("user", "pass")741 url = httpbin("digest-auth", "auth", "user", "pass", authtype, "never")
2299 unmodified lines