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

Skip to content

Commit 5596619

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 007a903 + 5d0d2e6 commit 5596619

4 files changed

Lines changed: 25 additions & 5 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
@@ -201,6 +201,7 @@ Tarn Weisner Burton
201201
Lee Busby
202202
Katherine Busch
203203
Ralph Butler
204+
Zach Byrne
204205
Nicolas Cadou
205206
Jp Calderone
206207
Arnaud Calmettes

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ Library
573573
- Issue #21923: Prevent AttributeError in distutils.sysconfig.customize_compiler
574574
due to possible uninitialized _config_vars.
575575

576+
- Issue #21323: Fix http.server to again handle scripts in CGI subdirectories,
577+
broken by the fix for security issue #19435. Patch by Zach Byrne.
578+
576579
Extension Modules
577580
-----------------
578581

0 commit comments

Comments
 (0)