diff --git a/starlette/formparsers.py b/starlette/formparsers.py index decaf0bfd..fd1949229 100644 --- a/starlette/formparsers.py +++ b/starlette/formparsers.py @@ -159,7 +159,7 @@ async def parse(self) -> FormData: charset = params.get(b"charset", "utf-8") if type(charset) == bytes: charset = charset.decode("latin-1") - boundary = params.get(b"boundary") + boundary = params[b"boundary"] # Callbacks dictionary. callbacks = { diff --git a/tests/test_formparsers.py b/tests/test_formparsers.py index 05f0f053c..3d4b0a199 100644 --- a/tests/test_formparsers.py +++ b/tests/test_formparsers.py @@ -1,6 +1,8 @@ import os import typing +import pytest + from starlette.formparsers import UploadFile, _user_safe_decode from starlette.requests import Request from starlette.responses import JSONResponse @@ -386,3 +388,18 @@ def test_user_safe_decode_helper(): def test_user_safe_decode_ignores_wrong_charset(): result = _user_safe_decode(b"abc", "latin-8") assert result == "abc" + + +def test_missing_boundary_parameter(test_client_factory): + client = test_client_factory(app) + with pytest.raises(KeyError, match="boundary"): + client.post( + "/", + data=( + # file + b'Content-Disposition: form-data; name="file"; filename="\xe6\x96\x87\xe6\x9b\xb8.txt"\r\n' # noqa: E501 + b"Content-Type: text/plain\r\n\r\n" + b"\r\n" + ), + headers={"Content-Type": "multipart/form-data; charset=utf-8"}, + )