Skip to content

Commit a2773a3

Browse files
miss-islingtonlkk7
andauthored
[3.13] gh-155694: Scope HTTPPasswordMgr credentials by URL scheme (GH-155696) (#155970)
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 f0e236e commit a2773a3

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
@@ -943,8 +943,14 @@ These methods are available on :class:`HTTPPasswordMgr` and
943943

944944
*uri* can be either a single URI, or a sequence of URIs. *realm*, *user* and
945945
*passwd* must be strings. This causes ``(user, passwd)`` to be used as
946-
authentication tokens when authentication for *realm* and a super-URI of any of
947-
the given URIs is given.
946+
authentication tokens when authentication for *realm* and a super-URI of any
947+
of the given URIs is given. If a URI includes a scheme, its credentials only
948+
match authentication URIs with the same scheme or no scheme. A URI without a
949+
scheme matches authentication URIs with any scheme.
950+
951+
.. versionchanged:: next
952+
Authentication credentials for URIs with a scheme are now scoped by
953+
that scheme.
948954

949955

950956
.. 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
@@ -273,6 +273,50 @@ def test_password_manager_default_port(self):
273273
self.assertEqual(find_user_pass("i", "http://j.example.com:80"),
274274
(None, None))
275275

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

277321
class MockOpener:
278322
addheaders = []
@@ -1795,6 +1839,18 @@ def test_basic_prior_auth_auto_send(self):
17951839
# expect request to be sent with auth header
17961840
self.assertTrue(http_handler.has_auth_header)
17971841

1842+
def test_basic_prior_auth_different_scheme(self):
1843+
pwd_manager = HTTPPasswordMgrWithPriorAuth()
1844+
auth_handler = HTTPBasicAuthHandler(pwd_manager)
1845+
auth_handler.add_password(
1846+
None, "https://example.com/", "user", "password",
1847+
is_authenticated=True)
1848+
1849+
request = Request("http://example.com/")
1850+
auth_handler.http_request(request)
1851+
1852+
self.assertFalse(request.has_header("Authorization"))
1853+
17981854
def test_basic_prior_auth_send_after_first_success(self):
17991855
# Auto send auth header after authentication is successful once
18001856

Lib/urllib/request.py

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

821821
def find_user_password(self, realm, authuri):
822822
domains = self.passwd.get(realm, {})
823823
for default_port in True, False:
824-
reduced_authuri = self.reduce_uri(authuri, default_port)
824+
reduced_authuri = self._reduce_uri_with_scheme(
825+
authuri, default_port)
825826
for uris, authinfo in domains.items():
826827
for uri in uris:
827-
if self.is_suburi(uri, reduced_authuri):
828+
if self._is_suburi_with_scheme(uri, reduced_authuri):
828829
return authinfo
829830
return None, None
830831

@@ -851,6 +852,17 @@ def reduce_uri(self, uri, default_port=True):
851852
authority = "%s:%d" % (host, dport)
852853
return authority, path
853854

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

897909
for default_port in True, False:
898910
for u in uri:
899-
reduced_uri = self.reduce_uri(u, default_port)
911+
reduced_uri = self._reduce_uri_with_scheme(u, default_port)
900912
self.authenticated[reduced_uri] = is_authenticated
901913

902914
def is_authenticated(self, authuri):
903915
for default_port in True, False:
904-
reduced_authuri = self.reduce_uri(authuri, default_port)
916+
reduced_authuri = self._reduce_uri_with_scheme(
917+
authuri, default_port)
905918
for uri in self.authenticated:
906-
if self.is_suburi(uri, reduced_authuri):
919+
if self._is_suburi_with_scheme(uri, reduced_authuri):
907920
return self.authenticated[uri]
908921

909922

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)