Repository navigation
Expand file tree
/
Copy pathCVE-2024-7592.diff
More file actions
107 lines (101 loc) · 3.47 KB
/
Copy pathCVE-2024-7592.diff
File metadata and controls
107 lines (101 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
diff --git a/Lib/Cookie.py b/Lib/Cookie.py
index 10823637..a6ba4a92 100644
--- a/Lib/Cookie.py
+++ b/Lib/Cookie.py
@@ -323,9 +323,13 @@ def _quote(str, LegalChars=_LegalChars,
return '"' + _nulljoin( map(_Translator.get, str, str) ) + '"'
# end _quote
+_unquote_sub = re.compile(r'\\(?:([0-3][0-7][0-7])|(.))').sub
-_OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
-_QuotePatt = re.compile(r"[\\].")
+def _unquote_replace(m):
+ if m.group(1):
+ return chr(int(m.group(1), 8))
+ else:
+ return m.group(2)
def _unquote(str):
# If there aren't any doublequotes,
@@ -345,28 +349,7 @@ def _unquote(str):
# \012 --> \n
# \" --> "
#
- i = 0
- n = len(str)
- res = []
- while 0 <= i < n:
- Omatch = _OctalPatt.search(str, i)
- Qmatch = _QuotePatt.search(str, i)
- if not Omatch and not Qmatch: # Neither matched
- res.append(str[i:])
- break
- # else:
- j = k = -1
- if Omatch: j = Omatch.start(0)
- if Qmatch: k = Qmatch.start(0)
- if Qmatch and ( not Omatch or k < j ): # QuotePatt matched
- res.append(str[i:k])
- res.append(str[k+1])
- i = k+2
- else: # OctalPatt matched
- res.append(str[i:j])
- res.append( chr( int(str[j+1:j+4], 8) ) )
- i = j+4
- return _nulljoin(res)
+ return _unquote_sub(_unquote_replace, str)
# end _unquote
# The _getdate() routine is used to set the expiration time in
diff --git a/Lib/test/test_cookie.py b/Lib/test/test_cookie.py
index 40419012..efd2ed3c 100644
--- a/Lib/test/test_cookie.py
+++ b/Lib/test/test_cookie.py
@@ -1,6 +1,7 @@
# Simple test suite for Cookie.py
from test.test_support import run_unittest, run_doctest, check_warnings
+from test import support
import unittest
import Cookie
import pickle
@@ -156,6 +157,44 @@ class CookieTests(unittest.TestCase):
self.assertEqual(dict(C), {})
self.assertEqual(C.output(), '')
+ 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 = Cookie.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 = Cookie.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_pickle(self):
rawdata = 'Customer="WILE_E_COYOTE"; Path=/acme; Version=1'
expected_output = 'Set-Cookie: %s' % rawdata