Skip to content

Commit e84dc87

Browse files
sammiee5311jacobtylerwalls
authored andcommitted
Fixed #36931 -- Handled LookupError in multipart parser for invalid RFC 2231 encoding.
Added LookupError to the except clause so invalid headers are silently skipped, consistent with other malformed header handling.
1 parent acd0bec commit e84dc87

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

django/http/multipartparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ def parse_boundary_stream(stream, max_header_size):
726726
name = header_name.lower().rstrip(" ")
727727
value, params = parse_header_parameters(value_and_params.lstrip(" "))
728728
params = {k: v.encode() for k, v in params.items()}
729-
except ValueError: # Invalid header.
729+
except (ValueError, LookupError): # Invalid header.
730730
continue
731731

732732
if name == "content-disposition":

tests/requests_tests/tests.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,18 @@ def test_body_after_POST_multipart_form_data(self):
455455
request.body
456456

457457
def test_malformed_multipart_header(self):
458-
for header in [
459-
'Content-Disposition : form-data; name="name"',
460-
'Content-Disposition:form-data; name="name"',
461-
'Content-Disposition :form-data; name="name"',
462-
]:
458+
tests = [
459+
('Content-Disposition : form-data; name="name"', {"name": ["value"]}),
460+
('Content-Disposition:form-data; name="name"', {"name": ["value"]}),
461+
('Content-Disposition :form-data; name="name"', {"name": ["value"]}),
462+
# The invalid encoding causes the entire part to be skipped.
463+
(
464+
'Content-Disposition: form-data; name="name"; '
465+
"filename*=BOGUS''test%20file.txt",
466+
{},
467+
),
468+
]
469+
for header, expected_post in tests:
463470
with self.subTest(header):
464471
payload = FakePayload(
465472
"\r\n".join(
@@ -480,7 +487,7 @@ def test_malformed_multipart_header(self):
480487
"wsgi.input": payload,
481488
}
482489
)
483-
self.assertEqual(request.POST, {"name": ["value"]})
490+
self.assertEqual(request.POST, expected_post)
484491

485492
def test_body_after_POST_multipart_related(self):
486493
"""

0 commit comments

Comments
 (0)