|  | 
| 7 | 7 | import os | 
| 8 | 8 | import pickle | 
| 9 | 9 | import re | 
|  | 10 | +import tempfile | 
| 10 | 11 | import threading | 
| 11 | 12 | import warnings | 
| 12 | 13 | from unittest import mock | 
| @@ -704,6 +705,36 @@ def get_netrc_auth_mock(url): | 
| 704 | 705 |         finally: | 
| 705 | 706 |             requests.sessions.get_netrc_auth = old_auth | 
| 706 | 707 | 
 | 
|  | 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 | + | 
| 707 | 738 |     def test_DIGEST_HTTP_200_OK_GET(self, httpbin): | 
| 708 | 739 |         for authtype in self.digest_auth_algo: | 
| 709 | 740 |             auth = HTTPDigestAuth("user", "pass") | 
|  | 
0 commit comments