jhawthorn / dkim

:envelope: Pure Ruby DKIM signature library with Rails integration
93 stars 18 forks source link

dkim fail(wrong body hash) #24

Closed zephyrpathsofglory closed 4 years ago

zephyrpathsofglory commented 4 years ago

Hi there:

I use the dkim gem to sign my emails but I failed to verify, it returns: dkim=neutral (body hash did not verify) in gmail, I have test my dkim in appmaildev, the result is: wrong body hash

But when I send emails only contains text/plain Content-Type (I delete my *.html.erb file), it passed.

I guess the culprit lies in encoding but I don't have enough proof.

Additionally, I use dkim 1.0.1 and my rails is 5.1.7.

I wish to get some help on how to debug it!

carsonreinke commented 4 years ago

@zhangyonguu it is hard to tell without the specific message. It could be your MTA, it could be a lot of things. If you feel it could be the encoding, try first with just ASCII, after that try with more complicated encodings.

zephyrpathsofglory commented 4 years ago

@carsonreinke

I have a receipt_email.html.rb:

<p>
  test
</p>

and receipt_email.html.rb: test

I have debugged a lot and find that when sending multipart emails, the canonical_body that be computed in my case is:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <p>
  test
</p>
  </body>
</html>

and the body action_mailer eventually send is:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  </head>

  <body>
    <p>
  test
</p>
  </body>
</html>

My conclusion that the eventually sent body is the above is on the basis of two facts:

  1. rails print the mail message when sending emails
  2. the message rails print is completely the same with the mail that my gmail received!

So I guess that the timing that the gem computes the body hash and other signature headers is too early, the body is not the body that eventually sent.

Is this a bug? Can you give me some help?

zephyrpathsofglory commented 4 years ago

@carsonreinke

Additionally

I use dkim 1.0.1 ,rails 5.1.7, actionmailer 5.1.7.

zephyrpathsofglory commented 4 years ago

@carsonreinke

It is not a bug, it is caused by a gem named premailer-rails which registers a hook to change html style before sending emails, but unfortunately, after signing dkim. I have a dependency which depends on premailer-rails. I will close this issue.

carsonreinke commented 4 years ago

@zhangyonguu glad to hear you found it!!!