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

Skip to content

Commit b3acacc

Browse files
committed
Transplant of rev 544b654d000c: directory traversal attack in CGIHttpRequestHandler.
1 parent 85b8be1 commit b3acacc

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

Lib/http/server.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -987,18 +987,17 @@ def is_python(self, path):
987987

988988
def run_cgi(self):
989989
"""Execute a CGI script."""
990-
path = self.path
991990
dir, rest = self.cgi_info
992991

993-
i = path.find('/', len(dir) + 1)
992+
i = rest.find('/')
994993
while i >= 0:
995-
nextdir = path[:i]
996-
nextrest = path[i+1:]
994+
nextdir = rest[:i]
995+
nextrest = rest[i+1:]
997996

998997
scriptdir = self.translate_path(nextdir)
999998
if os.path.isdir(scriptdir):
1000999
dir, rest = nextdir, nextrest
1001-
i = path.find('/', len(dir) + 1)
1000+
i = rest.find('/')
10021001
else:
10031002
break
10041003

Lib/test/test_httpservers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ def setUp(self):
325325
self.parent_dir = tempfile.mkdtemp()
326326
self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin')
327327
os.mkdir(self.cgi_dir)
328+
self.nocgi_path = None
328329
self.file1_path = None
329330
self.file2_path = None
330331

@@ -345,6 +346,11 @@ def setUp(self):
345346
self.tearDown()
346347
self.skipTest("Python executable path is not encodable to utf-8")
347348

349+
self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py')
350+
with open(self.nocgi_path, 'w') as fp:
351+
fp.write(cgi_file1 % self.pythonexe)
352+
os.chmod(self.nocgi_path, 0o777)
353+
348354
self.file1_path = os.path.join(self.cgi_dir, 'file1.py')
349355
with open(self.file1_path, 'w', encoding='utf-8') as file1:
350356
file1.write(cgi_file1 % self.pythonexe)
@@ -362,6 +368,8 @@ def tearDown(self):
362368
os.chdir(self.cwd)
363369
if self.pythonexe != sys.executable:
364370
os.remove(self.pythonexe)
371+
if self.nocgi_path:
372+
os.remove(self.nocgi_path)
365373
if self.file1_path:
366374
os.remove(self.file1_path)
367375
if self.file2_path:
@@ -418,6 +426,10 @@ def test_headers_and_content(self):
418426
self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
419427
(res.read(), res.getheader('Content-type'), res.status))
420428

429+
def test_issue19435(self):
430+
res = self.request('///////////nocgi.py/../cgi-bin/nothere.sh')
431+
self.assertEqual(res.status, 404)
432+
421433
def test_post(self):
422434
params = urllib.parse.urlencode(
423435
{'spam' : 1, 'eggs' : 'python', 'bacon' : 123456})

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ What's New in Python 3.3.3?
77

88
*Release date: XX-Nov-2013*
99

10+
Library
11+
-------
12+
13+
- Issue #19435: Fix directory traversal attack on CGIHttpRequestHandler.
14+
1015
Tests
1116
-----
1217

0 commit comments

Comments
 (0)