From 955d52ad6ad269670be05409dbf3d9390254d9f7 Mon Sep 17 00:00:00 2001 From: Martin Panter Date: Mon, 18 Apr 2016 03:45:18 +0000 Subject: [PATCH] Issue #26657: Fix Windows directory traversal vulnerability with http.server Based on patch by Philipp Hagemeister. This fixes a regression caused by revision f4377699fd47. (cherry picked from commit d274b3f1f1e2d8811733fb952c9f18d7da3a376a) --- Lib/http/server.py | 6 +++--- Lib/test/test_httpservers.py | 19 +++++++++++++++++++ Misc/NEWS | 4 ++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 4688096b31c47b..2cfd3172a137e4 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -817,9 +817,9 @@ def translate_path(self, path): 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 += '/' diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py index 6e5f2db7fdd36b..64a576570312f8 100644 --- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -12,6 +12,7 @@ import sys import re import base64 +import ntpath import shutil import urllib.parse import html @@ -829,6 +830,24 @@ def test_start_with_double_slash(self): 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): diff --git a/Misc/NEWS b/Misc/NEWS index ac54bbc8b35a5f..da19254c446448 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -19,6 +19,10 @@ Documentation 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 #27850: Remove 3DES from ssl module's default cipher list to counter measure sweet32 attack (CVE-2016-2183).