Skip to content

Commit 25db610

Browse files
lkk7miss-islington
authored andcommitted
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 d9e8e8e commit 25db610

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

923923
*uri* can be either a single URI, or a sequence of URIs. *realm*, *user* and
924924
*passwd* must be strings. This causes ``(user, passwd)`` to be used as
925-
authentication tokens when authentication for *realm* and a super-URI of any of
926-
the given URIs is given.
925+
authentication tokens when authentication for *realm* and a super-URI of any
926+
of the given URIs is given. If a URI includes a scheme, its credentials only
927+
match authentication URIs with the same scheme or no scheme. A URI without a
928+
scheme matches authentication URIs with any scheme.
929+
930+
.. versionchanged:: next
931+
Authentication credentials for URIs with a scheme are now scoped by
932+
that scheme.
927933

928934

929935
.. 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
@@ -269,6 +269,50 @@ def test_password_manager_default_port(self):
269269
self.assertEqual(find_user_pass("i", "http://j.example.com:80"),
270270
(None, None))
271271

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

273317
class MockOpener:
274318
addheaders = []
@@ -1712,6 +1756,18 @@ def test_basic_prior_auth_auto_send(self):
17121756
# expect request to be sent with auth header
17131757
self.assertTrue(http_handler.has_auth_header)
17141758

1759+
def test_basic_prior_auth_different_scheme(self):
1760+
pwd_manager = HTTPPasswordMgrWithPriorAuth()
1761+
auth_handler = HTTPBasicAuthHandler(pwd_manager)
1762+
auth_handler.add_password(
1763+
None, "https://example.com/", "user", "password",
1764+
is_authenticated=True)
1765+
1766+
request = Request("http://example.com/")
1767+
auth_handler.http_request(request)
1768+
1769+
self.assertFalse(request.has_header("Authorization"))
1770+
17151771
def test_basic_prior_auth_send_after_first_success(self):
17161772
# Auto send auth header after authentication is successful once
17171773

Lib/urllib/request.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -844,16 +844,17 @@ def add_password(self, realm, uri, user, passwd):
844844
self.passwd[realm] = {}
845845
for default_port in True, False:
846846
reduced_uri = tuple(
847-
self.reduce_uri(u, default_port) for u in uri)
847+
self._reduce_uri_with_scheme(u, default_port) for u in uri)
848848
self.passwd[realm][reduced_uri] = (user, passwd)
849849

850850
def find_user_password(self, realm, authuri):
851851
domains = self.passwd.get(realm, {})
852852
for default_port in True, False:
853-
reduced_authuri = self.reduce_uri(authuri, default_port)
853+
reduced_authuri = self._reduce_uri_with_scheme(
854+
authuri, default_port)
854855
for uris, authinfo in domains.items():
855856
for uri in uris:
856-
if self.is_suburi(uri, reduced_authuri):
857+
if self._is_suburi_with_scheme(uri, reduced_authuri):
857858
return authinfo
858859
return None, None
859860

@@ -880,6 +881,17 @@ def reduce_uri(self, uri, default_port=True):
880881
authority = "%s:%d" % (host, dport)
881882
return authority, path
882883

884+
def _reduce_uri_with_scheme(self, uri, default_port=True):
885+
parts = urlsplit(uri)
886+
scheme = parts[0] if parts[1] else None
887+
return (scheme or None, *self.reduce_uri(uri, default_port))
888+
889+
def _is_suburi_with_scheme(self, base, test):
890+
if (base[0] is not None and test[0] is not None and
891+
base[0] != test[0]):
892+
return False
893+
return self.is_suburi(base[1:], test[1:])
894+
883895
def is_suburi(self, base, test):
884896
"""Check if test is below base in a URI tree
885897
@@ -925,14 +937,15 @@ def update_authenticated(self, uri, is_authenticated=False):
925937

926938
for default_port in True, False:
927939
for u in uri:
928-
reduced_uri = self.reduce_uri(u, default_port)
940+
reduced_uri = self._reduce_uri_with_scheme(u, default_port)
929941
self.authenticated[reduced_uri] = is_authenticated
930942

931943
def is_authenticated(self, authuri):
932944
for default_port in True, False:
933-
reduced_authuri = self.reduce_uri(authuri, default_port)
945+
reduced_authuri = self._reduce_uri_with_scheme(
946+
authuri, default_port)
934947
for uri in self.authenticated:
935-
if self.is_suburi(uri, reduced_authuri):
948+
if self._is_suburi_with_scheme(uri, reduced_authuri):
936949
return self.authenticated[uri]
937950

938951

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)