Skip to content

Commit 849d0ce

Browse files
miss-islingtonrajorinorsenthil
authored
[3.15] gh-158907: Do not send proxy credentials to a direct origin on 407 (GH-158956) (#161919)
gh-158907: Do not send proxy credentials to a direct origin on 407 (GH-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. --------- (cherry picked from commit ab48ba8) Co-authored-by: rajorin <167426833+rajorin@users.noreply.github.com> Co-authored-by: Senthil Kumaran <senthil@python.org>
1 parent 19177c6 commit 849d0ce

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
@@ -1863,6 +1863,37 @@ def test_digest_auth_empty_path(self):
18631863
self.assertIn('uri="/"', auth)
18641864
self.assertIn('response="%s"' % respdig, auth)
18651865

1866+
def test_proxy_auth_ignores_direct_407(self):
1867+
# gh-158907: a direct HTTP origin that returns 407 must not receive
1868+
# credentials registered for the same authority over HTTPS.
1869+
realm = "test-realm"
1870+
cases = [
1871+
(urllib.request.ProxyBasicAuthHandler,
1872+
'Basic realm="%s"' % realm),
1873+
(urllib.request.ProxyDigestAuthHandler,
1874+
'Digest realm="%s", nonce="abc", qop="auth"' % realm),
1875+
]
1876+
for handler_class, challenge in cases:
1877+
with self.subTest(handler=handler_class.__name__):
1878+
opener = OpenerDirector()
1879+
opener.add_handler(urllib.request.ProxyHandler({}))
1880+
password_manager = urllib.request.HTTPPasswordMgr()
1881+
auth_handler = handler_class(password_manager)
1882+
http_handler = MockHTTPHandlerRedirect(
1883+
407, 'Proxy-Authenticate: %s\r\n\r\n' % challenge)
1884+
opener.add_handler(auth_handler)
1885+
opener.add_handler(http_handler)
1886+
1887+
password_manager.add_password(
1888+
realm, "https://example.com/",
1889+
"victim-user", "victim-secret")
1890+
opener.open("http://example.com/resource")
1891+
1892+
self.assertEqual(len(http_handler.requests), 1)
1893+
self.assertFalse(
1894+
http_handler.requests[0].has_header(
1895+
"Proxy-authorization"))
1896+
18661897
def test_basic_and_digest_auth_handlers(self):
18671898
# HTTPDigestAuthHandler raised an exception if it couldn't handle a 40*
18681899
# response (https://bugs.python.org/issue1479302), where it should instead

‎Lib/urllib/request.py‎

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

10501050
def http_error_407(self, req, fp, code, msg, headers):
1051+
# gh-158907: a 407 from a direct origin is not a proxy challenge
1052+
if not req.has_proxy() and not req._tunnel_host:
1053+
return None
10511054
# http_error_auth_reqed requires that there is no userinfo component in
10521055
# authority. Assume there isn't one, since urllib.request does not (and
10531056
# should not, RFC 3986 s. 3.2.1) support requests for URLs containing
@@ -1307,6 +1310,9 @@ class ProxyDigestAuthHandler(BaseHandler, AbstractDigestAuthHandler):
13071310
handler_order = 490 # before Basic auth
13081311

13091312
def http_error_407(self, req, fp, code, msg, headers):
1313+
# gh-158907: a 407 from a direct origin is not a proxy challenge
1314+
if not req.has_proxy() and not req._tunnel_host:
1315+
return None
13101316
host = req.host
13111317
retry = self.http_error_auth_reqed('proxy-authenticate',
13121318
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)