Skip to content

Commit

Permalink
fix(MailMessage): read hangs on missing final multipart boundary #2401
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-f committed Jun 22, 2022
1 parent 1e3bc35 commit 47f2c35
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Net/src/MessageHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ void MessageHeader::read(std::istream& istr)
add(name, decodeWord(value));
++fields;
}
istr.putback(ch);
if (istr.good() && ch != eof)
istr.putback(ch);
}


Expand Down
1 change: 1 addition & 0 deletions Net/src/MultipartReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ int MultipartStreamBuf::readFromDevice(char* buffer, std::streamsize length)
*buffer++ = (char) buf.sbumpc(); ++n;
ch = buf.sgetc();
}
if (ch == eof) _lastPart = true;
return n;
}

Expand Down
52 changes: 52 additions & 0 deletions Net/testsuite/src/MailMessageTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include "Poco/Timestamp.h"
#include "Poco/FileStream.h"
#include "Poco/String.h"
#include "Poco/TemporaryFile.h"
#include <sstream>
#include <fstream>
#include <vector>


Expand All @@ -36,6 +38,7 @@ using Poco::Timestamp;
using Poco::FileInputStream;
using Poco::replaceInPlace;
using Poco::icompare;
using Poco::TemporaryFile;


namespace
Expand Down Expand Up @@ -506,6 +509,54 @@ void MailMessageTest::testReadMultiPartDefaultTransferEncoding()
}


void MailMessageTest::testReadMultiPartNoFinalBoundaryFromFile()
{
std::string data(
"Content-Type: multipart/mixed; boundary=MIME_boundary_01234567\r\n"
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
"From: poco@appinf.com\r\n"
"Mime-Version: 1.0\r\n"
"Subject: Test Message\r\n"
"To: John Doe <john.doe@no.where>\r\n"
"\r\n"
"\r\n"
"--MIME_boundary_01234567\r\n"
"Content-Disposition: inline\r\n"
"Content-Transfer-Encoding: 8bit\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"Hello World!\r\n"
"\r\n"
"--MIME_boundary_01234567\r\n"
"Content-Disposition: attachment; filename=sample.dat\r\n"
"Content-Transfer-Encoding: base64\r\n"
"Content-Type: application/octet-stream; name=sample\r\n"
"\r\n"
"VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n"
);

// The problem occurs during reading message from file.
// (For stringstreams it works fine.)
// https://github.com/pocoproject/poco/issues/2401
TemporaryFile file;
std::ofstream(file.path()) << data;
std::ifstream istr(file.path());

StringPartHandler handler;
MailMessage message;
message.read(istr, handler);

assertTrue (handler.data().size() == 2);
assertTrue (handler.data()[0] == "Hello World!\r\n");
assertTrue (handler.type()[0] == "text/plain");
assertTrue (handler.disp()[0] == "inline");

assertTrue (handler.data()[1] == "This is some binary data. Really.");
assertTrue (handler.type()[1] == "application/octet-stream; name=sample");
assertTrue (handler.disp()[1] == "attachment; filename=sample.dat");
}


void MailMessageTest::testReadWriteMultiPart()
{
std::string msgin(
Expand Down Expand Up @@ -657,6 +708,7 @@ CppUnit::Test* MailMessageTest::suite()
CppUnit_addTest(pSuite, MailMessageTest, testRead8Bit);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPartDefaultTransferEncoding);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPartNoFinalBoundaryFromFile);
CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPartStore);
CppUnit_addTest(pSuite, MailMessageTest, testEncodeWord);
Expand Down
1 change: 1 addition & 0 deletions Net/testsuite/src/MailMessageTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MailMessageTest: public CppUnit::TestCase
void testReadMultiPart();
void testReadMultiPartWithAttachmentNames();
void testReadMultiPartDefaultTransferEncoding();
void testReadMultiPartNoFinalBoundaryFromFile();
void testEncodeWord();

void setUp();
Expand Down

0 comments on commit 47f2c35

Please sign in to comment.