use proper 'Message-Id' field

This commit is contained in:
Régis Hanol 2016-06-09 00:33:13 +02:00
parent 9a81115c1c
commit 214e25f1b5
2 changed files with 7 additions and 8 deletions

View File

@ -7,7 +7,7 @@ class WebhooksController < ActionController::Base
return mailgun_failure if SiteSetting.mailgun_api_key.blank? return mailgun_failure if SiteSetting.mailgun_api_key.blank?
# token is a random string of 50 characters # token is a random string of 50 characters
token = params.delete("token") token = params["token"]
return mailgun_failure if token.blank? || token.size != 50 return mailgun_failure if token.blank? || token.size != 50
# prevent replay attack # prevent replay attack
@ -16,14 +16,14 @@ class WebhooksController < ActionController::Base
$redis.expire(key, 10.minutes) $redis.expire(key, 10.minutes)
# ensure timestamp isn't too far from current time # ensure timestamp isn't too far from current time
timestamp = params.delete("timestamp") timestamp = params["timestamp"]
return mailgun_failure if (Time.at(timestamp.to_i) - Time.now).abs > 24.hours.to_i return mailgun_failure if (Time.at(timestamp.to_i) - Time.now).abs > 24.hours.to_i
# check the signature # check the signature
return mailgun_failure unless mailgun_verify(timestamp, token, params["signature"]) return mailgun_failure unless mailgun_verify(timestamp, token, params["signature"])
event = params.delete("event") event = params["event"]
message_id = params.delete("message-id") message_id = params["Message-Id"].tr("<>", "")
# only handle soft bounces, because hard bounces are also handled # only handle soft bounces, because hard bounces are also handled
# by the "dropped" event and we don't want to increase bounce score twice # by the "dropped" event and we don't want to increase bounce score twice

View File

@ -4,13 +4,13 @@ describe WebhooksController do
before { $redis.flushall } before { $redis.flushall }
let(:email) { "em@il.com" } let(:email) { "em@il.com" }
let(:message_id) { "12345@il.com" }
context "mailgun" do context "mailgun" do
it "works" do it "works" do
SiteSetting.mailgun_api_key = "key-8221462f0c915af3f6f2e2df7aa5a493" SiteSetting.mailgun_api_key = "key-8221462f0c915af3f6f2e2df7aa5a493"
message_id = "12345@il.com"
user = Fabricate(:user, email: email) user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id) email_log = Fabricate(:email_log, user: user, message_id: message_id)
@ -19,7 +19,7 @@ describe WebhooksController do
post :mailgun, "token" => "705a8ccd2ce932be8e98c221fe701c1b4a0afcb8bbd57726de", post :mailgun, "token" => "705a8ccd2ce932be8e98c221fe701c1b4a0afcb8bbd57726de",
"timestamp" => Time.now.to_i, "timestamp" => Time.now.to_i,
"event" => "dropped", "event" => "dropped",
"message-id" => message_id "Message-Id" => "<12345@il.com>"
expect(response).to be_success expect(response).to be_success
@ -34,7 +34,7 @@ describe WebhooksController do
it "works" do it "works" do
user = Fabricate(:user, email: email) user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: "12345@il.com") email_log = Fabricate(:email_log, user: user, message_id: message_id)
post :sendgrid, "_json" => [ post :sendgrid, "_json" => [
{ {
@ -57,7 +57,6 @@ describe WebhooksController do
context "mailjet" do context "mailjet" do
it "works" do it "works" do
message_id = "12345@il.com"
user = Fabricate(:user, email: email) user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id) email_log = Fabricate(:email_log, user: user, message_id: message_id)