Skip to content

Commit 3cb0503

Browse files
kkangshawnSiwon Kang
authored andcommitted
bpo-38863: Improve is_cgi() in http.server
is_cgi() function of http.server library does not currently handle a cgi script if one of the cgi_directories is located at the sub-directory of given path. Since is_cgi() in CGIHTTPRequestHandler class separates given path into (dir, rest) based on the first seen '/', multi-level directories like /sub/dir/cgi-bin/hello.py is divided into head=/sub, rest=dir/cgi-bin/hello.py then check whether '/sub' exists in cgi_directories = [..., '/sub/dir/cgi-bin']. This patch makes the is_cgi() keep expanding dir part to the next '/' then checking if that expanded path exists in the cgi_directories. Signed-off-by: Siwon Kang <[email protected]>
1 parent 4dedd0f commit 3cb0503

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/http/server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,8 +1014,10 @@ def is_cgi(self):
10141014
"""
10151015
collapsed_path = _url_collapse_path(self.path)
10161016
dir_sep = collapsed_path.find('/', 1)
1017-
head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
1018-
if head in self.cgi_directories:
1017+
while dir_sep > 0 and not collapsed_path[:dir_sep] in self.cgi_directories:
1018+
dir_sep = collapsed_path.find('/', dir_sep+1)
1019+
if dir_sep > 0:
1020+
head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
10191021
self.cgi_info = head, tail
10201022
return True
10211023
return False

Lib/test/test_httpservers.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,20 @@ def setUp(self):
601601
self.parent_dir = tempfile.mkdtemp()
602602
self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin')
603603
self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir')
604+
self.sub_dir_1 = os.path.join(self.parent_dir, 'sub')
605+
self.sub_dir_2 = os.path.join(self.sub_dir_1, 'dir')
606+
self.cgi_dir_in_sub_dir = os.path.join(self.sub_dir_2, 'cgi-bin')
604607
os.mkdir(self.cgi_dir)
605608
os.mkdir(self.cgi_child_dir)
609+
os.mkdir(self.sub_dir_1)
610+
os.mkdir(self.sub_dir_2)
611+
os.mkdir(self.cgi_dir_in_sub_dir)
606612
self.nocgi_path = None
607613
self.file1_path = None
608614
self.file2_path = None
609615
self.file3_path = None
610616
self.file4_path = None
617+
self.file5_path = None
611618

612619
# The shebang line should be pure ASCII: use symlink if possible.
613620
# See issue #7668.
@@ -652,6 +659,11 @@ def setUp(self):
652659
file4.write(cgi_file4 % (self.pythonexe, 'QUERY_STRING'))
653660
os.chmod(self.file4_path, 0o777)
654661

662+
self.file5_path = os.path.join(self.cgi_dir_in_sub_dir, 'file5.py')
663+
with open(self.file5_path, 'w', encoding='utf-8') as file5:
664+
file5.write(cgi_file1 % self.pythonexe)
665+
os.chmod(self.file5_path, 0o777)
666+
655667
os.chdir(self.parent_dir)
656668

657669
def tearDown(self):
@@ -669,8 +681,13 @@ def tearDown(self):
669681
os.remove(self.file3_path)
670682
if self.file4_path:
671683
os.remove(self.file4_path)
684+
if self.file5_path:
685+
os.remove(self.file5_path)
672686
os.rmdir(self.cgi_child_dir)
673687
os.rmdir(self.cgi_dir)
688+
os.rmdir(self.cgi_dir_in_sub_dir)
689+
os.rmdir(self.sub_dir_2)
690+
os.rmdir(self.sub_dir_1)
674691
os.rmdir(self.parent_dir)
675692
finally:
676693
BaseTestCase.tearDown(self)
@@ -789,6 +806,13 @@ def test_query_with_continuous_slashes(self):
789806
'text/html', HTTPStatus.OK),
790807
(res.read(), res.getheader('Content-type'), res.status))
791808

809+
def test_cgi_path_in_sub_directories(self):
810+
CGIHTTPRequestHandler.cgi_directories.append('/sub/dir/cgi-bin')
811+
res = self.request('/sub/dir/cgi-bin/file5.py')
812+
self.assertEqual(
813+
(b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK),
814+
(res.read(), res.getheader('Content-type'), res.status))
815+
792816

793817
class SocketlessRequestHandler(SimpleHTTPRequestHandler):
794818
def __init__(self, directory=None):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve :func:`is_cgi` function in :mod:`http.server`, which enables processing
2+
the case that cgi directory is a child of another directory other than root.

0 commit comments

Comments
 (0)