Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion python_multipart/decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ def write(self, data: bytes) -> int:
:param data: base64 data to decode
"""

# Line breaks and spaces may appear anywhere in Base64 (MIME wraps at
# 76 characters). Drop them before counting, otherwise a chunk
# boundary falling inside a quartet is miscounted and the decode fails.
written = len(data)
data = data.translate(None, b"\r\n \t")

# Prepend any cache info to our data.
if len(self.cache) > 0:
data = bytes(self.cache) + data
Expand All @@ -85,7 +91,7 @@ def write(self, data: bytes) -> int:
self.cache[:] = b""

# Return the length of the data to indicate no error.
return len(data)
return written

def close(self) -> None:
"""Close this decoder. If the underlying object has a `close()`
Expand Down
12 changes: 12 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import base64
import os
import random
import sys
Expand Down Expand Up @@ -692,6 +693,17 @@ def test_long_bad_split(self) -> None:
self.d.write(second)
self.assert_data(b"foobar")

def test_line_wrapped_input_any_split(self) -> None:
# MIME-style Base64 is wrapped with line breaks; the result must not
# depend on where the chunk boundaries fall.
raw = bytes(range(100))
wrapped = base64.encodebytes(raw)
for i in range(len(wrapped) + 1):
self.setUp()
self.assertEqual(self.d.write(wrapped[:i]), i)
self.d.write(wrapped[i:])
self.assert_data(raw)

def test_close_and_finalize(self) -> None:
parser = Mock()
f = Base64Decoder(parser)
Expand Down
Loading