Commit f3667ce5 authored by Guido van Rossum's avatar Guido van Rossum

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.
parent f7c0583d
...@@ -531,6 +531,7 @@ class Morsel(dict): ...@@ -531,6 +531,7 @@ class Morsel(dict):
_LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]" _LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"
_CookiePattern = re.compile( _CookiePattern = re.compile(
r"(?x)" # This is a Verbose pattern r"(?x)" # This is a Verbose pattern
r"\s*" # Optional whitespace at start of cookie
r"(?P<key>" # Start of group 'key' r"(?P<key>" # Start of group 'key'
""+ _LegalCharsPatt +"+?" # Any word of at least one letter, nongreedy ""+ _LegalCharsPatt +"+?" # Any word of at least one letter, nongreedy
r")" # End of group 'key' r")" # End of group 'key'
...@@ -646,7 +647,7 @@ class BaseCookie(dict): ...@@ -646,7 +647,7 @@ class BaseCookie(dict):
while 0 <= i < n: while 0 <= i < n:
# Start looking for a cookie # Start looking for a cookie
match = patt.search(str, i) match = patt.match(str, i)
if not match: break # No more cookies if not match: break # No more cookies
K,V = match.group("key"), match.group("val") K,V = match.group("key"), match.group("val")
......
...@@ -133,6 +133,15 @@ class CookieTests(unittest.TestCase): ...@@ -133,6 +133,15 @@ class CookieTests(unittest.TestCase):
self.assertEqual(C['Customer']['version'], '1') self.assertEqual(C['Customer']['version'], '1')
self.assertEqual(C['Customer']['path'], '/acme') 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(): def test_main():
run_unittest(CookieTests) run_unittest(CookieTests)
if Cookie.__doc__ is not None: if Cookie.__doc__ is not None:
......
...@@ -136,6 +136,7 @@ Martin Bless ...@@ -136,6 +136,7 @@ Martin Bless
Pablo Bleyer Pablo Bleyer
Erik van Blokland Erik van Blokland
Eric Blossom Eric Blossom
Sergey Bobrov
Finn Bock Finn Bock
Paul Boddie Paul Boddie
Matthew Boedicker Matthew Boedicker
......
...@@ -21,6 +21,9 @@ Core and Builtins ...@@ -21,6 +21,9 @@ Core and Builtins
Library 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 - Issue #21147: sqlite3 now raises an exception if the request contains a null
character instead of truncate it. Based on patch by Victor Stinner. character instead of truncate it. Based on patch by Victor Stinner.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment