mat-sz / letterparser

✉️ Isomorphic e-mail parser (with MIME support) written in TypeScript.
BSD 3-Clause Clear License
34 stars 4 forks source link

Base-64 parsing error #8

Closed edwh closed 3 years ago

edwh commented 3 years ago

Thanks for an interesting project.

I had trouble parsing a message with a base64-encoded attachment - it was throwing an exception about the length not being a multiple of 4. What worked for me as a quick hack was to strip line breaks from the string before decoding it.

diff --git a/node_modules/letterparser/lib/parser.js b/node_modules/letterparser/lib/parser.js
index 487ecd9..657f4ca 100644
--- a/node_modules/letterparser/lib/parser.js
+++ b/node_modules/letterparser/lib/parser.js
@@ -225,6 +225,9 @@ function parseBody(depth, lines, lineStartIdx, lineEndIdx, lookaheadBoundaryLine
                     case 'base64':
                         {
                             var decoder = new TextDecoder(parsedType.encoding);
+                            stringBody = stringBody.replace(/\r?\n|\r/, '')
                             body = decoder.decode(base64_js_1.toByteArray(stringBody));
                         }
                         break;
@@ -236,8 +239,10 @@ function parseBody(depth, lines, lineStartIdx, lineEndIdx, lookaheadBoundaryLine
             else {
                 switch (headers['Content-Transfer-Encoding'].toLowerCase()) {
                     case 'base64':
-                        body = base64_js_1.toByteArray(stringBody);
-                        break;
+                      stringBody = stringBody.replace(/\r?\n|\r/, '')
+                      body = base64_js_1.toByteArray(stringBody);
+
+                      break;
                     case 'quoted-printable':
                         body = lettercoder_1.decodeQuotedPrintable(stringBody, parsedType.encoding);
                         break;

That's probably not a full fix, so I've not created a PR, but perhaps it's helpful.

avigoldman commented 3 years ago

I've run into a similar issue. I'll try to find an EML file to replicate this issue.

mat-sz commented 3 years ago

Thank you for your report, the issue has been resolved in c683bf9 and a new version will be released soon.

@edwh @avigoldman Please reopen if the issue is still present after update.

mat-sz commented 3 years ago

Released with 0.0.8.

edwh commented 3 years ago

That seems to work for me. Thank you.