Skip to content

Commit

Permalink
Throw a ParserException in ParseEntity() when the stream does not hav…
Browse files Browse the repository at this point in the history
…e headers

Fixes issue #443
  • Loading branch information
jstedfast committed Oct 24, 2018
1 parent 5db6b89 commit 47bc775
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion MimeKit/AsyncMimeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ async Task<BoundaryType> 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,
Expand Down
19 changes: 13 additions & 6 deletions MimeKit/MimeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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--;
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion UnitTests/TestData/mbox/jwz.mbox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <AA21478> for [email protected]; Wed, 26 Aug 92 22:14:07 EDT
Expand Down

0 comments on commit 47bc775

Please sign in to comment.