]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Raise KeyError at early stage for missing boundary (#1349)
authorАнтон Касимов <kasimov.an@gmail.com>
Thu, 3 Feb 2022 11:34:25 +0000 (14:34 +0300)
committerGitHub <noreply@github.com>
Thu, 3 Feb 2022 11:34:25 +0000 (11:34 +0000)
* Raise KeyError at early stage for missing boundary

* Linting fix.

* Linting

* Linting

Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>
Co-authored-by: Tom Christie <tom@tomchristie.com>
starlette/formparsers.py
tests/test_formparsers.py

index decaf0bfd041135c0568809a58415e8294a60c39..fd19492293750629e35ca58d7e6eb6eeb8674ae1 100644 (file)
@@ -159,7 +159,7 @@ class MultiPartParser:
         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 = {
index 05f0f053c8d0a2dbfdfdc2ee4a8fd99c2ce67675..3d4b0a199314bc1b0ee54da84fce945b9b440419 100644 (file)
@@ -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"<file content>\r\n"
+            ),
+            headers={"Content-Type": "multipart/form-data; charset=utf-8"},
+        )