Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 637e454

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.
2 parents 8fad167 + 7d0b8f9 commit 637e454

4 files changed

Lines changed: 16 additions & 1 deletion

File tree

Lib/http/cookies.py

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

535536
while 0 <= i < n:
536537
# Start looking for a cookie
537-
match = patt.search(str, i)
538+
match = patt.match(str, i)
538539
if not match:
539540
# No more cookies
540541
break

Lib/test/test_http_cookies.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ def test_quoted_meta(self):
179179
</script>
180180
""")
181181

182+
def test_invalid_cookies(self):
183+
# Accepting these could be a security issue
184+
C = cookies.SimpleCookie()
185+
for s in (']foo=x', '[foo=x', 'blah]foo=x', 'blah[foo=x'):
186+
C.load(s)
187+
self.assertEqual(dict(C), {})
188+
self.assertEqual(C.output(), '')
189+
190+
182191
class MorselTests(unittest.TestCase):
183192
"""Tests for the Morsel object."""
184193

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Martin Bless
140140
Pablo Bleyer
141141
Erik van Blokland
142142
Eric Blossom
143+
Sergey Bobrov
143144
Finn Bock
144145
Paul Boddie
145146
Matthew Boedicker

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Core and Builtins
3232
Library
3333
-------
3434

35+
- Lax cookie parsing in http.cookies could be a security issue when combined
36+
with non-standard cookie handling in some Web browsers. Reported by
37+
Sergey Bobrov.
38+
3539
- Issue #22384: An exception in Tkinter callback no longer crashes the program
3640
when it is run with pythonw.exe.
3741

0 commit comments

Comments
 (0)