]> granicus.if.org Git - python/commitdiff
Issue #26657: Fix Windows directory traversal vulnerability with http.server
authorMartin Panter <vadmium+py@gmail.com>
Mon, 18 Apr 2016 03:45:18 +0000 (03:45 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Mon, 18 Apr 2016 03:45:18 +0000 (03:45 +0000)
Based on patch by Philipp Hagemeister.  This fixes a regression caused by
revision f4377699fd47.

Lib/http/server.py
Lib/test/test_httpservers.py
Misc/NEWS

index fac4d9db39ed37f5ff2027f1909c9ea0ed42fdc2..3bd1f7afb363d81bf554e4f30a6c90dc83e7a008 100644 (file)
@@ -774,9 +774,9 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
         words = filter(None, words)
         path = os.getcwd()
         for word in words:
-            drive, word = os.path.splitdrive(word)
-            head, word = os.path.split(word)
-            if word in (os.curdir, os.pardir): continue
+            if os.path.dirname(word) or word in (os.curdir, os.pardir):
+                # Ignore components that are not a simple file/directory name
+                continue
             path = os.path.join(path, word)
         if trailing_slash:
             path += '/'
index f396ebd66c9be5b429cfbd009c315671ca92df78..08666848d00f6de070920207e74a4a6311c194e3 100644 (file)
@@ -12,6 +12,7 @@ import os
 import sys
 import re
 import base64
+import ntpath
 import shutil
 import urllib.parse
 import html
@@ -918,6 +919,24 @@ class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
         path = self.handler.translate_path('//filename?foo=bar')
         self.assertEqual(path, self.translated)
 
+    def test_windows_colon(self):
+        with support.swap_attr(server.os, 'path', ntpath):
+            path = self.handler.translate_path('c:c:c:foo/filename')
+            path = path.replace(ntpath.sep, os.sep)
+            self.assertEqual(path, self.translated)
+
+            path = self.handler.translate_path('\\c:../filename')
+            path = path.replace(ntpath.sep, os.sep)
+            self.assertEqual(path, self.translated)
+
+            path = self.handler.translate_path('c:\\c:..\\foo/filename')
+            path = path.replace(ntpath.sep, os.sep)
+            self.assertEqual(path, self.translated)
+
+            path = self.handler.translate_path('c:c:foo\\c:c:bar/filename')
+            path = path.replace(ntpath.sep, os.sep)
+            self.assertEqual(path, self.translated)
+
 
 class MiscTestCase(unittest.TestCase):
     def test_all(self):
index 7b6b4187cbba4acfcbc8ae57ca40cee97720447a..867613fddca950d54f7d77ffb3d86cdff505e492 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -107,6 +107,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #26657: Fix directory traversal vulnerability with http.server on
+  Windows.  This fixes a regression that was introduced in 3.3.4rc1 and
+  3.4.0rc1.  Based on patch by Philipp Hagemeister.
+
 - Issue #26717: Stop encoding Latin-1-ized WSGI paths with UTF-8.  Patch by
   Anthony Sottile.