A platform for community discussion. Free, open, simple.
FIX: Add nil guards to webhook signature validation
Jake Goldsboroughabb64e8b113f
diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rbindex acd118650e8..763c31d07aa 100644--- a/app/controllers/webhooks_controller.rb+++ b/app/controllers/webhooks_controller.rb@@ -38,7 +38,7 @@ class WebhooksController < ActionController::Base# so we set the error code to 5.1.2 which translates to permanent failure bad destination system address.error_code = "5.1.2" if !error_code && event["type"] == "blocked"- if error_code[Email::SMTP_STATUS_TRANSIENT_FAILURE]+ if error_code&.[](Email::SMTP_STATUS_TRANSIENT_FAILURE)process_bounce(message_id, to_address, SiteSetting.soft_bounce_score, error_code)elseprocess_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)@@ -241,7 +241,7 @@ class WebhooksController < ActionController::Base# only handle soft bounces, because hard bounces are also handled# by the "dropped" event and we don't want to increase bounce score twice# for the same message- if event == "bounced" && params["error"][Email::SMTP_STATUS_TRANSIENT_FAILURE]+ if event == "bounced" && params["error"]&.[](Email::SMTP_STATUS_TRANSIENT_FAILURE)process_bounce(message_id, to_address, SiteSetting.soft_bounce_score, error_code)elsif event == "dropped"process_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)@@ -280,6 +280,8 @@ class WebhooksController < ActionController::Basedef valid_sendgrid_signature?signature = request.headers["X-Twilio-Email-Event-Webhook-Signature"]timestamp = request.headers["X-Twilio-Email-Event-Webhook-Timestamp"]+ return false if signature.blank? || timestamp.blank?+request.body.rewindpayload = request.body.read@@ -297,11 +299,13 @@ class WebhooksController < ActionController::Baseenddef valid_mailjet_token?+ return false if params[:t].blank?ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.mailjet_webhook_token)enddef valid_mandrill_signature?signature = request.headers["X-Mandrill-Signature"]+ return false if signature.blank?payload = "#{Discourse.base_url}/webhooks/mandrill"params@@ -322,10 +326,12 @@ class WebhooksController < ActionController::Baseenddef valid_postmark_token?+ return false if params[:t].blank?ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.postmark_webhook_token)enddef valid_sparkpost_token?+ return false if params[:t].blank?ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.sparkpost_webhook_token)enddiff --git a/spec/requests/webhooks_controller_spec.rb b/spec/requests/webhooks_controller_spec.rbindex a4076d367e4..bd4a52ec0ee 100644--- a/spec/requests/webhooks_controller_spec.rb+++ b/spec/requests/webhooks_controller_spec.rb@@ -197,6 +197,35 @@ RSpec.describe WebhooksController doexpect(response.status).to eq(406)end++ it "returns error if signature header is missing" do+ SiteSetting.sendgrid_verification_key =+ "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="++ post "/webhooks/sendgrid.json",+ headers: {+ "X-Twilio-Email-Event-Webhook-Timestamp" => "1600112492",+ },+ params:+ "[{\"email\":\"[email protected]\",\"event\":\"dropped\",\"reason\":\"Bounced Address\",\"sg_event_id\":\"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA\",\"sg_message_id\":\"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0\",\"smtp-id\":\"<[email protected]>\",\"timestamp\":1600112492}]\r\n"++ expect(response.status).to eq(406)+ end++ it "returns error if timestamp header is missing" do+ SiteSetting.sendgrid_verification_key =+ "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="++ post "/webhooks/sendgrid.json",+ headers: {+ "X-Twilio-Email-Event-Webhook-Signature" =>+ "MEUCIGHQVtGj+Y3LkG9fLcxf3qfI10QysgDWmMOVmxG0u6ZUAiEAyBiXDWzM+uOe5W0JuG+luQAbPIqHh89M15TluLtEZtM=",+ },+ params:+ "[{\"email\":\"[email protected]\",\"event\":\"dropped\",\"reason\":\"Bounced Address\",\"sg_event_id\":\"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA\",\"sg_message_id\":\"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0\",\"smtp-id\":\"<[email protected]>\",\"timestamp\":1600112492}]\r\n"++ expect(response.status).to eq(406)+ endenddescribe "#mailjet" do@@ -253,6 +282,23 @@ RSpec.describe WebhooksController doexpect(response.status).to eq(406)expect(email_log.reload.bounced).to eq(false)end++ it "returns error if token param is missing" do+ SiteSetting.mailjet_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)++ post "/webhooks/mailjet.json",+ params: {+ "event" => "bounce",+ "email" => email,+ "hard_bounce" => true,+ "CustomID" => message_id,+ }++ expect(response.status).to eq(406)+ expect(email_log.reload.bounced).to eq(false)+ endenddescribe "#mailpace" do@@ -367,6 +413,14 @@ RSpec.describe WebhooksController doexpect(response.status).to eq(406)end++ it "returns error if signature header is missing" do+ SiteSetting.mandrill_authentication_key = "wr_JeJNO9OI65RFDrvk3Zw"++ post "/webhooks/mandrill.json", params: payload++ expect(response.status).to eq(406)+ endenddescribe "#mandrill_head" do@@ -445,6 +499,22 @@ RSpec.describe WebhooksController doexpect(response.status).to eq(406)expect(email_log.reload.bounced).to eq(false)end++ it "returns error if token param is missing" do+ SiteSetting.postmark_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)++ post "/webhooks/postmark.json",+ params: {+ "Type" => "HardBounce",+ "MessageID" => message_id,+ "Email" => email,+ }++ expect(response.status).to eq(406)+ expect(email_log.reload.bounced).to eq(false)+ endenddescribe "#sparkpost" do@@ -530,6 +600,33 @@ RSpec.describe WebhooksController doexpect(response.status).to eq(406)expect(email_log.reload.bounced).to eq(false)end++ it "returns error if token param is missing" do+ SiteSetting.sparkpost_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)++ post "/webhooks/sparkpost.json",+ params: {+ "_json" => [+ {+ "msys" => {+ "message_event" => {+ "bounce_class" => 10,+ "error_code" => "554",+ "rcpt_to" => email,+ "rcpt_meta" => {+ "message_id" => message_id,+ },+ },+ },+ },+ ],+ }++ expect(response.status).to eq(406)+ expect(email_log.reload.bounced).to eq(false)+ endenddescribe "#aws" do