From 49b470b9187b6be60e573fed08c8f4a87f133750 Mon Sep 17 00:00:00 2001 From: Mehrdad Date: Fri, 3 Jun 2022 01:30:24 -0400 Subject: [PATCH] Refs #33697 -- Made MultiPartParser use django.utils.http.parse_header_parameters() for parsing Content-Type header. --- django/http/multipartparser.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py index 26fb2bc41f..e70875a8df 100644 --- a/django/http/multipartparser.py +++ b/django/http/multipartparser.py @@ -19,6 +19,7 @@ from django.core.exceptions import ( from django.core.files.uploadhandler import SkipFile, StopFutureHandlers, StopUpload from django.utils.datastructures import MultiValueDict from django.utils.encoding import force_str +from django.utils.http import parse_header_parameters from django.utils.regex_helper import _lazy_re_compile __all__ = ("MultiPartParser", "MultiPartParserError", "InputStreamExhausted") @@ -49,7 +50,7 @@ class MultiPartParser: and returns a tuple of ``(MultiValueDict(POST), MultiValueDict(FILES))``. """ - boundary_re = _lazy_re_compile(rb"[ -~]{0,200}[!-~]") + boundary_re = _lazy_re_compile(r"[ -~]{0,200}[!-~]") def __init__(self, META, input_data, upload_handlers, encoding=None): """ @@ -70,14 +71,16 @@ class MultiPartParser: if not content_type.startswith("multipart/"): raise MultiPartParserError("Invalid Content-Type: %s" % content_type) - # Parse the header to get the boundary to split the parts. try: - ctypes, opts = parse_header(content_type.encode("ascii")) + content_type.encode("ascii") except UnicodeEncodeError: raise MultiPartParserError( "Invalid non-ASCII Content-Type in multipart: %s" % force_str(content_type) ) + + # Parse the header to get the boundary to split the parts. + _, opts = parse_header_parameters(content_type) boundary = opts.get("boundary") if not boundary or not self.boundary_re.fullmatch(boundary): raise MultiPartParserError( @@ -95,9 +98,7 @@ class MultiPartParser: # This means we shouldn't continue...raise an error. raise MultiPartParserError("Invalid content length: %r" % content_length) - if isinstance(boundary, str): - boundary = boundary.encode("ascii") - self._boundary = boundary + self._boundary = boundary.encode("ascii") self._input_data = input_data # For compatibility with low-level network APIs (with 32-bit integers),