-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
gh-69589, gh-84774: Fix path normalization in urllib.parse.urljoin() #126679
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -362,6 +362,11 @@ def checkJoin(self, base, relurl, expected, *, relroundtrip=True): | |||||||||||||||||||||||||||||||||||||
self.assertEqual(urllib.parse.urljoin(base, relurl), expected) | ||||||||||||||||||||||||||||||||||||||
relurlb = urllib.parse.urlunsplit(urllib.parse.urlsplit(relurlb)) | ||||||||||||||||||||||||||||||||||||||
self.assertEqual(urllib.parse.urljoin(baseb, relurlb), expectedb) | ||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||
relurl = urllib.parse.urlunsplit(urllib.parse.urlsplit(relurl)) | ||||||||||||||||||||||||||||||||||||||
self.assertNotEqual(urllib.parse.urljoin(base, relurl), expected) | ||||||||||||||||||||||||||||||||||||||
relurlb = urllib.parse.urlunsplit(urllib.parse.urlsplit(relurlb)) | ||||||||||||||||||||||||||||||||||||||
self.assertNotEqual(urllib.parse.urljoin(baseb, relurlb), expectedb) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def test_unparse_parse(self): | ||||||||||||||||||||||||||||||||||||||
str_cases = ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',] | ||||||||||||||||||||||||||||||||||||||
|
@@ -568,6 +573,9 @@ def test_urljoins(self): | |||||||||||||||||||||||||||||||||||||
# slashes | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a/b/c/d/e/', '../../f/g/', 'http://a/b/c/f/g/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a/b/c/d/e', '../../f/g/', 'http://a/b/f/g/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a/b/c/d/e//', '../../f/g/', 'http://a/b/c/d/f/g/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a/b/c/d/e///', '../../f/g/', 'http://a/b/c/d/e/f/g/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a/b/c/d/e////', '../../f/g/', 'http://a/b/c/d/e//f/g/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a/b/c/d/e/', '/../../f/g/', 'http://a/f/g/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a/b/c/d/e', '/../../f/g/', 'http://a/f/g/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a/b/c/d/e/', '../../f/g', 'http://a/b/c/f/g') | ||||||||||||||||||||||||||||||||||||||
|
@@ -645,6 +653,16 @@ def test_urljoins_relative_base(self): | |||||||||||||||||||||||||||||||||||||
self.checkJoin('//', '/w', '///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', '///w', '///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', 'w', '///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', '../w', '///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', './w', '///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', '..//w', '///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', './/w', '///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', '..', '//') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', '.', '//') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', '../', '//') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', './', '//') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', '..//', '///') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//', './/', '///') | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '', '//a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '//', '//a') | ||||||||||||||||||||||||||||||||||||||
|
@@ -653,6 +671,16 @@ def test_urljoins_relative_base(self): | |||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '/w', '//a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '///w', '//a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', 'w', '//a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '../w', '//a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', './w', '//a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '..//w', '//a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', './/w', '//a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '..', '//a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '.', '//a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '../', '//a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', './', '//a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', '..//', '//a/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('//a', './/', '//a/') | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
for scheme in '', 'http:': | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '', 'http:') | ||||||||||||||||||||||||||||||||||||||
|
@@ -661,7 +689,21 @@ def test_urljoins_relative_base(self): | |||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '//v/w', 'http://v/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '/w', 'http:/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '///w', 'http:/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + 'w', 'http:/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + 'w', 'http:w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '../w', 'http:w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + './w', 'http:w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '..//w', 'http:/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + './/w', 'http:/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '..///w', 'http:////w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + './//w', 'http:////w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '..', 'http:') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '.', 'http:') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '../', 'http:') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + './', 'http:') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '..//', 'http:/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + './/', 'http:/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + '..///', 'http:////') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http:', scheme + './//', 'http:////') | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '', 'http://') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '//', 'http://') | ||||||||||||||||||||||||||||||||||||||
|
@@ -670,6 +712,20 @@ def test_urljoins_relative_base(self): | |||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '/w', 'http:///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '///w', 'http:///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + 'w', 'http:///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '../w', 'http:///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + './w', 'http:///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '..//w', 'http:///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + './/w', 'http:///w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '..///w', 'http:////w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + './//w', 'http:////w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '..', 'http://') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '.', 'http://') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '../', 'http://') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + './', 'http://') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '..//', 'http:///') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + './/', 'http:///') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + '..///', 'http:////') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://', scheme + './//', 'http:////') | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '', 'http://a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '//', 'http://a') | ||||||||||||||||||||||||||||||||||||||
|
@@ -678,6 +734,38 @@ def test_urljoins_relative_base(self): | |||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '/w', 'http://a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '///w', 'http://a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + 'w', 'http://a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '../w', 'http://a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + './w', 'http://a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '..//w', 'http://a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + './/w', 'http://a/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '..///w', 'http://a//w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + './//w', 'http://a//w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '..', 'http://a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '.', 'http://a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '../', 'http://a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + './', 'http://a') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '..//', 'http://a/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + './/', 'http://a/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + '..///', 'http://a//') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('http://a', scheme + './//', 'http://a//') | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '', 'b/c') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '//', 'b/c') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '//v', '//v') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '//v/w', '//v/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '/w', '/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '///w', '/w') | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same; the result should be |
||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', 'w', 'b/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '../w', 'w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '../../w', 'w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '../../../w', 'w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', 'w/.', 'b/w/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '../w/.', 'w/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '../../w/.', 'w/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '../../../w/.', 'w/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '..', '') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '../..', '') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('b/c', '../../..', '') | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+760
to
+768
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although these fall outside the direct scope of the pseudocode defined in RFC 3986 because Given non–RFC 3986 input where the base URI is path-relative (undefined scheme, undefined authority, and path not beginning with
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '', '/b/c') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '//', '/b/c') | ||||||||||||||||||||||||||||||||||||||
|
@@ -686,6 +774,16 @@ def test_urljoins_relative_base(self): | |||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '/w', '/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '///w', '/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', 'w', '/b/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '../w', '/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '../../w', '/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '../../../w', '/w') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', 'w/.', '/b/w/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '../w/.', '/w/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '../../w/.', '/w/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '../../../w/.', '/w/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '..', '/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '../..', '/') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('/b/c', '../../..', '/') | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
self.checkJoin('///b/c', '', '///b/c') | ||||||||||||||||||||||||||||||||||||||
self.checkJoin('///b/c', '//', '///b/c') | ||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix `urllib.parse.urljoin` for the case when the base path is relative | ||
and the relative reference path starts with '..'. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Preserve double slashes in the path in :func:`urllib.parse.urljoin`. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RFC 3986 carefully distinguishes between undefined and empty, and
//
has an empty authority, not undefined, so we should hit theif defined(R.authority)
branch in §5.2.2. The result should be//
.(This is independent of the discussion of #96015, I think.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I know. I left them non-distinguished for compatibility. We will likely change this in a separate issue.