Code

Ticket #19036: 19036-1.diff

File 19036-1.diff, 2.5 KB (added by claudep, 21 months ago)

Decoding base64 by multiple of 4

Line 
1diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
2index 40aefd6..156124a 100644
3--- a/django/http/multipartparser.py
4+++ b/django/http/multipartparser.py
5@@ -199,6 +199,12 @@ class MultiPartParser(object):
6                         for chunk in field_stream:
7                             if transfer_encoding == 'base64':
8                                 # We only special-case base64 transfer encoding
9+                                # We should always read base64 streams by multiple of 4
10+                                over_bytes = len(chunk) % 4
11+                                if over_bytes:
12+                                    over_chunk = field_stream.read(4 - over_bytes)
13+                                    chunk += over_chunk
14+
15                                 try:
16                                     chunk = base64.b64decode(chunk)
17                                 except Exception as e:
18diff --git a/tests/regressiontests/file_uploads/tests.py b/tests/regressiontests/file_uploads/tests.py
19index 2a1ec7d..4ee7c18 100644
20--- a/tests/regressiontests/file_uploads/tests.py
21+++ b/tests/regressiontests/file_uploads/tests.py
22@@ -60,15 +60,14 @@ class FileUploadTests(TestCase):
23 
24         self.assertEqual(response.status_code, 200)
25 
26-    def test_base64_upload(self):
27-        test_string = "This data will be transmitted base64-encoded."
28+    def _test_base64_upload(self, content):
29         payload = "\r\n".join([
30             '--' + client.BOUNDARY,
31             'Content-Disposition: form-data; name="file"; filename="test.txt"',
32             'Content-Type: application/octet-stream',
33             'Content-Transfer-Encoding: base64',
34             '',
35-            base64.b64encode(force_bytes(test_string)).decode('ascii'),
36+            base64.b64encode(force_bytes(content)).decode('ascii'),
37             '--' + client.BOUNDARY + '--',
38             '',
39         ]).encode('utf-8')
40@@ -82,7 +81,13 @@ class FileUploadTests(TestCase):
41         response = self.client.request(**r)
42         received = json.loads(response.content.decode('utf-8'))
43 
44-        self.assertEqual(received['file'], test_string)
45+        self.assertEqual(received['file'], content)
46+
47+    def test_base64_upload(self):
48+        self._test_base64_upload("This data will be transmitted base64-encoded.")
49+
50+    def test_big_base64_upload(self):
51+        self._test_base64_upload("Big data" * 68000)  # > 512Kb
52 
53     def test_unicode_file_name(self):
54         tdir = tempfile.gettempdir()