mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
[3.11] gh-123067: Fix quadratic complexity in parsing "-quoted cookie values with backslashes (GH-123075) (#123105)
This fixes CVE-2024-7592.
(cherry picked from commit 44e458357f
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
d0e8c100e4
commit
d4ac921a4b
3 changed files with 47 additions and 26 deletions
|
@ -5,6 +5,7 @@ import unittest
|
|||
import doctest
|
||||
from http import cookies
|
||||
import pickle
|
||||
from test import support
|
||||
|
||||
|
||||
class CookieTests(unittest.TestCase):
|
||||
|
@ -58,6 +59,43 @@ class CookieTests(unittest.TestCase):
|
|||
for k, v in sorted(case['dict'].items()):
|
||||
self.assertEqual(C[k].value, v)
|
||||
|
||||
def test_unquote(self):
|
||||
cases = [
|
||||
(r'a="b=\""', 'b="'),
|
||||
(r'a="b=\\"', 'b=\\'),
|
||||
(r'a="b=\="', 'b=='),
|
||||
(r'a="b=\n"', 'b=n'),
|
||||
(r'a="b=\042"', 'b="'),
|
||||
(r'a="b=\134"', 'b=\\'),
|
||||
(r'a="b=\377"', 'b=\xff'),
|
||||
(r'a="b=\400"', 'b=400'),
|
||||
(r'a="b=\42"', 'b=42'),
|
||||
(r'a="b=\\042"', 'b=\\042'),
|
||||
(r'a="b=\\134"', 'b=\\134'),
|
||||
(r'a="b=\\\""', 'b=\\"'),
|
||||
(r'a="b=\\\042"', 'b=\\"'),
|
||||
(r'a="b=\134\""', 'b=\\"'),
|
||||
(r'a="b=\134\042"', 'b=\\"'),
|
||||
]
|
||||
for encoded, decoded in cases:
|
||||
with self.subTest(encoded):
|
||||
C = cookies.SimpleCookie()
|
||||
C.load(encoded)
|
||||
self.assertEqual(C['a'].value, decoded)
|
||||
|
||||
@support.requires_resource('cpu')
|
||||
def test_unquote_large(self):
|
||||
n = 10**6
|
||||
for encoded in r'\\', r'\134':
|
||||
with self.subTest(encoded):
|
||||
data = 'a="b=' + encoded*n + ';"'
|
||||
C = cookies.SimpleCookie()
|
||||
C.load(data)
|
||||
value = C['a'].value
|
||||
self.assertEqual(value[:3], 'b=\\')
|
||||
self.assertEqual(value[-2:], '\\;')
|
||||
self.assertEqual(len(value), n + 3)
|
||||
|
||||
def test_load(self):
|
||||
C = cookies.SimpleCookie()
|
||||
C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue