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 MultiPartException when missing "name" field on Content-Disposition header #1643

Merged
merged 1 commit into from
May 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ filterwarnings=
error
ignore: run_until_first_complete is deprecated and will be removed in a future version.:DeprecationWarning
ignore: starlette\.middleware\.wsgi is deprecated and will be removed in a future release\.*:DeprecationWarning
ignore: Async generator 'starlette\.requests\.Request\.stream' was garbage collected before it had been exhausted.*:ResourceWarning

[coverage:run]
source_pkgs = starlette, tests
Expand Down
8 changes: 7 additions & 1 deletion starlette/formparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ async def parse(self) -> FormData:
header_value = b""
elif message_type == MultiPartMessage.HEADERS_FINISHED:
disposition, options = parse_options_header(content_disposition)
field_name = _user_safe_decode(options[b"name"], charset)
try:
field_name = _user_safe_decode(options[b"name"], charset)
except KeyError:
raise MultiPartException(
'The Content-Disposition header field "name" must be '
"provided."
)
if b"filename" in options:
filename = _user_safe_decode(options[b"filename"], charset)
file = UploadFile(
Expand Down
32 changes: 32 additions & 0 deletions tests/test_formparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,35 @@ def test_missing_boundary_parameter(
)
assert res.status_code == 400
assert res.text == "Missing boundary in multipart."


@pytest.mark.parametrize(
"app,expectation",
[
(app, pytest.raises(MultiPartException)),
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
],
)
def test_missing_name_parameter_on_content_disposition(
app, expectation, test_client_factory: typing.Callable[..., TestClient]
):
client = test_client_factory(app)
with expectation:
res = client.post(
"/",
data=(
# data
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n"
b'Content-Disposition: form-data; ="field0"\r\n\r\n'
b"value0\r\n"
),
headers={
"Content-Type": (
"multipart/form-data; boundary=a7f7ac8d4e2e437c877bb7b8d7cc549c"
)
},
)
assert res.status_code == 400
assert (
res.text == 'The Content-Disposition header field "name" must be provided.'
)