Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise a more obvious exception when form data is missing "boundary". #1349

Merged
merged 6 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion starlette/formparsers.py
Expand Up @@ -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 = {
Expand Down
17 changes: 17 additions & 0 deletions tests/test_formparsers.py
@@ -1,6 +1,8 @@
import os
import typing

import pytest
tomchristie marked this conversation as resolved.
Show resolved Hide resolved

from starlette.formparsers import UploadFile, _user_safe_decode
from starlette.requests import Request
from starlette.responses import JSONResponse
Expand Down Expand Up @@ -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"<file content>\r\n"
),
headers={"Content-Type": "multipart/form-data; charset=utf-8"},
)