Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions litestar/_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
)

from litestar.datastructures.upload_file import UploadFile
from litestar.exceptions import ClientException
from litestar.exceptions import ClientException, HTTPException, RequestEntityTooLarge
from starlette.status import HTTP_413_REQUEST_ENTITY_TOO_LARGE
from litestar.utils.compat import async_next

__all__ = ("parse_content_header", "parse_multipart_form")
raise HTTPException(
status_code=HTTP_413_REQUEST_ENTITY_TOO_LARGE,
detail="multipart request exceeds size limits",
)

from litestar.utils.compat import async_next
__all__ = ("parse_content_header", "parse_multipart_form")

if TYPE_CHECKING:
from collections.abc import AsyncGenerator
Expand Down Expand Up @@ -133,8 +138,8 @@ async def parse_multipart_form( # noqa: C901
await data.close()
await _close_upload_files(fields)

# FIXME (3.0): This should raise a '413 - Request Entity Too Large', but for
# backwards compatibility, we keep it as a 400 for now
raise ClientException("Request Entity Too Large") from None
# FIXED (3.0):Raises '413 - Request Entity Too Large' when upload exceeds the configured limit
# Note: Previously returned 400 for backward compatibility; now correctly raises 413
raise RequestEntityTooLarge("Request Entity Too Large") from None

return {k: v if len(v) > 1 else v[0] for k, v in fields.items()}
19 changes: 19 additions & 0 deletions tests/unit/test_multipart_413.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest
from litestar import Litestar, post
from starlette.status import HTTP_413_REQUEST_ENTITY_TOO_LARGE
from litestar.testing import create_test_client

@post("/upload")
async def upload_handler(data):
return {"success": True}

@pytest.mark.anyio
async def test_oversized_file_returns_413():
app = Litestar(
route_handlers=[upload_handler],
multipart_form_part_limit=10, # small limit to trigger the error
)

async with create_test_client(app) as client:
response = await client.post("/upload", data={"file": "x" * 100})
assert response.status_code == HTTP_413_REQUEST_ENTITY_TOO_LARGE
Loading