Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d0d2e6

Browse files
committed
Issue #21323: Fix http.server to again handle scripts in CGI subdirectories,
broken by the fix for security issue #19435. Patch by Zach Byrne.
2 parents ad5ffd4 + 217f4cd commit 5d0d2e6

4 files changed

Lines changed: 26 additions & 6 deletions

File tree

Lib/http/server.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,16 +1000,16 @@ def is_python(self, path):
10001000
def run_cgi(self):
10011001
"""Execute a CGI script."""
10021002
dir, rest = self.cgi_info
1003-
1004-
i = rest.find('/')
1003+
path = dir + '/' + rest
1004+
i = path.find('/', len(dir)+1)
10051005
while i >= 0:
1006-
nextdir = rest[:i]
1007-
nextrest = rest[i+1:]
1006+
nextdir = path[:i]
1007+
nextrest = path[i+1:]
10081008

10091009
scriptdir = self.translate_path(nextdir)
10101010
if os.path.isdir(scriptdir):
10111011
dir, rest = nextdir, nextrest
1012-
i = rest.find('/')
1012+
i = path.find('/', len(dir)+1)
10131013
else:
10141014
break
10151015

Lib/test/test_httpservers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,13 @@ def setUp(self):
346346
self.cwd = os.getcwd()
347347
self.parent_dir = tempfile.mkdtemp()
348348
self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin')
349+
self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir')
349350
os.mkdir(self.cgi_dir)
351+
os.mkdir(self.cgi_child_dir)
350352
self.nocgi_path = None
351353
self.file1_path = None
352354
self.file2_path = None
355+
self.file3_path = None
353356

354357
# The shebang line should be pure ASCII: use symlink if possible.
355358
# See issue #7668.
@@ -383,6 +386,11 @@ def setUp(self):
383386
file2.write(cgi_file2 % self.pythonexe)
384387
os.chmod(self.file2_path, 0o777)
385388

389+
self.file3_path = os.path.join(self.cgi_child_dir, 'file3.py')
390+
with open(self.file3_path, 'w', encoding='utf-8') as file3:
391+
file3.write(cgi_file1 % self.pythonexe)
392+
os.chmod(self.file3_path, 0o777)
393+
386394
os.chdir(self.parent_dir)
387395

388396
def tearDown(self):
@@ -396,6 +404,9 @@ def tearDown(self):
396404
os.remove(self.file1_path)
397405
if self.file2_path:
398406
os.remove(self.file2_path)
407+
if self.file3_path:
408+
os.remove(self.file3_path)
409+
os.rmdir(self.cgi_child_dir)
399410
os.rmdir(self.cgi_dir)
400411
os.rmdir(self.parent_dir)
401412
finally:
@@ -491,6 +502,11 @@ def test_urlquote_decoding_in_cgi_check(self):
491502
self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
492503
(res.read(), res.getheader('Content-type'), res.status))
493504

505+
def test_nested_cgi_path_issue21323(self):
506+
res = self.request('/cgi-bin/child-dir/file3.py')
507+
self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
508+
(res.read(), res.getheader('Content-type'), res.status))
509+
494510

495511
class SocketlessRequestHandler(SimpleHTTPRequestHandler):
496512
def __init__(self):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ Tarn Weisner Burton
200200
Lee Busby
201201
Katherine Busch
202202
Ralph Butler
203+
Zach Byrne
203204
Nicolas Cadou
204205
Jp Calderone
205206
Arnaud Calmettes

Misc/NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
+++++++++++
1+
+++++++++++
22
Python News
33
+++++++++++
44

@@ -158,6 +158,9 @@ Library
158158
- Issue #21923: Prevent AttributeError in distutils.sysconfig.customize_compiler
159159
due to possible uninitialized _config_vars.
160160

161+
- Issue #21323: Fix http.server to again handle scripts in CGI subdirectories,
162+
broken by the fix for security issue #19435. Patch by Zach Byrne.
163+
161164
Build
162165
-----
163166

0 commit comments

Comments
 (0)