diff options
| author | Standa Opichal <stanislav.opichal@rossum.ai> | 2023-11-10 17:40:24 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-11-24 12:06:54 +0100 |
| commit | 1c6e8ec4ed6d9c374161eda965160e4782c7d71e (patch) | |
| tree | 3a8b9d2ef22a94606d353455376be823919b17fa /tests | |
| parent | 5e28cd3f2cfc31bf947a747256bc036f8f64888a (diff) | |
Fixed #34968 -- Made multipart parsing of headers raise an error on too long headers.
This also allow customizing the maximum size of headers via
MAX_TOTAL_HEADER_SIZE.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/file_uploads/tests.py | 52 | ||||
| -rw-r--r-- | tests/requests_tests/tests.py | 27 |
2 files changed, 78 insertions, 1 deletions
diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py index 693efc4c62..9fdef57324 100644 --- a/tests/file_uploads/tests.py +++ b/tests/file_uploads/tests.py @@ -16,6 +16,7 @@ from django.core.files.storage import default_storage from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile from django.http.multipartparser import ( FILE, + MAX_TOTAL_HEADER_SIZE, MultiPartParser, MultiPartParserError, Parser, @@ -603,6 +604,57 @@ class FileUploadTests(TestCase): temp_path = response.json()["temp_path"] self.assertIs(os.path.exists(temp_path), False) + def test_upload_large_header_fields(self): + payload = client.FakePayload( + "\r\n".join( + [ + "--" + client.BOUNDARY, + 'Content-Disposition: form-data; name="my_file"; ' + 'filename="test.txt"', + "Content-Type: text/plain", + "X-Long-Header: %s" % ("-" * 500), + "", + "file contents", + "--" + client.BOUNDARY + "--\r\n", + ] + ), + ) + r = { + "CONTENT_LENGTH": len(payload), + "CONTENT_TYPE": client.MULTIPART_CONTENT, + "PATH_INFO": "/echo_content/", + "REQUEST_METHOD": "POST", + "wsgi.input": payload, + } + response = self.client.request(**r) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json(), {"my_file": "file contents"}) + + def test_upload_header_fields_too_large(self): + payload = client.FakePayload( + "\r\n".join( + [ + "--" + client.BOUNDARY, + 'Content-Disposition: form-data; name="my_file"; ' + 'filename="test.txt"', + "Content-Type: text/plain", + "X-Long-Header: %s" % ("-" * (MAX_TOTAL_HEADER_SIZE + 1)), + "", + "file contents", + "--" + client.BOUNDARY + "--\r\n", + ] + ), + ) + r = { + "CONTENT_LENGTH": len(payload), + "CONTENT_TYPE": client.MULTIPART_CONTENT, + "PATH_INFO": "/echo_content/", + "REQUEST_METHOD": "POST", + "wsgi.input": payload, + } + response = self.client.request(**r) + self.assertEqual(response.status_code, 400) + def test_fileupload_getlist(self): file = tempfile.NamedTemporaryFile with file() as file1, file() as file2, file() as file2a: diff --git a/tests/requests_tests/tests.py b/tests/requests_tests/tests.py index 228198ae8a..035552713c 100644 --- a/tests/requests_tests/tests.py +++ b/tests/requests_tests/tests.py @@ -11,7 +11,7 @@ from django.http import ( RawPostDataException, UnreadablePostError, ) -from django.http.multipartparser import MultiPartParserError +from django.http.multipartparser import MAX_TOTAL_HEADER_SIZE, MultiPartParserError from django.http.request import split_domain_port from django.test import RequestFactory, SimpleTestCase, override_settings from django.test.client import BOUNDARY, MULTIPART_CONTENT, FakePayload @@ -691,6 +691,31 @@ class RequestsTests(SimpleTestCase): with self.assertRaisesMessage(MultiPartParserError, msg): request.POST + def test_multipart_with_header_fields_too_large(self): + payload = FakePayload( + "\r\n".join( + [ + "--boundary", + 'Content-Disposition: form-data; name="name"', + "X-Long-Header: %s" % ("-" * (MAX_TOTAL_HEADER_SIZE + 1)), + "", + "value", + "--boundary--", + ] + ) + ) + request = WSGIRequest( + { + "REQUEST_METHOD": "POST", + "CONTENT_TYPE": "multipart/form-data; boundary=boundary", + "CONTENT_LENGTH": len(payload), + "wsgi.input": payload, + } + ) + msg = "Request max total header size exceeded." + with self.assertRaisesMessage(MultiPartParserError, msg): + request.POST + def test_POST_connection_error(self): """ If wsgi.input.read() raises an exception while trying to read() the |
