From 214e25f1b5ea9c65ef485cfedef1b01fbbb724d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 9 Jun 2016 00:33:13 +0200 Subject: [PATCH] use proper 'Message-Id' field --- app/controllers/webhooks_controller.rb | 8 ++++---- spec/controllers/webhooks_controller_spec.rb | 7 +++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index f85d485da..79265ee2a 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -7,7 +7,7 @@ class WebhooksController < ActionController::Base return mailgun_failure if SiteSetting.mailgun_api_key.blank? # token is a random string of 50 characters - token = params.delete("token") + token = params["token"] return mailgun_failure if token.blank? || token.size != 50 # prevent replay attack @@ -16,14 +16,14 @@ class WebhooksController < ActionController::Base $redis.expire(key, 10.minutes) # 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 # check the signature return mailgun_failure unless mailgun_verify(timestamp, token, params["signature"]) - event = params.delete("event") - message_id = params.delete("message-id") + event = params["event"] + message_id = params["Message-Id"].tr("<>", "") # only handle soft bounces, because hard bounces are also handled # by the "dropped" event and we don't want to increase bounce score twice diff --git a/spec/controllers/webhooks_controller_spec.rb b/spec/controllers/webhooks_controller_spec.rb index 5ff1de6f8..0bf1b3611 100644 --- a/spec/controllers/webhooks_controller_spec.rb +++ b/spec/controllers/webhooks_controller_spec.rb @@ -4,13 +4,13 @@ describe WebhooksController do before { $redis.flushall } let(:email) { "em@il.com" } + let(:message_id) { "12345@il.com" } context "mailgun" do it "works" do SiteSetting.mailgun_api_key = "key-8221462f0c915af3f6f2e2df7aa5a493" - message_id = "12345@il.com" user = Fabricate(:user, email: email) email_log = Fabricate(:email_log, user: user, message_id: message_id) @@ -19,7 +19,7 @@ describe WebhooksController do post :mailgun, "token" => "705a8ccd2ce932be8e98c221fe701c1b4a0afcb8bbd57726de", "timestamp" => Time.now.to_i, "event" => "dropped", - "message-id" => message_id + "Message-Id" => "<12345@il.com>" expect(response).to be_success @@ -34,7 +34,7 @@ describe WebhooksController do it "works" do 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" => [ { @@ -57,7 +57,6 @@ describe WebhooksController do context "mailjet" do it "works" do - message_id = "12345@il.com" user = Fabricate(:user, email: email) email_log = Fabricate(:email_log, user: user, message_id: message_id)