summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2015-10-03 05:55:46 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2015-10-03 05:55:46 (GMT)
commitcb29e8c0e5dc67c5f73926ea0c540612ca40714a (patch)
treea687804c0989d3a5d108daaa1550a6fa1aff3c48 /Lib
parenta02e18a43f61543c911c82698d3cf44c8c11a0c2 (diff)
downloadcpython-cb29e8c0e5dc67c5f73926ea0c540612ca40714a.zip
cpython-cb29e8c0e5dc67c5f73926ea0c540612ca40714a.tar.gz
cpython-cb29e8c0e5dc67c5f73926ea0c540612ca40714a.tar.bz2
Issue #24657: Prevent CGIRequestHandler from collapsing the URL query
Initial patch from Xiang Zhang. Also fix out-of-date _url_collapse_path() doc string.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/http/server.py13
-rw-r--r--Lib/test/test_httpservers.py7
2 files changed, 16 insertions, 4 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py
index 9a7c976..a97aa31 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -887,13 +887,15 @@ def _url_collapse_path(path):
The utility of this function is limited to is_cgi method and helps
preventing some security attacks.
- Returns: A tuple of (head, tail) where tail is everything after the final /
- and head is everything before it. Head will always start with a '/' and,
- if it contains anything else, never have a trailing '/'.
+ Returns: The reconstituted URL, which will always start with a '/'.
Raises: IndexError if too many '..' occur within the path.
"""
+ # Query component should not be involved.
+ path, _, query = path.partition('?')
+ path = urllib.parse.unquote(path)
+
# Similar to os.path.split(os.path.normpath(path)) but specific to URL
# path semantics rather than local operating system semantics.
path_parts = path.split('/')
@@ -914,6 +916,9 @@ def _url_collapse_path(path):
else:
tail_part = ''
+ if query:
+ tail_part = '?'.join((tail_part, query))
+
splitpath = ('/' + '/'.join(head_parts), tail_part)
collapsed_path = "/".join(splitpath)
@@ -995,7 +1000,7 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
(and the next character is a '/' or the end of the string).
"""
- collapsed_path = _url_collapse_path(urllib.parse.unquote(self.path))
+ collapsed_path = _url_collapse_path(self.path)
dir_sep = collapsed_path.find('/', 1)
head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
if head in self.cgi_directories:
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
index d50a9ac..6e5f2db 100644
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -565,6 +565,13 @@ class CGIHTTPServerTestCase(BaseTestCase):
(b'a=b?c=d' + self.linesep, 'text/html', 200),
(res.read(), res.getheader('Content-type'), res.status))
+ def test_query_with_continuous_slashes(self):
+ res = self.request('/cgi-bin/file4.py?k=aa%2F%2Fbb&//q//p//=//a//b//')
+ self.assertEqual(
+ (b'k=aa%2F%2Fbb&//q//p//=//a//b//' + self.linesep,
+ 'text/html', 200),
+ (res.read(), res.getheader('Content-type'), res.status))
+
class SocketlessRequestHandler(SimpleHTTPRequestHandler):
def __init__(self):