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.
This commit is contained in:
Antoine Pitrou 2014-09-17 00:23:55 +02:00
parent 860c367c29
commit dad182c16e
4 changed files with 16 additions and 1 deletions

View File

@ -432,6 +432,7 @@ class Morsel(dict):
_LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]" _LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"
_CookiePattern = re.compile(r""" _CookiePattern = re.compile(r"""
(?x) # This is a verbose pattern (?x) # This is a verbose pattern
\s* # Optional whitespace at start of cookie
(?P<key> # Start of group 'key' (?P<key> # Start of group 'key'
""" + _LegalCharsPatt + r"""+? # Any word of at least one letter """ + _LegalCharsPatt + r"""+? # Any word of at least one letter
) # End of group 'key' ) # End of group 'key'
@ -532,7 +533,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: if not match:
# No more cookies # No more cookies
break break

View File

@ -132,6 +132,15 @@ class CookieTests(unittest.TestCase):
</script> </script>
""") """)
def test_invalid_cookies(self):
# Accepting these could be a security issue
C = cookies.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(), '')
class MorselTests(unittest.TestCase): class MorselTests(unittest.TestCase):
"""Tests for the Morsel object.""" """Tests for the Morsel object."""

View File

@ -117,6 +117,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

View File

@ -37,6 +37,10 @@ Library
strings for ``rfc822Name`` (email), ``dNSName`` (DNS) and strings for ``rfc822Name`` (email), ``dNSName`` (DNS) and
``uniformResourceIdentifier`` (URI). ``uniformResourceIdentifier`` (URI).
- 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 #21766: Prevent a security hole in CGIHTTPServer by URL unquoting paths - Issue #21766: Prevent a security hole in CGIHTTPServer by URL unquoting paths
before checking for a CGI script at that path. before checking for a CGI script at that path.