8000 Lax cookie parsing in http.cookies could be a security issue when com… · stackless-dev/stackless@dad182c · GitHub
[go: up one dir, main page]

65F4
Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit dad182c

Browse files
committed
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.
1 parent 860c367 commit dad182c

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

Lib/http/cookies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ def OutputString(self, attrs=None):
432432
_LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"
433433
_CookiePattern = re.compile(r"""
434434
(?x) # This is a verbose pattern
435+
\s* # Optional whitespace at start of cookie
435436
(?P<key> # Start of group 'key'
436437
""" + _LegalCharsPatt + r"""+? # Any word of at least one letter
437438
) # End of group 'key'
@@ -532,7 +533,7 @@ def __parse_string(self, str, patt=_CookiePattern):
532533

533534
while 0 <= i < n:
534535
# Start looking for a cookie
535-
match = patt.search(str, i)
536+
match = patt.match(str, i)
536537
if not match:
537538
# No more cookies
538539
break

Lib/test/test_http_cookies.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ def test_quoted_meta(self):
132132
</script>
133133
""")
134134

135+
def test_invalid_cookies(self):
136+
# Accepting these could be a security issue
137+
C = cookies.SimpleCookie()
138+
for s in (']foo=x', '[foo=x', 'blah]foo=x', 'blah[foo=x'):
139+
C.load(s)
140+
self.assertEqual(dict(C), {})
141+
self.assertEqual(C.output(), '')
142+
143+
135144
class MorselTests(unittest.TestCase):
136145
"""Tests for the Morsel object."""
137146

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Martin Bless
117117
Pablo Bleyer
118118
Erik van Blokland
119119
Eric Blossom
120+
Sergey Bobrov
120121
Finn Bock
121122
Paul Boddie
122123
Matthew Boedicker

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Library
3737
strings for ``rfc822Name`` (email), ``dNSName`` (DNS) and
3838
``uniformResourceIdentifier`` (URI).
3939

40+
- Lax cookie parsing in http.cookies could be a security issue when combined
41+
with non-standard cookie handling in some Web browsers. Reported by
42+
Sergey Bobrov.
43+
4044
- Issue #21766: Prevent a security hole in CGIHTTPServer by URL unquoting paths
4145
before checking for a CGI script at that path.
4246

0 commit comments

Comments
 (0)
0