Commit 189b72f2 authored by Arkadiusz Hiler's avatar Arkadiusz Hiler

Don't mangle \n and \r combinations in the generated mboxes

Generated mboxes weren't representing newline (aka LF or \n) and
carriage return (aka CR or \r) found in the original patch faithfully.

This is mostly due to how email.Message is implemented - it tries to
normalize newlines in plain text emails. This doesn't work for us as we
want to provide the patches in an unchanged form.

To do that we have to overwrite as_string() and as_bytes() methods on
our Message class that will use modified Generator that doesn't mangle

The difference in the checksums is due to slightly different header
wrapping caused by the changes in generator.

Additionally tests are provided to make sure that we ingest emails
correctly (no mangling there) and that the generated mbox is also

Fixes: #33Signed-off-by: default avatarArkadiusz Hiler <>
parent fdb77357
Pipeline #172701 passed with stage
in 1 minute and 24 seconds
This diff is collapsed.
......@@ -406,3 +406,17 @@ class MboxEncoding(TestCase):
response = self.client.get('/patch/%d/mbox/' %
'Content-Transfer-Encoding: 8bit')
def testCarriageReturn(self):
sequence = 'nr\n\r rn\r\n r\r n\n'
patch = self.create_patch("a", sequence)
response = self.client.get('/patch/%d/mbox/' %
m = email.message_from_string(response.content.decode())
self.assertTrue(sequence.encode() in m.get_payload(decode=True))
def testCarriageReturnBase64(self):
sequence = 'nr\n\r rn\r\n r\r n\n'
patch = self.create_patch("a", ("y" * 250) + sequence)
response = self.client.get('/patch/%d/mbox/' %
m = email.message_from_string(response.content.decode())
self.assertTrue(sequence.encode() in m.get_payload(decode=True))
......@@ -341,6 +341,22 @@ class MultiPartEmailHandling(TestCase):
class CharacterTest(TestCase):
fixtures = ['default_states', 'default_events']
def setUp(self):
self.p1 = Project(linkname='test-project-1', name='Project 1',
listid='', listemail='')
def testCarriageReturn(self):
mail = read_mail('0014-all-ascii.mbox',
patch = Patch.objects.get()
self.assertTrue('13:\r\n' in patch.content)
class MultipleProjectPatchTest(TestCase):
""" Test that patches sent to multiple patchwork projects are
handled correctly """
......@@ -251,13 +251,13 @@ class APITest(APITestBase):
def testSeriesMbox(self):
self.check_mbox("/series/%s/revisions/1/mbox/" %,
def testMboxWithCoverLetter(self):
self.check_mbox("/series/%s/revisions/1/mbox_with_cover/" %,
def testCoverLetterMbox(self):
self.check_mbox("/series/%s/revisions/1/cover/" %
......@@ -21,10 +21,12 @@ from __future__ import absolute_import
import datetime
import time
from io import BytesIO
from email.encoders import encode_7or8bit, encode_base64
from email.header import Header
from email.mime.nonmultipart import MIMENonMultipart
from email.parser import HeaderParser
from email.generator import BytesGenerator
import email.utils
import re
......@@ -177,6 +179,23 @@ def process_multiplepatch_form(form, user, action, patches, context):
return errors
class NewlineNeutralGenerator(BytesGenerator):
"""Alternative generator that doesn't try to normalize newlines.
It's meant to be used by PatchMbox to accurately represent any CR and LF
combination found in the original email.
XXX: this may be prone to breakages as Python is getting upgraded to a new
release. We are overwriting _write_lines() here.
def _write_lines(self, lines):
# We have to transform the line endings.
if not lines:
class PatchMbox(MIMENonMultipart):
patch_charset = 'utf-8'
......@@ -190,6 +209,16 @@ class PatchMbox(MIMENonMultipart):
def as_string(self, unixfrom=False, maxheaderlen=0, policy=None):
return self.as_bytes(unixfrom=unixfrom, policy=policy).decode()
def as_bytes(self, unixfrom=False, policy=None):
policy = self.policy if policy is None else policy
fp = BytesIO()
g = NewlineNeutralGenerator(fp, mangle_from_=False, policy=policy)
g.flatten(self, unixfrom=unixfrom)
return fp.getvalue()
def get_from(patch, charset):
if patch.headers:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment