From fbaaf6ea3d04403181de0ae79d916abbc99e7556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sat, 17 Dec 2022 08:47:22 +0100 Subject: [PATCH] fix unexpected boundary not in multipart --- src/cpp/Attachment.cpp | 61 +++++++++++++++++++----------------------- src/cpp/kazMisc.cpp | 4 +-- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/cpp/Attachment.cpp b/src/cpp/Attachment.cpp index 9d2c11f..069ac81 100644 --- a/src/cpp/Attachment.cpp +++ b/src/cpp/Attachment.cpp @@ -73,7 +73,7 @@ const regex Attachment::nameRegEx (".*name=[ \t]*((\"(\\\\.|[^\\\\\r])*\")|[^\ // boundary="_000_PAVPR10MB6792713B313048E3A259B215B2079PAVPR10MB6792EURP_" // boundary=--boundary_1351_64006126-2b0e-4a3b-98ac-4797d1634188 // boundary=--boundary_1352_7e294c9a-cfab-44a0-bfb3-7310380ac7cb; -const regex Attachment::boundaryRegEx (".*boundary=[ \t]*((\"(\\\\.|[^\\\\\r])*\")|[^\r; ]*);?.*"); +const regex Attachment::boundaryRegEx (".*boundary=[ \t]*((\"(\\\\.|[^\\\\])*\")|[^; ]*);?.*"); const regex Attachment::cidDefRegEx (".*<([^>]*)>.*"); const regex Attachment::textRegEx (".*text/("+PLAIN+"|"+HTML+").*"); const regex Attachment::multiRegEx ("\\s*multipart/(mixed|"+RELATED+"|"+ALTERNATIVE+").*"); @@ -85,6 +85,7 @@ const string Attachment::IMG_END (">"); static const string SRC_BEGIN ("SRC=\""); static const string RFC822 ("message/rfc822"); +static const string MULTIPART ("multipart/"); // ================================================================================ string @@ -182,37 +183,26 @@ Attachment::getContentType () const { const string Attachment::getAttachName () const { + static string tokens [] = {contentTypeToken, contentDispositionToken}; DEF_LOG ("Attachment::getAttachName", ""); - string result = getProp (contentTypeToken, nameRegEx); - removeQuote (result); - if (result.length ()) { - LOG ("name=: " << result); - encodedWord (result); - return result; - } - result = getProp (contentTypeToken, nameCharsetRegEx); - removeQuote (result); - if (result.length ()) { - LOG ("name*=: " << result); - charsetValue (result); - return result; + for (string token : tokens) { + string result = getProp (token, nameRegEx); + removeQuote (result); + if (result.length ()) { + LOG ("name=: " << result); + encodedWord (result); + return result; + } + result = getProp (token, nameCharsetRegEx); + removeQuote (result); + if (result.length ()) { + LOG ("name*=: " << result); + charsetValue (result); + return result; + } } // XXX il faut composer s'il y a plusieurs ligne filename*x= - result = getProp (contentDispositionToken, nameRegEx); - removeQuote (result); - if (result.length ()) { - LOG ("filename=: " << result); - encodedWord (result); - return result; - } // XXX il faut composer s'il y a plusieurs ligne filename*x*= - result = getProp (contentDispositionToken, nameCharsetRegEx); - removeQuote (result); - if (result.length ()) { - LOG ("filename*=: " << result); - charsetValue (result); - return result; - } return getUnknown (getContentType ()); } @@ -328,12 +318,15 @@ Attachment::readMime (ifstream &mbox, streamoff &curPos) { contentPos = curPos; cid = getProp (contentIDToken, cidDefRegEx); - boundary = getProp (contentTypeToken, boundaryRegEx); - removeQuote (boundary); - LOG ("boundary: " << boundary); - if (boundary.length ()) { - boundary = "--"+boundary+"--"; - boundaryMiddleSize = boundary.length () - 2; + + if (caseInsensitiveFind (getContentType (), MULTIPART) != string::npos) { + boundary = getProp (contentTypeToken, boundaryRegEx); + removeQuote (boundary); + LOG ("boundary: " << boundary); + if (boundary.length ()) { + boundary = "--"+boundary+"--"; + boundaryMiddleSize = boundary.length () - 2; + } } LOG ("readMime contentPos: " << contentPos << " cid: " << cid << " boundary: " << boundary); } diff --git a/src/cpp/kazMisc.cpp b/src/cpp/kazMisc.cpp index 9982afa..ae31a52 100644 --- a/src/cpp/kazMisc.cpp +++ b/src/cpp/kazMisc.cpp @@ -186,13 +186,13 @@ kaz::quotedDecode (string &content) { } if (p+1 < content.end () && *(p+1) == '\n') { - LOG_BUG (q == content.begin (), ++p;continue, "kazMisc::quotedDecode bug: bad quoted-printable format. (start with '=', content: " << content << ")"); + LOG_BUG (q == content.begin (), ++p;continue, "kazMisc::quotedDecode bug: bad quoted-printable format. (start with '=', delim: " << int (delim) << " content: " << content << ")"); ++p; --q; continue; } - LOG_BUG (p+3 > content.end () || !isxdigit (p[1]) || !isxdigit (p[2]), return, "kazMisc::quotedDecode bug: bad quoted-printable format. (content: " << content << ")"); + LOG_BUG (p+3 > content.end () || !isxdigit (p[1]) || !isxdigit (p[2]), return, "kazMisc::quotedDecode bug: bad quoted-printable format. (delim: " << int (delim) << " content: " << content << ")"); *q = (char) ((getHexaVal (p[1]) << 4) + getHexaVal (p[2])); p += 2; }