postalsys / mailauth

Command line utility and a Node.js library for email authentication
Other
127 stars 10 forks source link

Invalid relaxed DKIM body hash if the message ens with mixed whitespace and newlines #17

Closed andris9 closed 2 years ago

andris9 commented 2 years ago

DKIM relaxed body hash calculation should trim all whitespace and newlines from the end of the email. Instead it trims starting from the last line that has at least one whitespace byte

Header\r\n
\r\n
Trims correctly\r\n
\r\n
\r\n
\r\n

-->

Header\r\n
\r\n
Trims correctly\r\n
Header\r\n
\r\n
Does not trim correctly\r\n
\r\n
\t\r\n
\r\n

-->

Header\r\n
\r\n
Does not trim correctly\r\n
\r\n
titanism commented 2 years ago

Perhaps use trimNewlines.end(trimNewlines.end(str.trimEnd()).trimEnd());?

https://www.npmjs.com/package/trim-newlines https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd

andris9 commented 2 years ago

The body hash calculation operates on a stream of bytes, it does not buffer the entire body to memory, so it can not use regular string manipulation functions.

andris9 commented 2 years ago

Fixed in v2.3.4