Skip to content

Commit b92dbe9

Browse files
miss-islingtonrajorinorsenthil
authored
[3.14] gh-158907: Do not send proxy credentials to a direct origin on 407 (GH-158956) (#161920)
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 5885d6d commit b92dbe9

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
@@ -1846,6 +1846,37 @@ def test_digest_auth_empty_path(self):
18461846
self.assertIn('uri="/"', auth)
18471847
self.assertIn('response="%s"' % respdig, auth)
18481848

1849+
def test_proxy_auth_ignores_direct_407(self):
1850+
# gh-158907: a direct HTTP origin that returns 407 must not receive
1851+
# credentials registered for the same authority over HTTPS.
1852+
realm = "test-realm"
1853+
cases = [
1854+
(urllib.request.ProxyBasicAuthHandler,
1855+
'Basic realm="%s"' % realm),
1856+
(urllib.request.ProxyDigestAuthHandler,
1857+
'Digest realm="%s", nonce="abc", qop="auth"' % realm),
1858+
]
1859+
for handler_class, challenge in cases:
1860+
with self.subTest(handler=handler_class.__name__):
1861+
opener = OpenerDirector()
1862+
opener.add_handler(urllib.request.ProxyHandler({}))
1863+
password_manager = urllib.request.HTTPPasswordMgr()
1864+
auth_handler = handler_class(password_manager)
1865+
http_handler = MockHTTPHandlerRedirect(
1866+
407, 'Proxy-Authenticate: %s\r\n\r\n' % challenge)
1867+
opener.add_handler(auth_handler)
1868+
opener.add_handler(http_handler)
1869+
1870+
password_manager.add_password(
1871+
realm, "https://example.com/",
1872+
"victim-user", "victim-secret")
1873+
opener.open("http://example.com/resource")
1874+
1875+
self.assertEqual(len(http_handler.requests), 1)
1876+
self.assertFalse(
1877+
http_handler.requests[0].has_header(
1878+
"Proxy-authorization"))
1879+
18491880
def test_basic_and_digest_auth_handlers(self):
18501881
# HTTPDigestAuthHandler raised an exception if it couldn't handle a 40*
18511882
# 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
@@ -1046,6 +1046,9 @@ class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler):
10461046
auth_header = 'Proxy-authorization'
10471047

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

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