Skip to content

Commit a0d023f

Browse files
miss-islingtonlkk7
andauthored
[3.14] gh-155694: Scope HTTPPasswordMgr credentials by URL scheme (GH-155696) (#155969)
gh-155694: Scope HTTPPasswordMgr credentials by URL scheme (GH-155696) Credentials stored for an https:// URI were also matched against the corresponding http:// URI, since `reduce_uri()` discards the scheme. `HTTPPasswordMgr` and `HTTPPasswordMgrWithPriorAuth` now compare the scheme too; URIs registered without a scheme still match any scheme. (cherry picked from commit a7bb524) Co-authored-by: Łukasz <lukaszlapinski7@gmail.com>
1 parent a59c5bb commit a0d023f

4 files changed

Lines changed: 87 additions & 8 deletions

File tree

Doc/library/urllib.request.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,14 @@ These methods are available on :class:`HTTPPasswordMgr` and
991991

992992
*uri* can be either a single URI, or a sequence of URIs. *realm*, *user* and
993993
*passwd* must be strings. This causes ``(user, passwd)`` to be used as
994-
authentication tokens when authentication for *realm* and a super-URI of any of
995-
the given URIs is given.
994+
authentication tokens when authentication for *realm* and a super-URI of any
995+
of the given URIs is given. If a URI includes a scheme, its credentials only
996+
match authentication URIs with the same scheme or no scheme. A URI without a
997+
scheme matches authentication URIs with any scheme.
998+
999+
.. versionchanged:: next
1000+
Authentication credentials for URIs with a scheme are now scoped by
1001+
that scheme.
9961002

9971003

9981004
.. method:: HTTPPasswordMgr.find_user_password(realm, authuri)

Lib/test/test_urllib2.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,50 @@ def test_password_manager_default_port(self):
270270
self.assertEqual(find_user_pass("i", "http://j.example.com:80"),
271271
(None, None))
272272

273+
def test_password_manager_scheme(self):
274+
mgr = urllib.request.HTTPPasswordMgr()
275+
mgr.add_password(
276+
"realm", "https://example.com/", "user", "password")
277+
278+
self.assertEqual(
279+
mgr.find_user_password("realm", "https://example.com/"),
280+
("user", "password"))
281+
self.assertEqual(
282+
mgr.find_user_password("realm", "http://example.com/"),
283+
(None, None))
284+
# Support an authority without a scheme.
285+
self.assertEqual(
286+
mgr.find_user_password("realm", "example.com"),
287+
("user", "password"))
288+
# An authority without a scheme continues to match any scheme.
289+
mgr.add_password(
290+
"realm", "schemeless.example.com", "user", "password")
291+
for scheme in "http", "https":
292+
with self.subTest(scheme=scheme):
293+
self.assertEqual(
294+
mgr.find_user_password(
295+
"realm", f"{scheme}://schemeless.example.com/"),
296+
("user", "password"))
297+
298+
# A network-path reference also has no scheme.
299+
mgr.add_password(
300+
"realm", "//network-path.example.com/", "user", "password")
301+
self.assertEqual(
302+
mgr.find_user_password(
303+
"realm", "https://network-path.example.com/"),
304+
("user", "password"))
305+
306+
def test_password_manager_reduced_uri(self):
307+
mgr = urllib.request.HTTPPasswordMgr()
308+
309+
self.assertEqual(
310+
mgr.reduce_uri("http://example.com/path"),
311+
("example.com:80", "/path"))
312+
self.assertTrue(
313+
mgr.is_suburi(
314+
("example.com", "/path"),
315+
("example.com", "/path/subpath")))
316+
273317

274318
class MockOpener:
275319
addheaders = []
@@ -1808,6 +1852,18 @@ def test_basic_prior_auth_auto_send(self):
18081852
# expect request to be sent with auth header
18091853
self.assertTrue(http_handler.has_auth_header)
18101854

1855+
def test_basic_prior_auth_different_scheme(self):
1856+
pwd_manager = HTTPPasswordMgrWithPriorAuth()
1857+
auth_handler = HTTPBasicAuthHandler(pwd_manager)
1858+
auth_handler.add_password(
1859+
None, "https://example.com/", "user", "password",
1860+
is_authenticated=True)
1861+
1862+
request = Request("http://example.com/")
1863+
auth_handler.http_request(request)
1864+
1865+
self.assertFalse(request.has_header("Authorization"))
1866+
18111867
def test_basic_prior_auth_send_after_first_success(self):
18121868
# Auto send auth header after authentication is successful once
18131869

Lib/urllib/request.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -813,16 +813,17 @@ def add_password(self, realm, uri, user, passwd):
813813
self.passwd[realm] = {}
814814
for default_port in True, False:
815815
reduced_uri = tuple(
816-
self.reduce_uri(u, default_port) for u in uri)
816+
self._reduce_uri_with_scheme(u, default_port) for u in uri)
817817
self.passwd[realm][reduced_uri] = (user, passwd)
818818

819819
def find_user_password(self, realm, authuri):
820820
domains = self.passwd.get(realm, {})
821821
for default_port in True, False:
822-
reduced_authuri = self.reduce_uri(authuri, default_port)
822+
reduced_authuri = self._reduce_uri_with_scheme(
823+
authuri, default_port)
823824
for uris, authinfo in domains.items():
824825
for uri in uris:
825-
if self.is_suburi(uri, reduced_authuri):
826+
if self._is_suburi_with_scheme(uri, reduced_authuri):
826827
return authinfo
827828
return None, None
828829

@@ -849,6 +850,17 @@ def reduce_uri(self, uri, default_port=True):
849850
authority = "%s:%d" % (host, dport)
850851
return authority, path
851852

853+
def _reduce_uri_with_scheme(self, uri, default_port=True):
854+
parts = urlsplit(uri)
855+
scheme = parts[0] if parts[1] else None
856+
return (scheme or None, *self.reduce_uri(uri, default_port))
857+
858+
def _is_suburi_with_scheme(self, base, test):
859+
if (base[0] is not None and test[0] is not None and
860+
base[0] != test[0]):
861+
return False
862+
return self.is_suburi(base[1:], test[1:])
863+
852864
def is_suburi(self, base, test):
853865
"""Check if test is below base in a URI tree
854866
@@ -894,14 +906,15 @@ def update_authenticated(self, uri, is_authenticated=False):
894906

895907
for default_port in True, False:
896908
for u in uri:
897-
reduced_uri = self.reduce_uri(u, default_port)
909+
reduced_uri = self._reduce_uri_with_scheme(u, default_port)
898910
self.authenticated[reduced_uri] = is_authenticated
899911

900912
def is_authenticated(self, authuri):
901913
for default_port in True, False:
902-
reduced_authuri = self.reduce_uri(authuri, default_port)
914+
reduced_authuri = self._reduce_uri_with_scheme(
915+
authuri, default_port)
903916
for uri in self.authenticated:
904-
if self.is_suburi(uri, reduced_authuri):
917+
if self._is_suburi_with_scheme(uri, reduced_authuri):
905918
return self.authenticated[uri]
906919

907920

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :cve:`2026-15806` by scoping :class:`~urllib.request.HTTPPasswordMgr`
2+
credentials to the URL scheme, preventing credentials stored for an HTTPS
3+
URL from being used for a matching HTTP URL, while URIs without a scheme
4+
continue to match any scheme.

0 commit comments

Comments
 (0)