Skip to content

Commit 95e57c3

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 3db5f9d commit 95e57c3

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

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

945951

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

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

275319
class MockOpener:
276320
addheaders = []
@@ -1714,6 +1758,18 @@ def test_basic_prior_auth_auto_send(self):
17141758
# expect request to be sent with auth header
17151759
self.assertTrue(http_handler.has_auth_header)
17161760

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

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)