]> granicus.if.org Git - python/commitdiff
Lax cookie parsing in http.cookies could be a security issue when
authorGuido van Rossum <guido@python.org>
Tue, 16 Sep 2014 22:45:36 +0000 (15:45 -0700)
committerGuido van Rossum <guido@python.org>
Tue, 16 Sep 2014 22:45:36 +0000 (15:45 -0700)
combined with non-standard cookie handling in some Web browsers.

Reported by Sergey Bobrov.

Lib/Cookie.py
Lib/test/test_cookie.py
Misc/ACKS
Misc/NEWS

index a5239ca6a379731cdbdb0df55c9f75e6a34dc60e..d67443745721a7d78222983e615dae2c4f13fac1 100644 (file)
@@ -531,6 +531,7 @@ class Morsel(dict):
 _LegalCharsPatt  = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"
 _CookiePattern = re.compile(
     r"(?x)"                       # This is a Verbose pattern
+    r"\s*"                        # Optional whitespace at start of cookie
     r"(?P<key>"                   # Start of group 'key'
     ""+ _LegalCharsPatt +"+?"     # Any word of at least one letter, nongreedy
     r")"                          # End of group 'key'
@@ -646,7 +647,7 @@ class BaseCookie(dict):
 
         while 0 <= i < n:
             # Start looking for a cookie
-            match = patt.search(str, i)
+            match = patt.match(str, i)
             if not match: break          # No more cookies
 
             K,V = match.group("key"), match.group("val")
index 5370a8de2b56de9689ac677627af973ce6b046c2..41ba60f9d2a4545a6b4e5ada825b9ba0b3bd678e 100644 (file)
@@ -133,6 +133,15 @@ class CookieTests(unittest.TestCase):
         self.assertEqual(C['Customer']['version'], '1')
         self.assertEqual(C['Customer']['path'], '/acme')
 
+    def test_invalid_cookies(self):
+        # Accepting these could be a security issue
+        C = Cookie.SimpleCookie()
+        for s in (']foo=x', '[foo=x', 'blah]foo=x', 'blah[foo=x'):
+            C.load(s)
+            self.assertEqual(dict(C), {})
+            self.assertEqual(C.output(), '')
+
+
 def test_main():
     run_unittest(CookieTests)
     if Cookie.__doc__ is not None:
index f9a4426b9999571ea4a8398a93b1379d425b734c..1ca04794fc90748124dd6846792766d6bbe81a46 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -136,6 +136,7 @@ Martin Bless
 Pablo Bleyer
 Erik van Blokland
 Eric Blossom
+Sergey Bobrov
 Finn Bock
 Paul Boddie
 Matthew Boedicker
index e5f8f76b156bf8cca771d071546e992966c89157..2907c1c7c6d0d345c3bcc031b172b76c3ea44ad1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@ Core and Builtins
 
 Library
 -------
+- Lax cookie parsing in http.cookies could be a security issue when combined
+  with non-standard cookie handling in some Web browsers.  Reported by
+  Sergey Bobrov.
 
 - Issue #21147: sqlite3 now raises an exception if the request contains a null
   character instead of truncate it.  Based on patch by Victor Stinner.