Permalink
Browse files

Fixed #19036 -- Fixed base64 uploads decoding

Thanks anthony at adsorbtion.org for the report, and johannesl for
bringing the patch up-to-date.
  • Loading branch information...
1 parent 9e7723f commit 2a67374b51c5705d5c64a5d7117ad8552e98b4bb @claudep claudep committed Oct 11, 2012
Showing with 15 additions and 4 deletions.
  1. +6 −0 django/http/multipartparser.py
  2. +9 −4 tests/regressiontests/file_uploads/tests.py
@@ -199,6 +199,12 @@ def parse(self):
for chunk in field_stream:
if transfer_encoding == 'base64':
# We only special-case base64 transfer encoding
+ # We should always read base64 streams by multiple of 4
+ over_bytes = len(chunk) % 4
+ if over_bytes:
+ over_chunk = field_stream.read(4 - over_bytes)
+ chunk += over_chunk
+
try:
chunk = base64.b64decode(chunk)
except Exception as e:
@@ -74,15 +74,14 @@ def test_large_upload(self):
self.assertEqual(response.status_code, 200)
- def test_base64_upload(self):
- test_string = "This data will be transmitted base64-encoded."
+ def _test_base64_upload(self, content):
payload = client.FakePayload("\r\n".join([
'--' + client.BOUNDARY,
'Content-Disposition: form-data; name="file"; filename="test.txt"',
'Content-Type: application/octet-stream',
'Content-Transfer-Encoding: base64',
'',]))
- payload.write(b"\r\n" + base64.b64encode(force_bytes(test_string)) + b"\r\n")
+ payload.write(b"\r\n" + base64.b64encode(force_bytes(content)) + b"\r\n")
payload.write('--' + client.BOUNDARY + '--\r\n')
r = {
'CONTENT_LENGTH': len(payload),
@@ -94,7 +93,13 @@ def test_base64_upload(self):
response = self.client.request(**r)
received = json.loads(response.content.decode('utf-8'))
- self.assertEqual(received['file'], test_string)
+ self.assertEqual(received['file'], content)
+
+ def test_base64_upload(self):
+ self._test_base64_upload("This data will be transmitted base64-encoded.")
+
+ def test_big_base64_upload(self):
+ self._test_base64_upload("Big data" * 68000) # > 512Kb
def test_unicode_file_name(self):
tdir = sys_tempfile.mkdtemp()

0 comments on commit 2a67374

Please sign in to comment.