[Python-checkins] cpython (2.7): url unquote the path before checking if it refers to a CGI script (closes

benjamin.peterson python-checkins at python.org
Sun Jun 15 03:41:57 CEST 2014


http://hg.python.org/cpython/rev/b4bab0788768
changeset:   91177:b4bab0788768
branch:      2.7
parent:      91172:bb8b0c7fefd0
user:        Benjamin Peterson <benjamin at python.org>
date:        Sat Jun 14 18:36:29 2014 -0700
summary:
  url unquote the path before checking if it refers to a CGI script (closes #21766)

files:
  Lib/CGIHTTPServer.py         |  2 +-
  Lib/test/test_httpservers.py |  5 +++++
  Misc/NEWS                    |  3 +++
  3 files changed, 9 insertions(+), 1 deletions(-)


diff --git a/Lib/CGIHTTPServer.py b/Lib/CGIHTTPServer.py
--- a/Lib/CGIHTTPServer.py
+++ b/Lib/CGIHTTPServer.py
@@ -84,7 +84,7 @@
         path begins with one of the strings in self.cgi_directories
         (and the next character is a '/' or the end of the string).
         """
-        collapsed_path = _url_collapse_path(self.path)
+        collapsed_path = _url_collapse_path(urllib.unquote(self.path))
         dir_sep = collapsed_path.find('/', 1)
         head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
         if head in self.cgi_directories:
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -510,6 +510,11 @@
                 (res.read(), res.getheader('Content-type'), res.status))
         self.assertEqual(os.environ['SERVER_SOFTWARE'], signature)
 
+    def test_urlquote_decoding_in_cgi_check(self):
+        res = self.request('/cgi-bin%2ffile1.py')
+        self.assertEqual((b'Hello World\n', 'text/html', 200),
+                (res.read(), res.getheader('Content-type'), res.status))
+
 
 class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
     """ Test url parsing """
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@
 Library
 -------
 
+- Issue #21766: Prevent a security hole in CGIHTTPServer by URL unquoting paths
+  before checking for a CGI script at that path.
+
 - Issue #21310: Fixed possible resource leak in failed open().
 
 - Issue #21304: Backport the key derivation function hashlib.pbkdf2_hmac from

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list