-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
gh-87389: avoid treating path as URI with netloc #93894
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
Open
nascheme
wants to merge
19
commits into
python:main
Choose a base branch
from
nascheme:urlunsplit_relative
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−17
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4f76c44
wip: alternative fix for gh-87389
nascheme 06b3879
Don't strip slashes if not building relative URL.
nascheme 00a3a92
Add unit test for urlunsplit relative URL case.
nascheme a8e1cc2
sanitize paths that can be confused as scheme
nascheme 83c8332
Add blurb.
nascheme f99e80b
Improve name and comment for _get_redirect_url().
nascheme e542578
Add urllib.parse.pathsplit() function.
nascheme b7b0b15
Fix blurb formating, add note about pathsplit().
nascheme 6915331
Improve comments and docs.
nascheme 915451c
Add basic unit test for pathsplit().
nascheme 952a0f4
Fix markup in blurb.
nascheme 899f512
Use pathsplit() in translate_path().
nascheme a00656c
Improved unit tests from gps.
nascheme 8985853
Add test for Request-URI containing scheme.
nascheme f1f94ae
Make _get_redirect_url() into a method.
nascheme 8a34cd0
Futher cleanups, remove urllib.parse.pathsplit().
nascheme 23d4b56
reword news
gpshead 7a71381
Merge branch 'main' into urlunsplit_relative
serhiy-storchaka d18bbd9
Merge branch 'main' into urlunsplit_relative
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -517,14 +517,32 @@ def urlunparse(components): | |
url = "%s;%s" % (url, params) | ||
return _coerce_result(urlunsplit((scheme, netloc, url, query, fragment))) | ||
|
||
# Returns true if path can confused with a scheme. I.e. a relative path | ||
# without leading dot that includes a colon in the first component. | ||
_is_scheme_like = re.compile(r'[^/.][^/]*:').match | ||
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. Why the special allowance for a leading dot? Is there a test case for it? Yes, a scheme cannot start with a dot, but a path-noscheme component of |
||
|
||
def urlunsplit(components): | ||
"""Combine the elements of a tuple as returned by urlsplit() into a | ||
complete URL as a string. The data argument can be any five-item iterable. | ||
This may result in a slightly different, but equivalent URL, if the URL that | ||
was parsed originally had unnecessary delimiters (for example, a ? with an | ||
empty query; the RFC states that these are equivalent).""" | ||
scheme, netloc, url, query, fragment, _coerce_result = ( | ||
scheme, netloc, path, query, fragment, _coerce_result = ( | ||
_coerce_args(*components)) | ||
if not scheme and not netloc: | ||
# Building a relative URI. Need to be careful that path is not | ||
# confused with scheme or netloc. | ||
if path.startswith('//'): | ||
# gh-87389: don't treat first component of path as netloc | ||
url = '/' + path.lstrip('/') | ||
elif _is_scheme_like(path): | ||
# first component has colon, ensure it will not be parsed as the | ||
# scheme | ||
url = './' + path | ||
else: | ||
url = path | ||
else: | ||
url = path | ||
if netloc or (scheme and scheme in uses_netloc) or url[:2] == '//': | ||
if url and url[:1] != '/': url = '/' + url | ||
url = '//' + (netloc or '') + url | ||
|
5 changes: 5 additions & 0 deletions
5
Misc/NEWS.d/next/Security/2022-06-16-12-13-55.gh-issue-87389.MS9wAR.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Change :func:`urllib.parse.urlunsplit` to sanitize ``path`` argument in order | ||
to avoid confusing the first component of the path as a net location or | ||
scheme. | ||
|
||
Co-authored-by: Gregory P. Smith <[email protected]> [Google] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
confusion with a protocol-relative URL? [as opposed to a host-relative URL]