From 47bc7751e7deb47946cf033bb3f1d3e6254e0459 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Wed, 24 Oct 2018 16:47:03 -0400 Subject: [PATCH] Throw a ParserException in ParseEntity() when the stream does not have headers Fixes issue #443 --- MimeKit/AsyncMimeParser.cs | 2 +- MimeKit/MimeParser.cs | 19 +++++++++++++------ UnitTests/TestData/mbox/jwz.mbox.txt | 1 - 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/MimeKit/AsyncMimeParser.cs b/MimeKit/AsyncMimeParser.cs index fc205b5404..083faceac7 100644 --- a/MimeKit/AsyncMimeParser.cs +++ b/MimeKit/AsyncMimeParser.cs @@ -323,7 +323,7 @@ async Task ConstructMessagePartAsync (MessagePart part, Cancellati } // parse the headers... - state = MimeParserState.Headers; + state = MimeParserState.MessageHeaders; if (await StepAsync (cancellationToken).ConfigureAwait (false) == MimeParserState.Error) { // Note: this either means that StepHeaders() found the end of the stream // or an invalid header field name at the start of the message headers, diff --git a/MimeKit/MimeParser.cs b/MimeKit/MimeParser.cs index 1098b747cb..5aa1df6c04 100644 --- a/MimeKit/MimeParser.cs +++ b/MimeKit/MimeParser.cs @@ -779,10 +779,17 @@ unsafe bool StepHeaders (byte* inbuf, ref bool scanningFieldName, ref bool check return false; } - if (state == MimeParserState.MessageHeaders && headers.Count == 0) { - // ignore From-lines that might appear at the start of a message - if (length < 5 || !IsMboxMarker (start, true)) { - // not a From-line... + if (headers.Count == 0) { + if (state == MimeParserState.MessageHeaders) { + // ignore From-lines that might appear at the start of a message + if (length < 5 || !IsMboxMarker (start, true)) { + // not a From-line... + inputIndex = (int) (start - inbuf); + state = MimeParserState.Error; + headerIndex = 0; + return false; + } + } else if (state == MimeParserState.Headers) { inputIndex = (int) (start - inbuf); state = MimeParserState.Error; headerIndex = 0; @@ -832,7 +839,7 @@ unsafe bool StepHeaders (byte* inbuf, ref bool scanningFieldName, ref bool check length = (inptr + 1) - start; - if (!valid && headers.Count == 0 && length > 5 && IsMboxMarker (start, true)) { + if (!valid && headers.Count == 0 && length >= 5 && IsMboxMarker (start, true)) { if (inptr[-1] == (byte) '\r') length--; length--; @@ -1279,7 +1286,7 @@ unsafe BoundaryType ConstructMessagePart (MessagePart part, byte* inbuf, Cancell } // parse the headers... - state = MimeParserState.Headers; + state = MimeParserState.MessageHeaders; if (Step (inbuf, cancellationToken) == MimeParserState.Error) { // Note: this either means that StepHeaders() found the end of the stream // or an invalid header field name at the start of the message headers, diff --git a/UnitTests/TestData/mbox/jwz.mbox.txt b/UnitTests/TestData/mbox/jwz.mbox.txt index 93edf33748..d3ab425e8d 100644 --- a/UnitTests/TestData/mbox/jwz.mbox.txt +++ b/UnitTests/TestData/mbox/jwz.mbox.txt @@ -6595,7 +6595,6 @@ a --PART.BOUNDARY.2418.16114.COSMOS.VLSI.CS.CMU.EDU.715029153.2 Content-type: message/rfc822 -26-Aug-92 22:15:02-LCL,22076;000000000000 Received: from po3.andrew.cmu.edu by COSMOS.VLSI.CS.CMU.EDU id aa13358; 26 Aug 92 22:14:26 EDT Received: from sqhilton.pc.cs.cmu.edu by po3.andrew.cmu.edu (5.54/3.15) id for beatty@cosmos.vlsi.cs.cmu.edu; Wed, 26 Aug 92 22:14:07 EDT