Skip to content

Commit

Permalink
Disregard whitespace between leading boundary hyphens and marker
Browse files Browse the repository at this point in the history
The way I read the RFC is that whitespace is not allowed before the
boundary marker, only afterwards, so the checks for leading WS are
removed, and the missing check for trailing WS is added.

See RFC 2046 §5.1.1: """The boundary delimiter line is then defined
as a line consisting entirely of two hyphen characters ("-", decimal
value 45) followed by the boundary parameter value from the
Content-Type header field, optional linear whitespace, and a
terminating CRLF."""
  • Loading branch information
jengelh committed Oct 5, 2019
1 parent c9119ef commit df32418
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
35 changes: 8 additions & 27 deletions src/vmime/body.cpp
Expand Up @@ -78,30 +78,12 @@ size_t body::findNextBoundaryPosition(
continue;
}

// Skip transport padding bytes (SPACE or HTAB), if any
size_t advance = 0;

while (pos != 0) {

parser->seek(pos - advance - 1);

const byte_t c = parser->peekByte();

if (c == ' ' || c == '\t') {
++advance;
} else {
break;
}
}

// Ensure the bytes before boundary are "[LF]--": boundary should be
// at the beginning of a line, and should start with "--"
if (pos - advance < 3) {
if (pos < 3) {
continue;
}

parser->seek(pos - advance - 3);

parser->seek(pos - 3);
if (!parser->matchBytes("\n--", 3)) {
continue;
}
Expand All @@ -111,21 +93,20 @@ size_t body::findNextBoundaryPosition(
const byte_t next = parser->peekByte();

// Boundary should be followed by a new line or a dash
if (next != '\r' && next != '\n' && next != '-') {
if (!isspace(next) && next != '-') {
continue;
}

// Get rid of the "[CR]" just before "[LF]--", if any
if (pos - advance >= 4) {

parser->seek(pos - advance - 4);

size_t backwards = 0;
if (pos >= 4) {
parser->seek(pos - 4);
if (parser->peekByte() == '\r') {
advance++;
++backwards;
}
}

*boundaryStart = pos - advance - 3;
*boundaryStart = pos - backwards - 3;
*boundaryEnd = pos + boundary.length();

return pos;
Expand Down
6 changes: 3 additions & 3 deletions tests/parser/bodyPartTest.cpp
Expand Up @@ -213,9 +213,9 @@ VMIME_TEST_SUITE_BEGIN(bodyPartTest)
vmime::string str =
"Content-Type: multipart/mixed; boundary=\"MY-BOUNDARY\""
"\r\n\r\n"
"-- \t MY-BOUNDARY\r\nHEADER1\r\n\r\nBODY1\r\n"
"--MY-BOUNDARY \t \r\nHEADER1\r\n\r\nBODY1\r\n"
"--MY-BOUNDARY\r\n"
"-- MY-BOUNDARY--\r\n";
"--MY-BOUNDARY-- \r\n";

vmime::bodyPart p;
p.parse(str);
Expand Down Expand Up @@ -291,7 +291,7 @@ VMIME_TEST_SUITE_BEGIN(bodyPartTest)
vmime::string str =
"Content-Type: multipart/mixed"
"\r\n\r\n"
"-- \t UNKNOWN-BOUNDARY\r\nHEADER1\r\n\r\nBODY1\r\n"
"--UNKNOWN-BOUNDARY \t \r\nHEADER1\r\n\r\nBODY1\r\n"
"--UNKNOWN-BOUNDARY\r\nHEADER2\r\n\r\nBODY2\r\n"
"--UNKNOWN-BOUNDARY--";

Expand Down

0 comments on commit df32418

Please sign in to comment.