Skip to content

Commit 7e21093

Browse files
suresh-thelkarSuresh Thelkar
andauthored
Patch CVE-2024-49767 in python-werkzeug (#10948)
Co-authored-by: Suresh Thelkar <sthelkar@microsoft.com>
1 parent 4f0ad92 commit 7e21093

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
From f73af277c1be8adfe429eacef2132785557e448f Mon Sep 17 00:00:00 2001
2+
From: Suresh Thelkar <sthelkar@microsoft.com>
3+
Date: Tue, 5 Nov 2024 12:24:50 +0530
4+
Subject: [PATCH] Patch for CVE-2024-49767
5+
6+
Upstream patch details are given below.
7+
https://github.com/pallets/werkzeug/commit/50cfeebcb0727e18cc52ffbeb125f4a66551179b#diff-ff3c479edefad986d2fe6fe7ead575a46b086e3bbcf0ccc86d85efc4a4c63c79
8+
---
9+
src/werkzeug/formparser.py | 11 +++++++++++
10+
src/werkzeug/sansio/multipart.py | 2 ++
11+
tests/test_formparser.py | 12 ++++++++++++
12+
3 files changed, 25 insertions(+)
13+
14+
diff --git a/src/werkzeug/formparser.py b/src/werkzeug/formparser.py
15+
index ba84721..d961bdb 100644
16+
--- a/src/werkzeug/formparser.py
17+
+++ b/src/werkzeug/formparser.py
18+
@@ -356,6 +356,7 @@ class MultiPartParser:
19+
self, stream: t.IO[bytes], boundary: bytes, content_length: int | None
20+
) -> tuple[MultiDict[str, str], MultiDict[str, FileStorage]]:
21+
current_part: Field | File
22+
+ field_size: int | None = None
23+
container: t.IO[bytes] | list[bytes]
24+
_write: t.Callable[[bytes], t.Any]
25+
26+
@@ -374,13 +375,23 @@ class MultiPartParser:
27+
while not isinstance(event, (Epilogue, NeedData)):
28+
if isinstance(event, Field):
29+
current_part = event
30+
+ field_size = 0
31+
container = []
32+
_write = container.append
33+
elif isinstance(event, File):
34+
current_part = event
35+
+ field_size = None
36+
container = self.start_file_streaming(event, content_length)
37+
_write = container.write
38+
elif isinstance(event, Data):
39+
+ if self.max_form_memory_size is not None and field_size is not None:
40+
+ # Ensure that accumulated data events do not exceed limit.
41+
+ # Also checked within single event in MultipartDecoder.
42+
+ field_size += len(event.data)
43+
+
44+
+ if field_size > self.max_form_memory_size:
45+
+ raise RequestEntityTooLarge()
46+
+
47+
_write(event.data)
48+
if not event.more_data:
49+
if isinstance(current_part, Field):
50+
diff --git a/src/werkzeug/sansio/multipart.py b/src/werkzeug/sansio/multipart.py
51+
index fc87353..731be03 100644
52+
--- a/src/werkzeug/sansio/multipart.py
53+
+++ b/src/werkzeug/sansio/multipart.py
54+
@@ -140,6 +140,8 @@ class MultipartDecoder:
55+
self.max_form_memory_size is not None
56+
and len(self.buffer) + len(data) > self.max_form_memory_size
57+
):
58+
+ # Ensure that data within single event does not exceed limit.
59+
+ # Also checked across accumulated events in MultiPartParser.
60+
raise RequestEntityTooLarge()
61+
else:
62+
self.buffer.extend(data)
63+
diff --git a/tests/test_formparser.py b/tests/test_formparser.py
64+
index 1ecb012..0fe152a 100644
65+
--- a/tests/test_formparser.py
66+
+++ b/tests/test_formparser.py
67+
@@ -448,3 +448,15 @@ class TestMultiPartParser:
68+
) as request:
69+
assert request.files["rfc2231"].filename == "a b c d e f.txt"
70+
assert request.files["rfc2231"].read() == b"file contents"
71+
+
72+
+
73+
+def test_multipart_max_form_memory_size() -> None:
74+
+ """max_form_memory_size is tracked across multiple data events."""
75+
+ data = b"--bound\r\nContent-Disposition: form-field; name=a\r\n\r\n"
76+
+ data += b"a" * 15 + b"\r\n--bound--"
77+
+ # The buffer size is less than the max size, so multiple data events will be
78+
+ # returned. The field size is greater than the max.
79+
+ parser = formparser.MultiPartParser(max_form_memory_size=10, buffer_size=5)
80+
+
81+
+ with pytest.raises(RequestEntityTooLarge):
82+
+ parser.parse(io.BytesIO(data), b"bound", None)
83+
--
84+
2.34.1
85+

SPECS/python-werkzeug/python-werkzeug.spec

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Summary: The Swiss Army knife of Python web development
55
Name: python-werkzeug
66
Version: 3.0.3
7-
Release: 1%{?dist}
7+
Release: 2%{?dist}
88
License: BSD
99
Vendor: Microsoft Corporation
1010
Distribution: Azure Linux
@@ -14,6 +14,7 @@ Source0: https://github.com/pallets/werkzeug/archive/%{version}.tar.gz#/w
1414
# Fixes PYTHONPATH handling in tests
1515
# Upstream: https://github.com/pallets/werkzeug/pull/2172
1616
Patch0: preserve-any-existing-PYTHONPATH-in-tests.patch
17+
Patch1: CVE-2024-49767.patch
1718
BuildArch: noarch
1819

1920
%description
@@ -78,6 +79,9 @@ pip3 install markupsafe
7879
%files -n python3-werkzeug-doc
7980

8081
%changelog
82+
* Tue Nov 05 2024 Suresh Thelkar <sthelkar@microsoft.com> - 3.0.3-2
83+
- Patch CVE-2024-49767
84+
8185
* Thu May 30 2024 Neha Agarwal <nehaagarwal@microsoft.com> - 3.0.3-1
8286
- Update to version 3.0.3 to fix CVE-2024-34069.
8387

0 commit comments

Comments
 (0)