Skip to content

Commit 7b92f9f

Browse files
vstinnerned-deily
authored andcommitted
bpo-26657: Fix Windows directory traversal vulnerability with http.server (#782) (#2860)
Based on patch by Philipp Hagemeister. This fixes a regression caused by revision f4377699fd47. (cherry picked from commit d274b3f) (cherry picked from commit 6f6bc1d)
1 parent 8e88f6b commit 7b92f9f

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

Lib/http/server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,9 @@ def translate_path(self, path):
793793
words = filter(None, words)
794794
path = os.getcwd()
795795
for word in words:
796-
drive, word = os.path.splitdrive(word)
797-
head, word = os.path.split(word)
798-
if word in (os.curdir, os.pardir): continue
796+
if os.path.dirname(word) or word in (os.curdir, os.pardir):
797+
# Ignore components that are not a simple file/directory name
798+
continue
799799
path = os.path.join(path, word)
800800
if trailing_slash:
801801
path += '/'

Lib/test/test_httpservers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import sys
1313
import re
1414
import base64
15+
import ntpath
1516
import shutil
1617
import urllib.parse
1718
import http.client
@@ -703,6 +704,24 @@ def test_start_with_double_slash(self):
703704
path = self.handler.translate_path('//filename?foo=bar')
704705
self.assertEqual(path, self.translated)
705706

707+
def test_windows_colon(self):
708+
with support.swap_attr(server.os, 'path', ntpath):
709+
path = self.handler.translate_path('c:c:c:foo/filename')
710+
path = path.replace(ntpath.sep, os.sep)
711+
self.assertEqual(path, self.translated)
712+
713+
path = self.handler.translate_path('\\c:../filename')
714+
path = path.replace(ntpath.sep, os.sep)
715+
self.assertEqual(path, self.translated)
716+
717+
path = self.handler.translate_path('c:\\c:..\\foo/filename')
718+
path = path.replace(ntpath.sep, os.sep)
719+
self.assertEqual(path, self.translated)
720+
721+
path = self.handler.translate_path('c:c:foo\\c:c:bar/filename')
722+
path = path.replace(ntpath.sep, os.sep)
723+
self.assertEqual(path, self.translated)
724+
706725

707726
def test_main(verbose=None):
708727
cwd = os.getcwd()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix directory traversal vulnerability with http.server on Windows. This
2+
fixes a regression that was introduced in 3.3.4rc1 and 3.4.0rc1. Based on
3+
patch by Philipp Hagemeister.

0 commit comments

Comments
 (0)