changeset: 91665:b957f475e41e branch: 3.3 parent: 91398:cf156cfb12e7 parent: 91664:4de94641ba3e user: Ned Deily date: Sat Jul 12 22:12:39 2014 -0700 files: Lib/http/server.py Lib/test/test_httpservers.py Misc/ACKS Misc/NEWS description: Issue #21323: Fix http.server to again handle scripts in CGI subdirectories, broken by the fix for security issue #19435. Patch by Zach Byrne. diff -r cf156cfb12e7 -r b957f475e41e Lib/http/server.py --- a/Lib/http/server.py Wed Jun 25 13:05:31 2014 +0200 +++ b/Lib/http/server.py Sat Jul 12 22:12:39 2014 -0700 @@ -994,16 +994,16 @@ def run_cgi(self): """Execute a CGI script.""" dir, rest = self.cgi_info - - i = rest.find('/') + path = dir + '/' + rest + i = path.find('/', len(dir)+1) while i >= 0: - nextdir = rest[:i] - nextrest = rest[i+1:] + nextdir = path[:i] + nextrest = path[i+1:] scriptdir = self.translate_path(nextdir) if os.path.isdir(scriptdir): dir, rest = nextdir, nextrest - i = rest.find('/') + i = path.find('/', len(dir)+1) else: break diff -r cf156cfb12e7 -r b957f475e41e Lib/test/test_httpservers.py --- a/Lib/test/test_httpservers.py Wed Jun 25 13:05:31 2014 +0200 +++ b/Lib/test/test_httpservers.py Sat Jul 12 22:12:39 2014 -0700 @@ -324,10 +324,13 @@ self.cwd = os.getcwd() self.parent_dir = tempfile.mkdtemp() self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin') + self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir') os.mkdir(self.cgi_dir) + os.mkdir(self.cgi_child_dir) self.nocgi_path = None self.file1_path = None self.file2_path = None + self.file3_path = None # The shebang line should be pure ASCII: use symlink if possible. # See issue #7668. @@ -361,6 +364,11 @@ file2.write(cgi_file2 % self.pythonexe) os.chmod(self.file2_path, 0o777) + self.file3_path = os.path.join(self.cgi_child_dir, 'file3.py') + with open(self.file3_path, 'w', encoding='utf-8') as file3: + file3.write(cgi_file1 % self.pythonexe) + os.chmod(self.file3_path, 0o777) + os.chdir(self.parent_dir) def tearDown(self): @@ -374,6 +382,9 @@ os.remove(self.file1_path) if self.file2_path: os.remove(self.file2_path) + if self.file3_path: + os.remove(self.file3_path) + os.rmdir(self.cgi_child_dir) os.rmdir(self.cgi_dir) os.rmdir(self.parent_dir) finally: @@ -469,6 +480,11 @@ self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), (res.read(), res.getheader('Content-type'), res.status)) + def test_nested_cgi_path_issue21323(self): + res = self.request('/cgi-bin/child-dir/file3.py') + self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), + (res.read(), res.getheader('Content-type'), res.status)) + class SocketlessRequestHandler(SimpleHTTPRequestHandler): def __init__(self): diff -r cf156cfb12e7 -r b957f475e41e Misc/ACKS --- a/Misc/ACKS Wed Jun 25 13:05:31 2014 +0200 +++ b/Misc/ACKS Sat Jul 12 22:12:39 2014 -0700 @@ -186,6 +186,7 @@ Tarn Weisner Burton Lee Busby Ralph Butler +Zach Byrne Nicolas Cadou Jp Calderone Arnaud Calmettes diff -r cf156cfb12e7 -r b957f475e41e Misc/NEWS --- a/Misc/NEWS Wed Jun 25 13:05:31 2014 +0200 +++ b/Misc/NEWS Sat Jul 12 22:12:39 2014 -0700 @@ -38,6 +38,9 @@ as documented. The pattern and source keyword parameters are left as deprecated aliases. +- Issue #21323: Fix http.server to again handle scripts in CGI subdirectories, + broken by the fix for security issue #19435. Patch by Zach Byrne. + Tests -----