diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py index 526e161ae4..07a1d676ee 100644 --- a/django/http/multipartparser.py +++ b/django/http/multipartparser.py @@ -67,7 +67,10 @@ class MultiPartParser: raise MultiPartParserError('Invalid Content-Type: %s' % content_type) # Parse the header to get the boundary to split the parts. - ctypes, opts = parse_header(content_type.encode('ascii')) + try: + ctypes, opts = parse_header(content_type.encode('ascii')) + except UnicodeEncodeError: + raise MultiPartParserError('Invalid non-ASCII Content-Type in multipart: %s' % force_str(content_type)) boundary = opts.get('boundary') if not boundary or not cgi.valid_boundary(boundary): raise MultiPartParserError('Invalid boundary in multipart: %s' % force_str(boundary)) diff --git a/tests/requests/tests.py b/tests/requests/tests.py index 945d77ff6e..a97b98e8a2 100644 --- a/tests/requests/tests.py +++ b/tests/requests/tests.py @@ -487,6 +487,17 @@ class RequestsTests(SimpleTestCase): with self.assertRaisesMessage(MultiPartParserError, 'Invalid boundary in multipart: None'): request.POST + def test_multipart_non_ascii_content_type(self): + request = WSGIRequest({ + 'REQUEST_METHOD': 'POST', + 'CONTENT_TYPE': 'multipart/form-data; boundary = \xe0', + 'CONTENT_LENGTH': 0, + 'wsgi.input': FakePayload(), + }) + msg = 'Invalid non-ASCII Content-Type in multipart: multipart/form-data; boundary = à' + 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