Skip to content

Commit cdffb9f

Browse files
serhiy-storchakamcepl
authored andcommitted
[CVE-2024-7592] Fix quadratic complexity in parsing "-quoted cookie
Fix quadratic complexity in parsing ``"``-quoted cookie values with backslashes by `http.cookies`. Fixes: gh#python#123067 Fixes: bsc#1229596 (CVE-2024-7592) From-PR: gh#python/cpython!123075 Co-authored-by: Serhiy Storchaka <[email protected]> Patch: CVE-2024-7592-quad-complex-cookies.patch
1 parent 3be49f5 commit cdffb9f

File tree

3 files changed

+47
-27
lines changed

3 files changed

+47
-27
lines changed

Lib/http/cookies.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,13 @@ def _quote(str):
189189
return '"' + str.translate(_Translator) + '"'
190190

191191

192-
_OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
193-
_QuotePatt = re.compile(r"[\\].")
192+
_unquote_sub = re.compile(r'\\(?:([0-3][0-7][0-7])|(.))').sub
193+
194+
def _unquote_replace(m):
195+
if m[1]:
196+
return chr(int(m[1], 8))
197+
else:
198+
return m[2]
194199

195200
def _unquote(str):
196201
# If there aren't any doublequotes,
@@ -210,30 +215,7 @@ def _unquote(str):
210215
# \012 --> \n
211216
# \" --> "
212217
#
213-
i = 0
214-
n = len(str)
215-
res = []
216-
while 0 <= i < n:
217-
o_match = _OctalPatt.search(str, i)
218-
q_match = _QuotePatt.search(str, i)
219-
if not o_match and not q_match: # Neither matched
220-
res.append(str[i:])
221-
break
222-
# else:
223-
j = k = -1
224-
if o_match:
225-
j = o_match.start(0)
226-
if q_match:
227-
k = q_match.start(0)
228-
if q_match and (not o_match or k < j): # QuotePatt matched
229-
res.append(str[i:k])
230-
res.append(str[k+1])
231-
i = k + 2
232-
else: # OctalPatt matched
233-
res.append(str[i:j])
234-
res.append(chr(int(str[j+1:j+4], 8)))
235-
i = j + 4
236-
return _nulljoin(res)
218+
return _unquote_sub(_unquote_replace, str)
237219

238220
# The _getdate() routine is used to set the expiration time in the cookie's HTTP
239221
# header. By default, _getdate() returns the current time in the appropriate

Lib/test/test_http_cookies.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Simple test suite for http/cookies.py
22

33
import copy
4-
from test.support import run_unittest, run_doctest, check_warnings
4+
from test.support import run_unittest, run_doctest, check_warnings, requires_resource
55
import unittest
66
from http import cookies
77
import pickle
@@ -67,6 +67,43 @@ def test_basic(self):
6767
for k, v in sorted(case['dict'].items()):
6868
self.assertEqual(C[k].value, v)
6969

70+
def test_unquote(self):
71+
cases = [
72+
(r'a="b=\""', 'b="'),
73+
(r'a="b=\\"', 'b=\\'),
74+
(r'a="b=\="', 'b=='),
75+
(r'a="b=\n"', 'b=n'),
76+
(r'a="b=\042"', 'b="'),
77+
(r'a="b=\134"', 'b=\\'),
78+
(r'a="b=\377"', 'b=\xff'),
79+
(r'a="b=\400"', 'b=400'),
80+
(r'a="b=\42"', 'b=42'),
81+
(r'a="b=\\042"', 'b=\\042'),
82+
(r'a="b=\\134"', 'b=\\134'),
83+
(r'a="b=\\\""', 'b=\\"'),
84+
(r'a="b=\\\042"', 'b=\\"'),
85+
(r'a="b=\134\""', 'b=\\"'),
86+
(r'a="b=\134\042"', 'b=\\"'),
87+
]
88+
for encoded, decoded in cases:
89+
with self.subTest(encoded):
90+
C = cookies.SimpleCookie()
91+
C.load(encoded)
92+
self.assertEqual(C['a'].value, decoded)
93+
94+
@requires_resource('cpu')
95+
def test_unquote_large(self):
96+
n = 10**6
97+
for encoded in r'\\', r'\134':
98+
with self.subTest(encoded):
99+
data = 'a="b=' + encoded*n + ';"'
100+
C = cookies.SimpleCookie()
101+
C.load(data)
102+
value = C['a'].value
103+
self.assertEqual(value[:3], 'b=\\')
104+
self.assertEqual(value[-2:], '\\;')
105+
self.assertEqual(len(value), n + 3)
106+
70107
def test_load(self):
71108
C = cookies.SimpleCookie()
72109
C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix quadratic complexity in parsing ``"``-quoted cookie values with backslashes by :mod:`http.cookies`.

0 commit comments

Comments
 (0)