From 781948a89676738532841414fb2fc5b9ae2f20b9 Mon Sep 17 00:00:00 2001 From: Anton Kasimov Date: Mon, 29 Nov 2021 20:39:29 +0300 Subject: [PATCH 1/4] Raise KeyError at early stage for missing boundary --- starlette/formparsers.py | 2 +- tests/test_formparsers.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/starlette/formparsers.py b/starlette/formparsers.py index 1614a9d69..ea76fa4fe 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 8a1174e1d..cbb9083d0 100644 --- a/tests/test_formparsers.py +++ b/tests/test_formparsers.py @@ -1,5 +1,6 @@ import os +import pytest from starlette.formparsers import UploadFile, _user_safe_decode from starlette.requests import Request from starlette.responses import JSONResponse @@ -329,3 +330,20 @@ 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') as exc: + 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" + }, + ) From 54d89af28b8a3dad3f83e21aa6cdef871caf8c6d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 3 Feb 2022 11:23:24 +0000 Subject: [PATCH 2/4] Linting fix. --- tests/test_formparsers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_formparsers.py b/tests/test_formparsers.py index 8fb7b1193..56f5555c2 100644 --- a/tests/test_formparsers.py +++ b/tests/test_formparsers.py @@ -2,6 +2,7 @@ import typing import pytest + from starlette.formparsers import UploadFile, _user_safe_decode from starlette.requests import Request from starlette.responses import JSONResponse From 53bfd9f24b1d26d6146f9830ec8f9ab6691cee56 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 3 Feb 2022 11:27:11 +0000 Subject: [PATCH 3/4] Linting --- tests/test_formparsers.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_formparsers.py b/tests/test_formparsers.py index 56f5555c2..cc1bc6bc7 100644 --- a/tests/test_formparsers.py +++ b/tests/test_formparsers.py @@ -392,7 +392,7 @@ def test_user_safe_decode_ignores_wrong_charset(): def test_missing_boundary_parameter(test_client_factory): client = test_client_factory(app) - with pytest.raises(KeyError, match='boundary') as exc: + with pytest.raises(KeyError, match="boundary") as exc: client.post( "/", data=( @@ -401,7 +401,5 @@ def test_missing_boundary_parameter(test_client_factory): b"Content-Type: text/plain\r\n\r\n" b"\r\n" ), - headers={ - "Content-Type": "multipart/form-data; charset=utf-8" - }, + headers={"Content-Type": "multipart/form-data; charset=utf-8"}, ) From cc0daefee73f667daca58954fd98d870d9ee4232 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 3 Feb 2022 11:31:26 +0000 Subject: [PATCH 4/4] Linting --- tests/test_formparsers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_formparsers.py b/tests/test_formparsers.py index cc1bc6bc7..3d4b0a199 100644 --- a/tests/test_formparsers.py +++ b/tests/test_formparsers.py @@ -392,7 +392,7 @@ def test_user_safe_decode_ignores_wrong_charset(): def test_missing_boundary_parameter(test_client_factory): client = test_client_factory(app) - with pytest.raises(KeyError, match="boundary") as exc: + with pytest.raises(KeyError, match="boundary"): client.post( "/", data=(