summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-09-17 00:23:55 +0200
committerAntoine Pitrou <solipsis@pitrou.net>2014-09-17 00:23:55 +0200
commitdad182c16e6c9d10267a659bd376ba3d10affd4f (patch)
treec70767003a273334c2c4bc9d4773c2a087beb97a
parent860c367c29eb557930099a7cc7fe297a259275f6 (diff)
downloadcpython-git-dad182c16e6c9d10267a659bd376ba3d10affd4f.tar.gz
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.
-rw-r--r--Lib/http/cookies.py3
-rw-r--r--Lib/test/test_http_cookies.py9
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS4
4 files changed, 16 insertions, 1 deletions
diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py
index ddbcbf877b..28c1161582 100644
--- a/Lib/http/cookies.py
+++ b/Lib/http/cookies.py
@@ -432,6 +432,7 @@ class Morsel(dict):
_LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"
_CookiePattern = re.compile(r"""
(?x) # This is a verbose pattern
+ \s* # Optional whitespace at start of cookie
(?P<key> # Start of group 'key'
""" + _LegalCharsPatt + r"""+? # Any word of at least one letter
) # End of group 'key'
@@ -532,7 +533,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:
# No more cookies
break
diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py
index 1f1ca5852e..30d48983c6 100644
--- a/Lib/test/test_http_cookies.py
+++ b/Lib/test/test_http_cookies.py
@@ -132,6 +132,15 @@ class CookieTests(unittest.TestCase):
</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):
"""Tests for the Morsel object."""
diff --git a/Misc/ACKS b/Misc/ACKS
index c183dc78f3..428fd01e8a 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -117,6 +117,7 @@ Martin Bless
Pablo Bleyer
Erik van Blokland
Eric Blossom
+Sergey Bobrov
Finn Bock
Paul Boddie
Matthew Boedicker
diff --git a/Misc/NEWS b/Misc/NEWS
index d8e61c3038..398ed294cf 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,10 @@ Library
strings for ``rfc822Name`` (email), ``dNSName`` (DNS) and
``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
before checking for a CGI script at that path.