Skip to content

Commit

Permalink
Fixed reserialization of message/rfc822 parts
Browse files Browse the repository at this point in the history
Also fixed DKIM and ARC signing to always enable FormatOptions.EnsureNewLine.

Fixes issue #510
  • Loading branch information
jstedfast committed Oct 2, 2019
1 parent 54953da commit b7ff8d2
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions MimeKit/Cryptography/ArcSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ async Task ArcSignAsync (FormatOptions options, MimeMessage message, IList<strin

options = options.Clone ();
options.NewLineFormat = NewLineFormat.Dos;
options.EnsureNewLine = true;

if (doAsync)
authres = await GenerateArcAuthenticationResultsAsync (options, message, cancellationToken).ConfigureAwait (false);
Expand Down
1 change: 1 addition & 0 deletions MimeKit/Cryptography/DkimSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ void DkimSign (FormatOptions options, MimeMessage message, IList<string> headers

options = options.Clone ();
options.NewLineFormat = NewLineFormat.Dos;
options.EnsureNewLine = true;

switch (SignatureAlgorithm) {
case DkimSignatureAlgorithm.Ed25519Sha256:
Expand Down
10 changes: 10 additions & 0 deletions MimeKit/MessagePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
}
}

if (options.EnsureNewLine) {
options = options.Clone ();
options.EnsureNewLine = false;
}

Message.WriteTo (options, stream, cancellationToken);
}

Expand Down Expand Up @@ -273,6 +278,11 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
await stream.WriteAsync (options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken).ConfigureAwait (false);
}

if (options.EnsureNewLine) {
options = options.Clone ();
options.EnsureNewLine = false;
}

await Message.WriteToAsync (options, stream, cancellationToken).ConfigureAwait (false);
}
}
Expand Down
77 changes: 77 additions & 0 deletions UnitTests/MimeMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,83 @@ This is the epilogue.
}
}

[Test]
public async Task TestReserializationDeliveryStatusReportWithEnsureNewLine ()
{
string rawMessageText = @"From: [email protected]
Date: Fri, 15 Feb 2019 16:00:08 +0000
Subject: report_with_no_body
To: [email protected]
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status; boundary=""A41C7.838631588=_/mm1""
Processing your mail message caused the following errors:
error: err.nosuchuser: [email protected]
--A41C7.838631588=_/mm1
Content-Type: message/delivery-status
Reporting-MTA: dns; mm1
Arrival-Date: Mon, 29 Jul 1996 02:12:50 -0700
Final-Recipient: RFC822; [email protected]
Action: failed
Diagnostic-Code: X-LOCAL; 500 (err.nosuchuser)
--A41C7.838631588=_/mm1
Content-Type: message/rfc822
Received: from urchin.netscape.com ([198.95.250.59]) by mm1.sprynet.com with ESMTP id <148217-12799>; Mon, 29 Jul 1996 02:12:50 -0700
Received: from gruntle (gruntle.mcom.com [205.217.230.10]) by urchin.netscape.com (8.7.5/8.7.3) with SMTP id CAA24688 for <[email protected]>; Mon, 29 Jul 1996 02:04:53 -0700 (PDT)
Sender: [email protected]
Message-ID: <[email protected]>
Date: Mon, 29 Jul 1996 02:04:52 -0700
From: Jamie Zawinski <[email protected]>
Organization: Netscape Communications Corporation, Mozilla Division
X-Mailer: Mozilla 3.0b6 (X11; U; IRIX 5.3 IP22)
MIME-Version: 1.0
To: [email protected]
Subject: unsubscribe
References: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
unsubscribe
--A41C7.838631588=_/mm1--
".Replace ("\r\n", "\n");

using (var source = new MemoryStream (Encoding.UTF8.GetBytes (rawMessageText))) {
var parser = new MimeParser (source, MimeFormat.Default);
var message = parser.ParseMessage ();

using (var serialized = new MemoryStream ()) {
var options = FormatOptions.Default.Clone ();
options.NewLineFormat = NewLineFormat.Unix;
options.EnsureNewLine = true;

message.WriteTo (options, serialized);

var result = Encoding.UTF8.GetString (serialized.ToArray ());

Assert.AreEqual (rawMessageText, result, "Reserialized message is not identical to the original.");
}

using (var serialized = new MemoryStream ()) {
var options = FormatOptions.Default.Clone ();
options.NewLineFormat = NewLineFormat.Unix;
options.EnsureNewLine = true;

await message.WriteToAsync (options, serialized);

var result = Encoding.UTF8.GetString (serialized.ToArray ());

Assert.AreEqual (rawMessageText, result, "Reserialized (async) message is not identical to the original.");
}
}
}

[Test]
public void TestMailMessageToMimeMessage ()
{
Expand Down

0 comments on commit b7ff8d2

Please sign in to comment.