From ca5be211adf050a2ba40aec42ea72a18470ae554 Mon Sep 17 00:00:00 2001 From: Fongeme Date: Sat, 9 Jan 2021 21:39:02 +0200 Subject: [PATCH 1/8] Changed commonprefix call to commonpath in posixpath.is_uri --- Lib/urllib/request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index e5febe61f556d3..8f0839471abf65 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -889,7 +889,7 @@ def is_suburi(self, base, test): return True if base[0] != test[0]: 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 From 506b8f670936118e419876a009f4d06624941f2d Mon Sep 17 00:00:00 2001 From: Fongeme Date: Sat, 9 Jan 2021 22:25:46 +0200 Subject: [PATCH 2/8] Fixed another bug in urllib.request.HTTPPasswordMgr.is_suburi where test was considered below base when base was below test, added tests --- Lib/test/test_urllib2.py | 11 +++++++++++ Lib/urllib/request.py | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 9db23e6ce04bd5..cfbaae717edfcc 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -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") @@ -211,6 +213,15 @@ def test_password_manager(self): self.assertEqual(find_user_pass("Some Realm", "e.example.com:3128"), ('5', 'e')) + # is_suburi + + self.assertTrue(is_suburi(reduce_uri('http://example.com/'), reduce_uri('http://example.com/sub_dir'))) + self.assertTrue(is_suburi(reduce_uri('http://example.com/sub_dir'), reduce_uri('http://example.com/sub_dir/sub_dir'))) + self.assertFalse(is_suburi(reduce_uri('http://example.com/second_dir'), reduce_uri('http://example.com/first_dir/second_dir'))) + self.assertFalse(is_suburi(reduce_uri('http://example.com/sub_dir'), reduce_uri('http://example.com/sub'))) + self.assertFalse(is_suburi(reduce_uri('http://example.com/sub_dir'), reduce_uri('http://exmaple.com/sub_dir_diff'))) + self.assertFalse(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 diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 8f0839471abf65..32100a68df1fa0 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -881,13 +881,13 @@ 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 below base in a URI tree (inclusive) 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) > len(base): return False common = posixpath.commonpath((base[1], test[1])) if len(common) == len(base[1]): From d72dde4cc07bf1128d3ffaea3fdb64f4b7e99fc5 Mon Sep 17 00:00:00 2001 From: Fongeme Date: Sat, 9 Jan 2021 22:36:12 +0200 Subject: [PATCH 3/8] Added blurb --- .../next/Library/2021-01-09-22-36-01.bpo-42766.6ybdUr.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-01-09-22-36-01.bpo-42766.6ybdUr.rst diff --git a/Misc/NEWS.d/next/Library/2021-01-09-22-36-01.bpo-42766.6ybdUr.rst b/Misc/NEWS.d/next/Library/2021-01-09-22-36-01.bpo-42766.6ybdUr.rst new file mode 100644 index 00000000000000..fe22bbb1035085 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-01-09-22-36-01.bpo-42766.6ybdUr.rst @@ -0,0 +1,3 @@ +Fixed 2 bugs in urllib.request.HTTPPasswordMgr.is_suburi, added some tests +for it. :func:`urllib.request.HTTPPasswordMgr.is_suburi`. Patch by Yair +Frid.` From 84424592993323e6a5226254ad59af0eff35004e Mon Sep 17 00:00:00 2001 From: Fongeme Date: Sat, 9 Jan 2021 22:51:38 +0200 Subject: [PATCH 4/8] Added test and fixes comparison --- Lib/test/test_urllib2.py | 3 ++- Lib/urllib/request.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index cfbaae717edfcc..c09781f7e28a2e 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -215,8 +215,9 @@ def test_password_manager(self): # is_suburi - self.assertTrue(is_suburi(reduce_uri('http://example.com/'), reduce_uri('http://example.com/sub_dir'))) + self.assertTrue(is_suburi(reduce_uri('http://example.com'), reduce_uri('http://example.com/sub_dir'))) self.assertTrue(is_suburi(reduce_uri('http://example.com/sub_dir'), reduce_uri('http://example.com/sub_dir/sub_dir'))) + self.assertFalse(is_suburi(reduce_uri('http://example.com/sub_dir/sub_dir'), reduce_uri('http://example.com/sub_dir'))) self.assertFalse(is_suburi(reduce_uri('http://example.com/second_dir'), reduce_uri('http://example.com/first_dir/second_dir'))) self.assertFalse(is_suburi(reduce_uri('http://example.com/sub_dir'), reduce_uri('http://example.com/sub'))) self.assertFalse(is_suburi(reduce_uri('http://example.com/sub_dir'), reduce_uri('http://exmaple.com/sub_dir_diff'))) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 32100a68df1fa0..b7162e0a8fd65e 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -853,6 +853,7 @@ def find_user_password(self, realm, authuri): reduced_authuri = self.reduce_uri(authuri, default_port) for uris, authinfo in domains.items(): for uri in uris: + print(uri, reduced_authuri) if self.is_suburi(uri, reduced_authuri): return authinfo return None, None @@ -887,7 +888,7 @@ def is_suburi(self, base, test): """ if base == test: return True - if base[0] != test[0] or len(test) > len(base): + if base[0] != test[0] or len(test[1]) < len(base[1]): return False common = posixpath.commonpath((base[1], test[1])) if len(common) == len(base[1]): From 4bf0a4426643598b66f635d75ac669393547a318 Mon Sep 17 00:00:00 2001 From: Fongeme Date: Sat, 9 Jan 2021 22:53:07 +0200 Subject: [PATCH 5/8] Remove debug print --- Lib/urllib/request.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index b7162e0a8fd65e..a0b754e587d41b 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -853,7 +853,6 @@ def find_user_password(self, realm, authuri): reduced_authuri = self.reduce_uri(authuri, default_port) for uris, authinfo in domains.items(): for uri in uris: - print(uri, reduced_authuri) if self.is_suburi(uri, reduced_authuri): return authinfo return None, None From cd68970e84632ee318ad939cd83cade6c942c43a Mon Sep 17 00:00:00 2001 From: Fongeme Date: Sat, 9 Jan 2021 23:27:29 +0200 Subject: [PATCH 6/8] Added myself to ACKS --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index 211455b4dfc3c2..b8c901407acf75 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -552,6 +552,7 @@ Stefan Franke Martin Franklin Kent Frazier Bruce Frederiksen +Yair Frid Jason Fried Robin Friedrich Bradley Froehle From 207c014dddc79ab51ac46ea9e398b798f2263d16 Mon Sep 17 00:00:00 2001 From: Yair Frid Date: Wed, 7 Jul 2021 08:50:21 +0300 Subject: [PATCH 7/8] Update Lib/test/test_urllib2.py Co-authored-by: Terry Jan Reedy --- Lib/test/test_urllib2.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index c09781f7e28a2e..680b7d619f5fef 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -215,13 +215,22 @@ def test_password_manager(self): # is_suburi - self.assertTrue(is_suburi(reduce_uri('http://example.com'), reduce_uri('http://example.com/sub_dir'))) - self.assertTrue(is_suburi(reduce_uri('http://example.com/sub_dir'), reduce_uri('http://example.com/sub_dir/sub_dir'))) - self.assertFalse(is_suburi(reduce_uri('http://example.com/sub_dir/sub_dir'), reduce_uri('http://example.com/sub_dir'))) - self.assertFalse(is_suburi(reduce_uri('http://example.com/second_dir'), reduce_uri('http://example.com/first_dir/second_dir'))) - self.assertFalse(is_suburi(reduce_uri('http://example.com/sub_dir'), reduce_uri('http://example.com/sub'))) - self.assertFalse(is_suburi(reduce_uri('http://example.com/sub_dir'), reduce_uri('http://exmaple.com/sub_dir_diff'))) - self.assertFalse(is_suburi(reduce_uri('http://example.com/sub_dir_diff'), reduce_uri('http://exmaple.com/sub_dir_second_diff'))) + true = self.assertTrue + false = self.assertFalse + true(is_suburi(reduce_uri('http://example.com'), + 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): """ From 769b213ff06fe940a540de75d1af8eab2a6d3ea4 Mon Sep 17 00:00:00 2001 From: Yair Frid Date: Mon, 12 Jul 2021 09:46:20 +0300 Subject: [PATCH 8/8] Update Lib/urllib/request.py --- Lib/urllib/request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index a0b754e587d41b..227485d336c3f6 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -881,7 +881,7 @@ 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 (inclusive) + """Check if test is equal or below base in a URI tree Both args must be URIs in reduced form. """