Skip to content

Commit

Permalink
Fixed a corner case in calculating relaxed DKIM-Signature body hashes
Browse files Browse the repository at this point in the history
Fixes issue #187
  • Loading branch information
jstedfast committed Nov 9, 2015
1 parent 3a99fe6 commit 5abf902
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions MimeKit/Cryptography/DkimRelaxedBodyFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public DkimRelaxedBodyFilter ()
lwsp = false;
}

unsafe int Filter (byte* inbuf, int length, byte* outbuf)
unsafe int Filter (byte* inbuf, int length, byte* outbuf, bool flush)
{
byte* inend = inbuf + length;
byte* outptr = outbuf;
Expand Down Expand Up @@ -110,6 +110,13 @@ unsafe int Filter (byte* inbuf, int length, byte* outbuf)
inptr++;
}

if (flush && lwsp) {
// collapse lwsp to a single space
*outptr++ = (byte) ' ';
lwsp = false;
count++;
}

return count;
}

Expand All @@ -133,7 +140,7 @@ protected override byte[] Filter (byte[] input, int startIndex, int length, out

unsafe {
fixed (byte* inptr = input, outptr = OutputBuffer) {
outputLength = Filter (inptr + startIndex, length, outptr);
outputLength = Filter (inptr + startIndex, length, outptr, flush);
}
}

Expand Down
17 changes: 17 additions & 0 deletions UnitTests/DkimRelaxedBodyFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,22 @@ public void TestNonEmptyBodyEndingWithMultipleNewLines ()

Assert.AreEqual (expected, actual);
}

[Test]
public void TestTrailingLineWithOnlySpaceOrTab ()
{
const string text = "Hello!\r\n\r\n ";
const string expected = "Hello!\r\n\r\n ";
var input = Encoding.ASCII.GetBytes (text);
var filter = new DkimRelaxedBodyFilter ();
int outputIndex, outputLength;
byte[] output;
string actual;

output = filter.Flush (input, 0, input.Length, out outputIndex, out outputLength);
actual = Encoding.ASCII.GetString (output, outputIndex, outputLength);

Assert.AreEqual (expected, actual);
}
}
}

0 comments on commit 5abf902

Please sign in to comment.