Skip to content

Commit 5cdd0f0

Browse files
rajorinorsenthil
andcommitted
[3.11] gh-158907: Do not send proxy credentials to a direct origin on 407 (#158956)
ProxyBasicAuthHandler and ProxyDigestAuthHandler looked up passwords with the scheme-less origin host, so an HTTP 407 could match credentials stored for HTTPS and retry in cleartext. --------- Co-authored-by: Senthil Kumaran <senthil@python.org> (cherry picked from commit ab48ba8)
1 parent e34c7bb commit 5cdd0f0

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎Lib/test/test_urllib2.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,37 @@ def test_proxy_basic_auth(self):
16241624
"proxy.example.com:3128",
16251625
)
16261626

1627+
def test_proxy_auth_ignores_direct_407(self):
1628+
# gh-158907: a direct HTTP origin that returns 407 must not receive
1629+
# credentials registered for the same authority over HTTPS.
1630+
realm = "test-realm"
1631+
cases = [
1632+
(urllib.request.ProxyBasicAuthHandler,
1633+
'Basic realm="%s"' % realm),
1634+
(urllib.request.ProxyDigestAuthHandler,
1635+
'Digest realm="%s", nonce="abc", qop="auth"' % realm),
1636+
]
1637+
for handler_class, challenge in cases:
1638+
with self.subTest(handler=handler_class.__name__):
1639+
opener = OpenerDirector()
1640+
opener.add_handler(urllib.request.ProxyHandler({}))
1641+
password_manager = urllib.request.HTTPPasswordMgr()
1642+
auth_handler = handler_class(password_manager)
1643+
http_handler = MockHTTPHandlerRedirect(
1644+
407, 'Proxy-Authenticate: %s\r\n\r\n' % challenge)
1645+
opener.add_handler(auth_handler)
1646+
opener.add_handler(http_handler)
1647+
1648+
password_manager.add_password(
1649+
realm, "https://example.com/",
1650+
"victim-user", "victim-secret")
1651+
opener.open("http://example.com/resource")
1652+
1653+
self.assertEqual(len(http_handler.requests), 1)
1654+
self.assertFalse(
1655+
http_handler.requests[0].has_header(
1656+
"Proxy-authorization"))
1657+
16271658
def test_basic_and_digest_auth_handlers(self):
16281659
# HTTPDigestAuthHandler raised an exception if it couldn't handle a 40*
16291660
# response (http://python.org/sf/1479302), where it should instead

‎Lib/urllib/request.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,9 @@ class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler):
10761076
auth_header = 'Proxy-authorization'
10771077

10781078
def http_error_407(self, req, fp, code, msg, headers):
1079+
# gh-158907: a 407 from a direct origin is not a proxy challenge
1080+
if not req.has_proxy() and not req._tunnel_host:
1081+
return None
10791082
# http_error_auth_reqed requires that there is no userinfo component in
10801083
# authority. Assume there isn't one, since urllib.request does not (and
10811084
# should not, RFC 3986 s. 3.2.1) support requests for URLs containing
@@ -1260,6 +1263,9 @@ class ProxyDigestAuthHandler(BaseHandler, AbstractDigestAuthHandler):
12601263
handler_order = 490 # before Basic auth
12611264

12621265
def http_error_407(self, req, fp, code, msg, headers):
1266+
# gh-158907: a 407 from a direct origin is not a proxy challenge
1267+
if not req.has_proxy() and not req._tunnel_host:
1268+
return None
12631269
host = req.host
12641270
retry = self.http_error_auth_reqed('proxy-authenticate',
12651271
host, req, headers)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:class:`~urllib.request.ProxyBasicAuthHandler` and
2+
:class:`~urllib.request.ProxyDigestAuthHandler` no longer send credentials
3+
in response to ``407`` from a direct origin. A scheme-less lookup against
4+
the origin host could match passwords stored for an HTTPS URL and retry
5+
the cleartext request with ``Proxy-Authorization``.

0 commit comments

Comments
 (0)