CVE-2024-47874
Description
Starlette is an Asynchronous Server Gateway Interface (ASGI) framework/toolkit. Prior to version 0.40.0, Starlette treats multipart/form-data parts without a filename as text form fields and buffers those in byte strings with no size limit. This allows an attacker to upload arbitrary large form fields and cause Starlette to both slow down significantly due to excessive memory allocations and copy operations, and also consume more and more memory until the server starts swapping and grinds to a halt, or the OS terminates the server process with an OOM error. Uploading multiple such requests in parallel may be enough to render a service practically unusable, even if reasonable request size limits are enforced by a reverse proxy in front of Starlette. This Denial of service (DoS) vulnerability affects all applications built with Starlette (or FastAPI) accepting form requests. Verison 0.40.0 fixes this issue.
Affected packages
Versions sourced from the GitHub Security Advisory.
| Package | Affected versions | Patched versions |
|---|---|---|
starlettePyPI | < 0.40.0 | 0.40.0 |
Patches
34ded4b7ac517fd038f3070c3fd038f3070c3Merge commit from fork
2 files changed · +45 −7
starlette/formparsers.py+7 −4 modified@@ -31,12 +31,12 @@ class FormMessage(Enum): class MultipartPart: content_disposition: bytes | None = None field_name: str = "" - data: bytes = b"" + data: bytearray = field(default_factory=bytearray) file: UploadFile | None = None item_headers: list[tuple[bytes, bytes]] = field(default_factory=list) -def _user_safe_decode(src: bytes, codec: str) -> str: +def _user_safe_decode(src: bytes | bytearray, codec: str) -> str: try: return src.decode(codec) except (UnicodeDecodeError, LookupError): @@ -117,7 +117,8 @@ async def parse(self) -> FormData: class MultiPartParser: - max_file_size = 1024 * 1024 + max_file_size = 1024 * 1024 # 1MB + max_part_size = 1024 * 1024 # 1MB def __init__( self, @@ -149,7 +150,9 @@ def on_part_begin(self) -> None: def on_part_data(self, data: bytes, start: int, end: int) -> None: message_bytes = data[start:end] if self._current_part.file is None: - self._current_part.data += message_bytes + if len(self._current_part.data) + len(message_bytes) > self.max_part_size: + raise MultiPartException(f"Part exceeded maximum size of {int(self.max_part_size / 1024)}KB.") + self._current_part.data.extend(message_bytes) else: self._file_parts_to_write.append((self._current_part, message_bytes))
tests/test_formparsers.py+38 −3 modified@@ -640,9 +640,7 @@ def test_max_files_is_customizable_low_raises( assert res.text == "Too many files. Maximum number of files is 1." -def test_max_fields_is_customizable_high( - test_client_factory: TestClientFactory, -) -> None: +def test_max_fields_is_customizable_high(test_client_factory: TestClientFactory) -> None: client = test_client_factory(make_app_max_parts(max_fields=2000, max_files=2000)) fields = [] for i in range(2000): @@ -664,3 +662,40 @@ def test_max_fields_is_customizable_high( "content": "", "content_type": None, } + + +@pytest.mark.parametrize( + "app,expectation", + [ + (app, pytest.raises(MultiPartException)), + (Starlette(routes=[Mount("/", app=app)]), does_not_raise()), + ], +) +def test_max_part_size_exceeds_limit( + app: ASGIApp, + expectation: typing.ContextManager[Exception], + test_client_factory: TestClientFactory, +) -> None: + client = test_client_factory(app) + boundary = "------------------------4K1ON9fZkj9uCUmqLHRbbR" + + multipart_data = ( + f"--{boundary}\r\n" + f'Content-Disposition: form-data; name="small"\r\n\r\n' + "small content\r\n" + f"--{boundary}\r\n" + f'Content-Disposition: form-data; name="large"\r\n\r\n' + + ("x" * 1024 * 1024 + "x") # 1MB + 1 byte of data + + "\r\n" + f"--{boundary}--\r\n" + ).encode("utf-8") + + headers = { + "Content-Type": f"multipart/form-data; boundary={boundary}", + "Transfer-Encoding": "chunked", + } + + with expectation: + response = client.post("/", data=multipart_data, headers=headers) # type: ignore + assert response.status_code == 400 + assert response.text == "Part exceeded maximum size of 1024KB."
Vulnerability mechanics
Generated by null/stub on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
4News mentions
0No linked articles in our index yet.