summaryrefslogtreecommitdiffstats
path: root/Lib/http
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2015-10-03 06:03:25 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2015-10-03 06:03:25 (GMT)
commit56b76d25dda22ed4d0a66b50e914920a4791f664 (patch)
tree47df32d5d700fca34a54e8cc94a449236acd3881 /Lib/http
parent103d06c81e0dcf57673072cedeedcffb2f3e73cc (diff)
parentcb29e8c0e5dc67c5f73926ea0c540612ca40714a (diff)
downloadcpython-56b76d25dda22ed4d0a66b50e914920a4791f664.zip
cpython-56b76d25dda22ed4d0a66b50e914920a4791f664.tar.gz
cpython-56b76d25dda22ed4d0a66b50e914920a4791f664.tar.bz2
Issues #25232, #24657: Merge two CGI server fixes from 3.4 into 3.5
Diffstat (limited to 'Lib/http')
-rw-r--r--Lib/http/server.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py
index 6a0e6fe..fde2f14 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -837,13 +837,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('/')
@@ -864,6 +866,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)
@@ -947,7 +952,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:
@@ -984,11 +989,7 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
break
# find an explicit query string, if present.
- i = rest.rfind('?')
- if i >= 0:
- rest, query = rest[:i], rest[i+1:]
- else:
- query = ''
+ rest, _, query = rest.partition('?')
# dissect the part after the directory name into a script name &
# a possible additional path, to be stored in PATH_INFO.