jakartaee / mail-api

Jakarta Mail Specification project
https://jakartaee.github.io/mail-api
Other
240 stars 100 forks source link

MimeMessage.saveChanges() inserts new line between body part boundaries, breaking DKIM body hash. #731

Closed dharley-gaggle closed 1 month ago

dharley-gaggle commented 1 month ago

Describe the bug We are a "man in the middle" processing emails (basically just adding a couple of our headers and then calling saveChanges() as per the javadocs), and then forwarding the email along. For customers that are using gmail, we are failing DKIM checks because saveChanges() causes a new line to be inserted between body part boundaries (gmail doesn't put them there, for wahtever reason), which causes DKIM validation to fail because the body hash has been modified.

To Reproduce Steps to reproduce the behavior:

  1. Go to gmail.
  2. Create an email with an attached file.
  3. Send to other account.
  4. Download and view original of received email from other receiver account, and notice that there is no new line between body part boundaries.
  5. Use jakarta MimeMessage API to load the message, set a new header value, call saveChanges on the message, write just-saved message to a file.
  6. Notice that the email now has a new line added between the body part boundaries, which modifies the body content.

Expected behavior The message should not modify the body at all when saveChanges() called.

Screenshots <redacted, due to stupidity>

Desktop (please complete the following information):

Mail server:

Additional context We were initially on jakarta mail 1.6.5, but have verified same erroneous behavior with the latest 1.6.7 release.

dharley-gaggle commented 1 month ago

I am in the process of creating a java-based test case to replicate this issue, and will attach to this ticket when complete.

dharley-gaggle commented 1 month ago

lol, built my simple client...and the problem didn't occur. so, i debugged extensively and it turned out the problem was occurring because the multipart was being cached, so fixed by setting -Dmail.mime.cachemultipart=false everywhere necessary.