Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #19036 -- Fixed base64 uploads decoding

Thanks anthony at adsorbtion.org for the report, and johannesl for
bringing the patch up-to-date.
Backport of 2a67374 from master.
  • Loading branch information...
commit fc379b48655a365f0dd51880a1f68a15811f903a 1 parent 1b6e751
Claude Paroz authored October 11, 2012
6  django/http/multipartparser.py
@@ -199,6 +199,12 @@ def parse(self):
199 199
                         for chunk in field_stream:
200 200
                             if transfer_encoding == 'base64':
201 201
                                 # We only special-case base64 transfer encoding
  202
+                                # We should always read base64 streams by multiple of 4
  203
+                                over_bytes = len(chunk) % 4
  204
+                                if over_bytes:
  205
+                                    over_chunk = field_stream.read(4 - over_bytes)
  206
+                                    chunk += over_chunk
  207
+
202 208
                                 try:
203 209
                                     chunk = base64.b64decode(chunk)
204 210
                                 except Exception as e:
13  tests/regressiontests/file_uploads/tests.py
@@ -74,15 +74,14 @@ def test_large_upload(self):
74 74
 
75 75
         self.assertEqual(response.status_code, 200)
76 76
 
77  
-    def test_base64_upload(self):
78  
-        test_string = "This data will be transmitted base64-encoded."
  77
+    def _test_base64_upload(self, content):
79 78
         payload = client.FakePayload("\r\n".join([
80 79
             '--' + client.BOUNDARY,
81 80
             'Content-Disposition: form-data; name="file"; filename="test.txt"',
82 81
             'Content-Type: application/octet-stream',
83 82
             'Content-Transfer-Encoding: base64',
84 83
             '',]))
85  
-        payload.write(b"\r\n" + base64.b64encode(force_bytes(test_string)) + b"\r\n")
  84
+        payload.write(b"\r\n" + base64.b64encode(force_bytes(content)) + b"\r\n")
86 85
         payload.write('--' + client.BOUNDARY + '--\r\n')
87 86
         r = {
88 87
             'CONTENT_LENGTH': len(payload),
@@ -94,7 +93,13 @@ def test_base64_upload(self):
94 93
         response = self.client.request(**r)
95 94
         received = json.loads(response.content.decode('utf-8'))
96 95
 
97  
-        self.assertEqual(received['file'], test_string)
  96
+        self.assertEqual(received['file'], content)
  97
+
  98
+    def test_base64_upload(self):
  99
+        self._test_base64_upload("This data will be transmitted base64-encoded.")
  100
+
  101
+    def test_big_base64_upload(self):
  102
+        self._test_base64_upload("Big data" * 68000)  # > 512Kb
98 103
 
99 104
     def test_unicode_file_name(self):
100 105
         tdir = sys_tempfile.mkdtemp()

0 notes on commit fc379b4

Please sign in to comment.
Something went wrong with that request. Please try again.