papnkukn / eml-format

RFC 822 EML file format parser and builder
MIT License
84 stars 51 forks source link

Multipart messages with Content-Transfer-Encoding missing in the body headers causes parsing to fail #21

Closed saiorama closed 3 years ago

saiorama commented 4 years ago

I received an email today with Content-Transfer-Encoding = 8bit set in the email headers but not inside each section of the multipart message. I have attached a screenshot to show the issue. Happy to share the actual eml file with you separately.

Due to this, eml-format _read method fails on line 505 because the encoding value is undefined.

The error message is as follows:

TypeError: Cannot read property 'startsWith' of undefined
    at _append (/opt/nodejs/node_modules/eml-format/lib/eml-format.js:505:49)
    at _read (/opt/nodejs/node_modules/eml-format/lib/eml-format.js:598:15)
    at /opt/nodejs/node_modules/eml-format/lib/eml-format.js:631:7
    at Object.emlformat.parse (/opt/nodejs/node_modules/eml-format/lib/eml-format.js:667:5)
    at Object.emlformat.read (/opt/nodejs/node_modules/eml-format/lib/eml-format.js:628:15)
    at /var/task/index.js:12:15
    at new Promise (<anonymous>)
    at processEml (/var/task/index.js:11:10)
    at /var/task/index.js:42:20
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

To resolve this, I believe you can simply change line 490 of lib/eml-format.js as follows. I'd be happy to send a PR if this seems right to you.

var encoding = headers["Content-Transfer-Encoding"] || result.headers['Content-Transfer-Encoding'];

Screenshot of eml file with

papnkukn commented 3 years ago

Accepted and merged. Thanks!