-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-87389: avoid treating path as URI with netloc #93894
base: main
Are you sure you want to change the base?
Changes from 11 commits
4f76c44
06b3879
00a3a92
a8e1cc2
83c8332
f99e80b
e542578
b7b0b15
6915331
915451c
952a0f4
899f512
a00656c
8985853
f1f94ae
8a34cd0
23d4b56
7a71381
d18bbd9
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 |
---|---|---|
|
@@ -1101,6 +1101,35 @@ def test_urlsplit_normalization(self): | |
with self.assertRaises(ValueError): | ||
urllib.parse.urlsplit(url) | ||
|
||
def test_urlunsplit_relative(self): | ||
cases = [ | ||
# expected result is a relative URL without netloc and scheme | ||
(('', 'a', '', '', ''), '//a'), | ||
# extra leading slashes need to be stripped to avoid confusion | ||
# with a relative URL | ||
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. confusion with a protocol-relative URL? [as opposed to a host-relative URL] |
||
(('', '', '//a', '', ''), '/a'), | ||
(('', '', '///a', '', ''), '/a'), | ||
# not relative so extra leading slashes don't need stripping since | ||
# they don't cause confusion | ||
(('http', 'x.y', '//a', '', ''), 'http://x.y//a'), | ||
# avoid confusion with path containing colon | ||
(('', '', 'a:b', '', ''), './a:b'), | ||
] | ||
for parts, result in cases: | ||
self.assertEqual(urllib.parse.urlunsplit(parts), result) | ||
nascheme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_pathsplit(self): | ||
cases = [ | ||
('//a', ('', '', '//a', '', '')), | ||
('a:b', ('', '', 'a:b', '', '')), | ||
('/a/b?x#y', ('', '', '/a/b', 'x', 'y')), | ||
('/a/b#y', ('', '', '/a/b', '', 'y')), | ||
('/a/b?x', ('', '', '/a/b', 'x', '')), | ||
] | ||
for uri, result in cases: | ||
self.assertEqual(urllib.parse.pathsplit(uri), result) | ||
|
||
|
||
class Utility_Tests(unittest.TestCase): | ||
"""Testcase to test the various utility functions in the urllib.""" | ||
# In Python 2 this test class was in test_urllib. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ | |
__all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag", | ||
"urlsplit", "urlunsplit", "urlencode", "parse_qs", | ||
"parse_qsl", "quote", "quote_plus", "quote_from_bytes", | ||
"unquote", "unquote_plus", "unquote_to_bytes", | ||
"unquote", "unquote_plus", "unquote_to_bytes", "pathsplit", | ||
"DefragResult", "ParseResult", "SplitResult", | ||
"DefragResultBytes", "ParseResultBytes", "SplitResultBytes"] | ||
|
||
|
@@ -480,6 +480,29 @@ def urlsplit(url, scheme='', allow_fragments=True): | |
v = SplitResult(scheme, netloc, url, query, fragment) | ||
return _coerce_result(v) | ||
|
||
# typed=True avoids BytesWarnings being emitted during cache key | ||
# comparison since this API supports both bytes and str input. | ||
@functools.lru_cache(typed=True) | ||
def pathsplit(path): | ||
nascheme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Parse a path that includes an optional query and fragment. | ||
The full syntax is: | ||
|
||
<path>?<query>#<fragment> | ||
|
||
The result is a named 5-tuple with fields set corresponding to the above. | ||
It is either a SplitResult or SplitResultBytes object, depending on the | ||
type of the url parameter. | ||
|
||
Note that % escapes are not expanded. | ||
""" | ||
path, _coerce_result = _coerce_args(path) | ||
for b in _UNSAFE_URL_BYTES_TO_REMOVE: | ||
path = path.replace(b, "") | ||
path, _, fragment = path.partition('#') | ||
path, _, query = path.partition('?') | ||
v = SplitResult('', '', path, query, fragment) | ||
return _coerce_result(v) | ||
|
||
def urlunparse(components): | ||
"""Put a parsed URL back together again. This may result in a | ||
slightly different, but equivalent URL, if the URL that was parsed | ||
|
@@ -491,14 +514,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 and url[:2] != '//'): | ||
if url and url[:1] != '/': url = '/' + url | ||
url = '//' + (netloc or '') + url | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
:mod:`http.server`: Fix an open redirection vulnerability in the HTTP server | ||
when an URI path starts with ``//``. Vulnerability discovered, and initial | ||
fix proposed, by Hamza Avvan. 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. Add :func:`urllib.parse.pathsplit` | ||
function. | ||
|
||
Co-authored-by: Gregory P. Smith <gps@google.com> | ||
nascheme marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Unfortunately BaseHTTPRequestHandler
self.path
isn't guaranteed to be only a pathname by anything.GET http://netloc/path/to/thing HTTP/1.0
Is a valid HTTP query to many http servers. Our existing http server code ignores the scheme and netloc on that and serves up /path/to/thing properly. This code might break things doing that? ie: if http://netloc winds up in the returned path, the it seems like that'd move through pathsplit and urlunsplit properly only by chance rather than design just given the name and purpose of the pathsplit API vs what was just handed to it as input.
Check out my updated test in #87389.
I have no idea if anything depends on this as it should be unusual for any client not pretending to be talking to an HTTP proxy to make requests that way as that isn't normal per the http/1.0 and 1.1 RFCs but as it worked before and other servers like Apache continue to support it, I don't think we can break that - at least not in a bugfix.
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.
Based on my reading of the RFC,
GET http://netloc/path/to/thing HTTP/1.0
should technically be allowed. However,http.server
doesn't handle it, before or after this PR. Instead, it tries to use the second word as an absolute file path. I feel like we should not attempt to fixhttp.server
to handle the above. For HTTP 1.0, using only a file path was required. Forcurl
I notice that even with the--http1.1
flag, it passes a path and not the URL with the scheme+netloc.The bug causing #87389, IMHO, is that
http.server
was not consistent in how it was treatingself.path
. Thetranslate_path()
method was treating it as a path and not a URL. But, the redirect to add a trailing slash (done only if a folder with that name was found), parsed it as allowing a full URL. It needs to do it one way or the other consistently. The_request_path_split()
method I added does this. It doesn't make sense to add this tourllib.parse
since whathttp.server
is doing is not modern HTTP.