diff options
Diffstat (limited to 'meta/recipes-devtools/python/python3/CVE-2024-7592.patch')
-rw-r--r-- | meta/recipes-devtools/python/python3/CVE-2024-7592.patch | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/meta/recipes-devtools/python/python3/CVE-2024-7592.patch b/meta/recipes-devtools/python/python3/CVE-2024-7592.patch new file mode 100644 index 0000000000..7a6d63005c --- /dev/null +++ b/meta/recipes-devtools/python/python3/CVE-2024-7592.patch | |||
@@ -0,0 +1,143 @@ | |||
1 | From dcc3eaef98cd94d6cb6cb0f44bd1c903d04f33b1 Mon Sep 17 00:00:00 2001 | ||
2 | From: "Miss Islington (bot)" | ||
3 | <31488909+miss-islington@users.noreply.github.com> | ||
4 | Date: Sun, 25 Aug 2024 00:37:11 +0200 | ||
5 | Subject: [PATCH] gh-123067: Fix quadratic complexity in parsing "-quoted | ||
6 | cookie values with backslashes (GH-123075) (#123104) | ||
7 | |||
8 | gh-123067: Fix quadratic complexity in parsing "-quoted cookie values with backslashes (GH-123075) | ||
9 | |||
10 | This fixes CVE-2024-7592. | ||
11 | (cherry picked from commit 44e458357fca05ca0ae2658d62c8c595b048b5ef) | ||
12 | |||
13 | Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> | ||
14 | |||
15 | CVE: CVE-2024-7592 | ||
16 | |||
17 | Upstream-Status: Backport [https://github.com/python/cpython/commit/dcc3eaef98cd94d6cb6cb0f44bd1c903d04f33b1] | ||
18 | |||
19 | Signed-off-by: Soumya Sambu <soumya.sambu@windriver.com> | ||
20 | --- | ||
21 | Lib/http/cookies.py | 34 ++++------------- | ||
22 | Lib/test/test_http_cookies.py | 38 +++++++++++++++++++ | ||
23 | ...-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst | 1 + | ||
24 | 3 files changed, 47 insertions(+), 26 deletions(-) | ||
25 | create mode 100644 Misc/NEWS.d/next/Library/2024-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst | ||
26 | |||
27 | diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py | ||
28 | index 35ac2dc..2c1f021 100644 | ||
29 | --- a/Lib/http/cookies.py | ||
30 | +++ b/Lib/http/cookies.py | ||
31 | @@ -184,8 +184,13 @@ def _quote(str): | ||
32 | return '"' + str.translate(_Translator) + '"' | ||
33 | |||
34 | |||
35 | -_OctalPatt = re.compile(r"\\[0-3][0-7][0-7]") | ||
36 | -_QuotePatt = re.compile(r"[\\].") | ||
37 | +_unquote_sub = re.compile(r'\\(?:([0-3][0-7][0-7])|(.))').sub | ||
38 | + | ||
39 | +def _unquote_replace(m): | ||
40 | + if m[1]: | ||
41 | + return chr(int(m[1], 8)) | ||
42 | + else: | ||
43 | + return m[2] | ||
44 | |||
45 | def _unquote(str): | ||
46 | # If there aren't any doublequotes, | ||
47 | @@ -205,30 +210,7 @@ def _unquote(str): | ||
48 | # \012 --> \n | ||
49 | # \" --> " | ||
50 | # | ||
51 | - i = 0 | ||
52 | - n = len(str) | ||
53 | - res = [] | ||
54 | - while 0 <= i < n: | ||
55 | - o_match = _OctalPatt.search(str, i) | ||
56 | - q_match = _QuotePatt.search(str, i) | ||
57 | - if not o_match and not q_match: # Neither matched | ||
58 | - res.append(str[i:]) | ||
59 | - break | ||
60 | - # else: | ||
61 | - j = k = -1 | ||
62 | - if o_match: | ||
63 | - j = o_match.start(0) | ||
64 | - if q_match: | ||
65 | - k = q_match.start(0) | ||
66 | - if q_match and (not o_match or k < j): # QuotePatt matched | ||
67 | - res.append(str[i:k]) | ||
68 | - res.append(str[k+1]) | ||
69 | - i = k + 2 | ||
70 | - else: # OctalPatt matched | ||
71 | - res.append(str[i:j]) | ||
72 | - res.append(chr(int(str[j+1:j+4], 8))) | ||
73 | - i = j + 4 | ||
74 | - return _nulljoin(res) | ||
75 | + return _unquote_sub(_unquote_replace, str) | ||
76 | |||
77 | # The _getdate() routine is used to set the expiration time in the cookie's HTTP | ||
78 | # header. By default, _getdate() returns the current time in the appropriate | ||
79 | diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py | ||
80 | index 925c869..8879902 100644 | ||
81 | --- a/Lib/test/test_http_cookies.py | ||
82 | +++ b/Lib/test/test_http_cookies.py | ||
83 | @@ -5,6 +5,7 @@ import unittest | ||
84 | import doctest | ||
85 | from http import cookies | ||
86 | import pickle | ||
87 | +from test import support | ||
88 | |||
89 | |||
90 | class CookieTests(unittest.TestCase): | ||
91 | @@ -58,6 +59,43 @@ class CookieTests(unittest.TestCase): | ||
92 | for k, v in sorted(case['dict'].items()): | ||
93 | self.assertEqual(C[k].value, v) | ||
94 | |||
95 | + def test_unquote(self): | ||
96 | + cases = [ | ||
97 | + (r'a="b=\""', 'b="'), | ||
98 | + (r'a="b=\\"', 'b=\\'), | ||
99 | + (r'a="b=\="', 'b=='), | ||
100 | + (r'a="b=\n"', 'b=n'), | ||
101 | + (r'a="b=\042"', 'b="'), | ||
102 | + (r'a="b=\134"', 'b=\\'), | ||
103 | + (r'a="b=\377"', 'b=\xff'), | ||
104 | + (r'a="b=\400"', 'b=400'), | ||
105 | + (r'a="b=\42"', 'b=42'), | ||
106 | + (r'a="b=\\042"', 'b=\\042'), | ||
107 | + (r'a="b=\\134"', 'b=\\134'), | ||
108 | + (r'a="b=\\\""', 'b=\\"'), | ||
109 | + (r'a="b=\\\042"', 'b=\\"'), | ||
110 | + (r'a="b=\134\""', 'b=\\"'), | ||
111 | + (r'a="b=\134\042"', 'b=\\"'), | ||
112 | + ] | ||
113 | + for encoded, decoded in cases: | ||
114 | + with self.subTest(encoded): | ||
115 | + C = cookies.SimpleCookie() | ||
116 | + C.load(encoded) | ||
117 | + self.assertEqual(C['a'].value, decoded) | ||
118 | + | ||
119 | + @support.requires_resource('cpu') | ||
120 | + def test_unquote_large(self): | ||
121 | + n = 10**6 | ||
122 | + for encoded in r'\\', r'\134': | ||
123 | + with self.subTest(encoded): | ||
124 | + data = 'a="b=' + encoded*n + ';"' | ||
125 | + C = cookies.SimpleCookie() | ||
126 | + C.load(data) | ||
127 | + value = C['a'].value | ||
128 | + self.assertEqual(value[:3], 'b=\\') | ||
129 | + self.assertEqual(value[-2:], '\\;') | ||
130 | + self.assertEqual(len(value), n + 3) | ||
131 | + | ||
132 | def test_load(self): | ||
133 | C = cookies.SimpleCookie() | ||
134 | C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') | ||
135 | diff --git a/Misc/NEWS.d/next/Library/2024-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst b/Misc/NEWS.d/next/Library/2024-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst | ||
136 | new file mode 100644 | ||
137 | index 0000000..6a23456 | ||
138 | --- /dev/null | ||
139 | +++ b/Misc/NEWS.d/next/Library/2024-08-16-19-13-21.gh-issue-123067.Nx9O4R.rst | ||
140 | @@ -0,0 +1 @@ | ||
141 | +Fix quadratic complexity in parsing ``"``-quoted cookie values with backslashes by :mod:`http.cookies`. | ||
142 | -- | ||
143 | 2.40.0 | ||