Skip to content

bpo-42766 Fix 2 bugs in urllib.request.HTTPPasswordMgr.is_suburi #24181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def test_password_manager(self):
mgr = urllib.request.HTTPPasswordMgr()
add = mgr.add_password
find_user_pass = mgr.find_user_password
is_suburi = mgr.is_suburi
reduce_uri = mgr.reduce_uri

add("Some Realm", "http://example.com/", "joe", "password")
add("Some Realm", "http://example.com/ni", "ni", "ni")
Expand Down Expand Up @@ -211,6 +213,25 @@ def test_password_manager(self):
self.assertEqual(find_user_pass("Some Realm", "e.example.com:3128"),
('5', 'e'))

# is_suburi

true = self.assertTrue
false = self.assertFalse
true(is_suburi(reduce_uri('http://example.com'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be easier to understand the logic tested if there was a blank line between each subtest.

reduce_uri('http://example.com/sub_dir')))
true(is_suburi(reduce_uri('http://example.com/sub_dir'),
reduce_uri('http://example.com/sub_dir/sub_dir')))
false(is_suburi(reduce_uri('http://example.com/sub_dir/sub_dir'),
reduce_uri('http://example.com/sub_dir')))
false(is_suburi(reduce_uri('http://example.com/second_dir'),
reduce_uri('http://example.com/first_dir/second_dir')))
false(is_suburi(reduce_uri('http://example.com/sub_dir'),
reduce_uri('http://example.com/sub')))
false(is_suburi(reduce_uri('http://example.com/sub_dir'),
reduce_uri('http://exmaple.com/sub_dir_diff')))
false(is_suburi(reduce_uri('http://example.com/sub_dir_diff'),
reduce_uri('http://exmaple.com/sub_dir_second_diff')))

def test_password_manager_default_port(self):
"""
The point to note here is that we can't guess the default port if
Expand Down
6 changes: 3 additions & 3 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,15 +881,15 @@ def reduce_uri(self, uri, default_port=True):
return authority, path

def is_suburi(self, base, test):
"""Check if test is below base in a URI tree
"""Check if test is equal or below base in a URI tree

Both args must be URIs in reduced form.
"""
if base == test:
return True
if base[0] != test[0]:
if base[0] != test[0] or len(test[1]) < len(base[1]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases pass even if this change is not included. Are you sure it's needed?

return False
common = posixpath.commonprefix((base[1], test[1]))
common = posixpath.commonpath((base[1], test[1]))
if len(common) == len(base[1]):
return True
return False
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ Stefan Franke
Martin Franklin
Kent Frazier
Bruce Frederiksen
Yair Frid
Jason Fried
Robin Friedrich
Bradley Froehle
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed 2 bugs in urllib.request.HTTPPasswordMgr.is_suburi, added some tests
Copy link
Contributor

@akulakov akulakov Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful to shortly describe the 2 bugs being fixed. One or two sentences should be enough.

for it. :func:`urllib.request.HTTPPasswordMgr.is_suburi`. Patch by Yair
Frid.`