Skip to content

A platform for community discussion. Free, open, simple.

DEV: Replace MissingMailgunApiKey check with comprehensive EmailBounceHandling (#38028)

Régis Hanole1d4b1637cff
diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb
index 763c31d07aa..60ba6178dfd 100644
--- a/app/controllers/webhooks_controller.rb
+++ b/app/controllers/webhooks_controller.rb
@@ -22,9 +22,7 @@ class WebhooksController < ActionController::Base
if SiteSetting.sendgrid_verification_key.present?
return signature_failure if !valid_sendgrid_signature?
else
- Rails.logger.warn(
- "Received a Sendgrid webhook, but no verification key has been configured. This is unsafe behaviour and will be disallowed in the future.",
- )
+ warn_unauthenticated_webhook("SendGrid", "sendgrid_verification_key")
end
events = params["_json"] || [params]
@@ -55,9 +53,7 @@ class WebhooksController < ActionController::Base
if SiteSetting.mailjet_webhook_token.present?
return signature_failure if !valid_mailjet_token?
else
- Rails.logger.warn(
- "Received a Mailjet webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",
- )
+ warn_unauthenticated_webhook("Mailjet", "mailjet_webhook_token")
end
events = params["_json"] || [params]
@@ -77,7 +73,11 @@ class WebhooksController < ActionController::Base
end
def mailpace
- # see https://docs.mailpace.com/guide/webhooks#email-events
+ if SiteSetting.mailpace_verification_key.present?
+ return signature_failure if !valid_mailpace_signature?
+ else
+ warn_unauthenticated_webhook("Mailpace", "mailpace_verification_key")
+ end
message_id = Email::MessageIdService.message_id_clean(params["payload"]["message_id"])
to_address = params["payload"]["to"]
@@ -97,9 +97,7 @@ class WebhooksController < ActionController::Base
if SiteSetting.mandrill_authentication_key.present?
return signature_failure if !valid_mandrill_signature?
else
- Rails.logger.warn(
- "Received a Mandrill webhook, but no authentication key has been configured. This is unsafe behaviour and will be disallowed in the future.",
- )
+ warn_unauthenticated_webhook("Mandrill", "mandrill_authentication_key")
end
JSON
@@ -130,18 +128,13 @@ class WebhooksController < ActionController::Base
if SiteSetting.postmark_webhook_token.present?
return signature_failure if !valid_postmark_token?
else
- Rails.logger.warn(
- "Received a Postmark webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",
- )
+ warn_unauthenticated_webhook("Postmark", "postmark_webhook_token")
end
- # see https://postmarkapp.com/developer/webhooks/bounce-webhook#bounce-webhook-data
- # and https://postmarkapp.com/developer/api/bounce-api#bounce-types
-
message_id = params["MessageID"]
to_address = params["Email"]
- type = params["Type"]
- case type
+
+ case params["Type"]
when "HardBounce", "SpamNotification", "SpamComplaint"
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
when "SoftBounce"
@@ -155,9 +148,7 @@ class WebhooksController < ActionController::Base
if SiteSetting.sparkpost_webhook_token.present?
return signature_failure if !valid_sparkpost_token?
else
- Rails.logger.warn(
- "Received a Sparkpost webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",
- )
+ warn_unauthenticated_webhook("SparkPost", "sparkpost_webhook_token")
end
events = params["_json"] || [params]
@@ -172,7 +163,6 @@ class WebhooksController < ActionController::Base
bounce_class = bounce_class.to_i
- # bounce class definitions: https://support.sparkpost.com/customer/portal/articles/1929896
if bounce_class < 80
if bounce_class == 10 || bounce_class == 25 || bounce_class == 30
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
@@ -201,6 +191,14 @@ class WebhooksController < ActionController::Base
private
+ def warn_unauthenticated_webhook(provider, setting)
+ Discourse.deprecate(
+ "Received a #{provider} webhook, but the `#{setting}` setting is not configured. Unauthenticated webhooks is unsafe behavior and will be rejected in a future version.",
+ since: "2026.2",
+ drop_from: "2026.5",
+ )
+ end
+
def signature_failure
render body: nil, status: :not_acceptable
end
@@ -210,22 +208,16 @@ class WebhooksController < ActionController::Base
end
def valid_mailgun_signature?(token, timestamp, signature)
- # token is a random 50 characters string
return false if token.blank? || token.size != 50
- # prevent replay attacks
key = "mailgun_token_#{token}"
return false unless Discourse.redis.setnx(key, 1)
Discourse.redis.expire(key, 10.minutes)
- # ensure timestamp isn't too far from current time
return false if (Time.at(timestamp.to_i) - Time.now).abs > 12.hours.to_i
- # check the signature
- ActiveSupport::SecurityUtils.secure_compare(
- signature,
- OpenSSL::HMAC.hexdigest("SHA256", SiteSetting.mailgun_api_key, "#{timestamp}#{token}"),
- )
+ digest = OpenSSL::HMAC.hexdigest("SHA256", SiteSetting.mailgun_api_key, "#{timestamp}#{token}")
+ ActiveSupport::SecurityUtils.secure_compare(signature, digest)
end
def handle_mailgun_legacy(params)
@@ -317,12 +309,8 @@ class WebhooksController < ActionController::Base
payload += value
end
- payload_signature =
- OpenSSL::HMAC.digest("sha1", SiteSetting.mandrill_authentication_key, payload)
- ActiveSupport::SecurityUtils.secure_compare(
- signature,
- Base64.strict_encode64(payload_signature),
- )
+ digest = OpenSSL::HMAC.digest("sha1", SiteSetting.mandrill_authentication_key, payload)
+ ActiveSupport::SecurityUtils.secure_compare(signature, Base64.strict_encode64(digest))
end
def valid_postmark_token?
@@ -330,6 +318,27 @@ class WebhooksController < ActionController::Base
ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.postmark_webhook_token)
end
+ def valid_mailpace_signature?
+ signature = request.headers["X-MailPace-Signature"]
+ return false if signature.blank?
+
+ request.body.rewind
+ payload = request.body.read
+
+ begin
+ decoded_key = Base64.strict_decode64(SiteSetting.mailpace_verification_key)
+ public_key = OpenSSL::PKey.read(decoded_key)
+ rescue StandardError
+ Rails.logger.error("Invalid Mailpace webhook verification key")
+ return false
+ end
+
+ decoded_signature = Base64.strict_decode64(signature)
+ public_key.verify(nil, decoded_signature, payload)
+ rescue StandardError
+ false
+ end
+
def valid_sparkpost_token?
return false if params[:t].blank?
ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.sparkpost_webhook_token)
diff --git a/app/models/problem_check.rb b/app/models/problem_check.rb
index 4e9c42908bd..dd2e4633ad9 100644
--- a/app/models/problem_check.rb
+++ b/app/models/problem_check.rb
@@ -77,7 +77,7 @@ class ProblemCheck
ProblemCheck::GroupEmailCredentials,
ProblemCheck::HostNames,
ProblemCheck::ImageMagick,
- ProblemCheck::MissingMailgunApiKey,
+ ProblemCheck::EmailBounceHandling,
ProblemCheck::OutOfDateThemes,
ProblemCheck::PollPop3Timeout,
ProblemCheck::PollPop3AuthError,
diff --git a/app/services/problem_check/email_bounce_handling.rb b/app/services/problem_check/email_bounce_handling.rb
new file mode 100644
index 00000000000..14cf68ca9f3
--- /dev/null
+++ b/app/services/problem_check/email_bounce_handling.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+class ProblemCheck::EmailBounceHandling < ProblemCheck
+ self.priority = "low"
+
+ PROVIDERS = [
+ { address: "smtp.mailgun.org", setting: :mailgun_api_key, name: "Mailgun" },
+ { address: "smtp.sendgrid.net", setting: :sendgrid_verification_key, name: "SendGrid" },
+ { address: /\.mailjet\.com\z/, setting: :mailjet_webhook_token, name: "Mailjet" },
+ { address: "smtp.mandrillapp.com", setting: :mandrill_authentication_key, name: "Mandrill" },
+ { address: "smtp.postmarkapp.com", setting: :postmark_webhook_token, name: "Postmark" },
+ { address: "smtp.sparkpostmail.com", setting: :sparkpost_webhook_token, name: "SparkPost" },
+ { address: "smtp.mailpace.com", setting: :mailpace_verification_key, name: "Mailpace" },
+ { address: /email-smtp\..+\.amazonaws\.com\z/, name: "AWS SES" },
+ ].freeze
+
+ def call
+ if SiteSetting.reply_by_email_enabled && Email::Sender.bounceable_reply_address?
+ return no_problem
+ end
+
+ smtp_address = ActionMailer::Base.smtp_settings[:address].to_s
+
+ if provider = PROVIDERS.find { |p| p[:address] === smtp_address }
+ return no_problem if provider[:setting].blank?
+ return no_problem if SiteSetting.public_send(provider[:setting]).present?
+
+ problem(
+ override_key: "dashboard.problem.email_bounce_handling.webhook_key_missing",
+ override_data: {
+ provider: provider[:name],
+ setting: provider[:setting],
+ },
+ )
+ else
+ problem(override_key: "dashboard.problem.email_bounce_handling.no_bounce_handling")
+ end
+ end
+end
diff --git a/app/services/problem_check/missing_mailgun_api_key.rb b/app/services/problem_check/missing_mailgun_api_key.rb
deleted file mode 100644
index 4e399faefb1..00000000000
--- a/app/services/problem_check/missing_mailgun_api_key.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-# frozen_string_literal: true
-
-class ProblemCheck::MissingMailgunApiKey < ProblemCheck
- self.priority = "low"
-
- def call
- return no_problem if !SiteSetting.reply_by_email_enabled
- return no_problem if ActionMailer::Base.smtp_settings[:address] != "smtp.mailgun.org"
- return no_problem if SiteSetting.mailgun_api_key.present?
-
- problem
- end
-end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 15bf5e6c880..f4637221361 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1710,7 +1710,9 @@ en:
email_polling_errored_recently:
one: "Email polling has generated an error in the past 24 hours. Look at <a href='%{base_path}/logs' target='_blank'>the logs</a> for more details."
other: "Email polling has generated %{count} errors in the past 24 hours. Look at <a href='%{base_path}/logs' target='_blank'>the logs</a> for more details."
- missing_mailgun_api_key: "The server is configured to send emails via Mailgun but you haven't provided an API key used to verify the webhook messages."
+ email_bounce_handling:
+ webhook_key_missing: "The server sends emails via %{provider} but the <code>%{setting}</code> setting is not configured. Without it, bounce webhooks will be rejected and bounced emails won't be tracked. Configure it in <a href='%{base_path}/admin/site_settings'>Site Settings</a>."
+ no_bounce_handling: "No bounce handling detected. Configure reply by email with VERP support, or set up bounce webhooks with a supported email provider. Without bounce handling, you won't be notified of email delivery failures."
qq_mail_smtp: "Your SMTP server (%{smtp_address}) is known to cause duplicate emails with Discourse. QQ Mail does not return proper acknowledgments, causing Discourse to retry sends that already succeeded. <a href='https://github.com/discourse/discourse/blob/main/docs/INSTALL-email.md' target='_blank'>See recommended alternatives</a>."
bad_favicon_url: "The favicon is failing to load. Check your favicon setting in <a href='%{base_path}/admin/site_settings'>Site Settings</a>."
poll_pop3_timeout: "Connection to the POP3 server is timing out. Incoming email could not be retrieved. Please check your <a href='%{base_path}/admin/site_settings/category/email'>POP3 settings</a> and service provider."
@@ -2343,6 +2345,7 @@ en:
mandrill_authentication_key: "Mandrill authentication key used to verify webhook messages."
postmark_webhook_token: "Token used to verify webhook payload. It must be passed as the 't' query parameter of the webhook, for example: https://example.com/webhook/postmark?t=supersecret"
sparkpost_webhook_token: "Token used to verify webhook payload. It must be passed as the 't' query parameter of the webhook, for example: https://example.com/webhook/sparkpost?t=supersecret"
+ mailpace_verification_key: "Verification key used to verify MailPace webhook messages."
soft_bounce_score: "Bounce score added to the user when a temporary bounce happens."
hard_bounce_score: "Bounce score added to the user when a permanent bounce happens."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 25f0ac0594e..c210b0058cc 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2019,6 +2019,9 @@ email:
sparkpost_webhook_token:
default: ""
secret: true
+ mailpace_verification_key:
+ default: ""
+ secret: true
bounce_score_threshold:
client: true
default: 4
diff --git a/db/migrate/20260224152846_remove_stale_missing_mailgun_api_key_problem_check_tracker.rb b/db/migrate/20260224152846_remove_stale_missing_mailgun_api_key_problem_check_tracker.rb
new file mode 100644
index 00000000000..5e988a26dd7
--- /dev/null
+++ b/db/migrate/20260224152846_remove_stale_missing_mailgun_api_key_problem_check_tracker.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+class RemoveStaleMissingMailgunApiKeyProblemCheckTracker < ActiveRecord::Migration[8.0]
+ def up
+ execute "DELETE FROM problem_check_trackers WHERE identifier = 'missing_mailgun_api_key'"
+ end
+
+ def down
+ # no-op: can't restore deleted tracker rows
+ end
+end
diff --git a/spec/requests/webhooks_controller_spec.rb b/spec/requests/webhooks_controller_spec.rb
index bd4a52ec0ee..f03b606f9a4 100644
--- a/spec/requests/webhooks_controller_spec.rb
+++ b/spec/requests/webhooks_controller_spec.rb
@@ -5,6 +5,19 @@ RSpec.describe WebhooksController do
fab!(:email) { "[email protected]" }
fab!(:message_id) { "[email protected]" }
+ fab!(:user) { Fabricate(:user, email:) }
+ fab!(:email_log) { Fabricate(:email_log, user:, message_id:, to_address: email) }
+
+ def expect_bounce(score:, error_code: nil)
+ email_log.reload
+ expect(email_log.bounced).to eq(true)
+ expect(email_log.bounce_error_code).to eq(error_code)
+ expect(email_log.user.user_stat.bounce_score).to eq(score)
+ end
+
+ def expect_no_bounce
+ expect(email_log.reload.bounced).to eq(false)
+ end
describe "#mailgun" do
let(:token) { "705a8ccd2ce932be8e98c221fe701c1b4a0afcb8bbd57726de" }
@@ -14,15 +27,28 @@ RSpec.describe WebhooksController do
before do
SiteSetting.mailgun_api_key = "key-8221462f0c915af3f6f2e2df7aa5a493"
- ActionController::Base.allow_forgery_protection = true # Ensure the endpoint works, even with CSRF protection generally enabled
+ ActionController::Base.allow_forgery_protection = true
end
after { ActionController::Base.allow_forgery_protection = false }
- it "works (deprecated)" do
- user = Fabricate(:user, email: email)
- email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+ it "returns 406 when API key is missing" do
+ SiteSetting.mailgun_api_key = ""
+
+ post "/webhooks/mailgun.json",
+ params: {
+ "token" => token,
+ "timestamp" => timestamp,
+ "event" => "dropped",
+ "recipient" => email,
+ "Message-Id" => "<#{message_id}>",
+ "signature" => signature,
+ }
+
+ expect(response.status).to eq(406)
+ end
+ it "processes legacy dropped events as hard bounces" do
post "/webhooks/mailgun.json",
params: {
"token" => token,
@@ -36,17 +62,27 @@ RSpec.describe WebhooksController do
}
expect(response.status).to eq(200)
-
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq("5.1.1")
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
+ expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")
end
- it "works (new)" do
- user = Fabricate(:user, email: email)
- email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+ it "processes legacy transient bounces as soft bounces" do
+ post "/webhooks/mailgun.json",
+ params: {
+ "token" => token,
+ "timestamp" => timestamp,
+ "event" => "bounced",
+ "recipient" => email,
+ "Message-Id" => "<#{message_id}>",
+ "signature" => signature,
+ "error" => "smtp; 4.7.1 Temporary failure",
+ "code" => "4.7.1",
+ }
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.soft_bounce_score, error_code: "4.7.1")
+ end
+
+ it "processes new format temporary failures as soft bounces" do
post "/webhooks/mailgun.json",
params: {
"signature" => {
@@ -65,58 +101,75 @@ RSpec.describe WebhooksController do
},
},
"delivery-status" => {
- "message" =>
- "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",
- "code" => "5.1.1",
- "description" => "",
+ "code" => "4.7.1",
},
}
expect(response.status).to eq(200)
-
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq("5.1.1")
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)
+ expect_bounce(score: SiteSetting.soft_bounce_score, error_code: "4.7.1")
end
- context "when readonly mode is enabled" do
- before { Discourse.enable_readonly_mode }
-
- it "returns 503" do
- user = Fabricate(:user, email:)
- email_log = Fabricate(:email_log, user:, message_id:, to_address: email)
-
- post "/webhooks/mailgun.json",
- params: {
+ it "processes new format permanent failures as hard bounces" do
+ post "/webhooks/mailgun.json",
+ params: {
+ "signature" => {
"token" => token,
"timestamp" => timestamp,
- "event" => "dropped",
- "recipient" => email,
- "Message-Id" => "<#{message_id}>",
"signature" => signature,
- "error" =>
- "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",
+ },
+ "event-data" => {
+ "event" => "failed",
+ "severity" => "permanent",
+ "recipient" => email,
+ "message" => {
+ "headers" => {
+ "message-id" => message_id,
+ },
+ },
+ },
+ "delivery-status" => {
"code" => "5.1.1",
- }
+ },
+ }
- expect(response.status).to eq(503)
- expect(email_log.reload.bounced).to eq(false)
- end
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")
+ end
+
+ it "returns 503 in readonly mode" do
+ Discourse.enable_readonly_mode
+
+ post "/webhooks/mailgun.json",
+ params: {
+ "token" => token,
+ "timestamp" => timestamp,
+ "event" => "dropped",
+ "recipient" => email,
+ "Message-Id" => "<#{message_id}>",
+ "signature" => signature,
+ "code" => "5.1.1",
+ }
+
+ expect(response.status).to eq(503)
+ expect_no_bounce
end
end
describe "#sendgrid" do
- fab!(:user) { Fabricate(:user, email:) }
- fab!(:email_log) { Fabricate(:email_log, user:, message_id: message_id, to_address: email) }
+ before do
+ SiteSetting.sendgrid_verification_key = "key"
+ WebhooksController.any_instance.stubs(:valid_sendgrid_signature?).returns(true)
+ end
+
+ it "processes webhooks with a deprecation warning when verification key is missing" do
+ SiteSetting.sendgrid_verification_key = ""
- it "works" do
post "/webhooks/sendgrid.json",
params: {
"_json" => [
{
"email" => email,
- "smtp-id" => "<[email protected]>",
+ "smtp-id" => "<#{message_id}>",
"event" => "bounce",
"status" => "5.0.0",
},
@@ -124,508 +177,396 @@ RSpec.describe WebhooksController do
}
expect(response.status).to eq(200)
-
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq("5.0.0")
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
+ expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.0.0")
end
- it "sets the bounce error code to 5.1.2 when payload's `event` is `bounce`, `type` is `blocked` and `status` is blank" do
+ it "returns 406 when signature is invalid" do
+ WebhooksController.any_instance.stubs(:valid_sendgrid_signature?).returns(false)
+
post "/webhooks/sendgrid.json",
params: {
"_json" => [
{
"email" => email,
- "smtp-id" => "<[email protected]>",
+ "smtp-id" => "<#{message_id}>",
"event" => "bounce",
- "type" => "blocked",
+ "status" => "5.0.0",
},
],
}
- expect(response.status).to eq(200)
-
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq("5.1.2")
+ expect(response.status).to eq(406)
end
- it "verifies signatures" do
- SiteSetting.sendgrid_verification_key =
- "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="
-
+ it "processes hard bounces" do
post "/webhooks/sendgrid.json",
- headers: {
- "X-Twilio-Email-Event-Webhook-Signature" =>
- "MEUCIGHQVtGj+Y3LkG9fLcxf3qfI10QysgDWmMOVmxG0u6ZUAiEAyBiXDWzM+uOe5W0JuG+luQAbPIqHh89M15TluLtEZtM=",
- "X-Twilio-Email-Event-Webhook-Timestamp" => "1600112502",
- },
- 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"
+ params: {
+ "_json" => [
+ {
+ "email" => email,
+ "smtp-id" => "<#{message_id}>",
+ "event" => "bounce",
+ "status" => "5.0.0",
+ },
+ ],
+ }
expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.0.0")
end
- it "returns error if signature verification fails" do
- SiteSetting.sendgrid_verification_key =
- "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="
-
+ it "processes soft bounces with transient failure status" do
post "/webhooks/sendgrid.json",
- headers: {
- "X-Twilio-Email-Event-Webhook-Signature" =>
- "MEUCIQCtIHJeH93Y+qpYeWrySphQgpNGNr/U+UyUlBkU6n7RAwIgJTz2C+8a8xonZGi6BpSzoQsbVRamr2nlxFDWYNH3j/0=",
- "X-Twilio-Email-Event-Webhook-Timestamp" => "1600112502",
- },
- 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 signature is invalid" do
- SiteSetting.sendgrid_verification_key = "foo"
-
- post "/webhooks/sendgrid.json",
- headers: {
- "X-Twilio-Email-Event-Webhook-Signature" =>
- "MEUCIQCtIHJeH93Y+qpYeWrySphQgpNGNr/U+UyUlBkU6n7RAwIgJTz2C+8a8xonZGi6BpSzoQsbVRamr2nlxFDWYNH3j/0=",
- "X-Twilio-Email-Event-Webhook-Timestamp" => "1600112502",
- },
- 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"
+ params: {
+ "_json" => [
+ {
+ "email" => email,
+ "smtp-id" => "<#{message_id}>",
+ "event" => "bounce",
+ "status" => "4.0.0",
+ },
+ ],
+ }
- expect(response.status).to eq(406)
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.soft_bounce_score, error_code: "4.0.0")
end
- it "returns error if signature header is missing" do
- SiteSetting.sendgrid_verification_key =
- "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="
-
+ it "processes dropped events as hard bounces" do
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"
+ params: {
+ "_json" => [
+ {
+ "email" => email,
+ "smtp-id" => "<#{message_id}>",
+ "event" => "dropped",
+ "status" => "5.0.0",
+ },
+ ],
+ }
- expect(response.status).to eq(406)
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.0.0")
end
- it "returns error if timestamp header is missing" do
- SiteSetting.sendgrid_verification_key =
- "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="
-
+ it "defaults to error code 5.1.2 for blocked bounces without status" do
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"
+ params: {
+ "_json" => [
+ {
+ "email" => email,
+ "smtp-id" => "<#{message_id}>",
+ "event" => "bounce",
+ "type" => "blocked",
+ },
+ ],
+ }
- expect(response.status).to eq(406)
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.2")
end
end
describe "#mailjet" do
- it "works" do
- user = Fabricate(:user, email: email)
- email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+ let(:bounce_params) do
+ { "event" => "bounce", "email" => email, "hard_bounce" => true, "CustomID" => message_id }
+ end
- post "/webhooks/mailjet.json",
- params: {
- "event" => "bounce",
- "email" => email,
- "hard_bounce" => true,
- "CustomID" => message_id,
- }
+ it "processes webhooks with a deprecation warning when webhook token is missing" do
+ post "/webhooks/mailjet.json", params: bounce_params
expect(response.status).to eq(200)
-
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq(nil) # mailjet doesn't give us this
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
+ expect_bounce(score: SiteSetting.hard_bounce_score)
end
- it "verifies signatures" 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)
+ context "with a valid webhook token" do
+ before { SiteSetting.mailjet_webhook_token = "foo" }
- post "/webhooks/mailjet.json?t=foo",
- params: {
- "event" => "bounce",
- "email" => email,
- "hard_bounce" => true,
- "CustomID" => message_id,
- }
+ it "processes hard bounces" do
+ post "/webhooks/mailjet.json?t=foo", params: bounce_params
- expect(response.status).to eq(200)
- expect(email_log.reload.bounced).to eq(true)
- end
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score)
+ end
- it "returns error if signature verification fails" 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)
+ it "processes soft bounces" do
+ post "/webhooks/mailjet.json?t=foo",
+ params: [
+ {
+ "event" => "bounce",
+ "email" => email,
+ "hard_bounce" => false,
+ "CustomID" => message_id,
+ },
+ ].to_json,
+ headers: {
+ "CONTENT_TYPE" => "application/json",
+ }
- post "/webhooks/mailjet.json?t=bar",
- params: {
- "event" => "bounce",
- "email" => email,
- "hard_bounce" => true,
- "CustomID" => message_id,
- }
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.soft_bounce_score)
+ end
- expect(response.status).to eq(406)
- expect(email_log.reload.bounced).to eq(false)
- end
+ it "rejects wrong token" do
+ post "/webhooks/mailjet.json?t=bar", params: bounce_params
- 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)
+ expect(response.status).to eq(406)
+ expect_no_bounce
+ end
- post "/webhooks/mailjet.json",
- params: {
- "event" => "bounce",
- "email" => email,
- "hard_bounce" => true,
- "CustomID" => message_id,
- }
+ it "rejects missing token param" do
+ post "/webhooks/mailjet.json", params: bounce_params
- expect(response.status).to eq(406)
- expect(email_log.reload.bounced).to eq(false)
+ expect(response.status).to eq(406)
+ expect_no_bounce
+ end
end
end
describe "#mailpace" do
- it "works" do
- user = Fabricate(:user, email: email)
- email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+ let(:bounce_params) do
+ {
+ event: "email.bounced",
+ payload: {
+ status: "bounced",
+ to: email,
+ message_id: "<#{message_id}>",
+ },
+ }
+ end
- post "/webhooks/mailpace.json",
- params: {
- event: "email.bounced",
- payload: {
- status: "bounced",
- to: email,
- message_id: "<#{message_id}>",
- },
- }
+ it "processes webhooks with a deprecation warning when verification key is missing" do
+ post "/webhooks/mailpace.json", params: bounce_params
expect(response.status).to eq(200)
-
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq(nil) # mailpace doesn't give us this
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
+ expect_bounce(score: SiteSetting.hard_bounce_score)
end
- it "soft bounces" do
- user = Fabricate(:user, email: email)
- email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+ it "returns 406 when signature is invalid" do
+ SiteSetting.mailpace_verification_key = "key"
+ WebhooksController.any_instance.stubs(:valid_mailpace_signature?).returns(false)
- post "/webhooks/mailpace.json",
- params: {
- event: "email.deferred",
- payload: {
- status: "deferred",
- to: email,
- message_id: "<#{message_id}>",
- },
- }
-
- expect(response.status).to eq(200)
-
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq(nil) # mailpace doesn't give us this
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)
+ post "/webhooks/mailpace.json", params: bounce_params
+ expect(response.status).to eq(406)
end
- end
- describe "#mandrill" do
- let(:payload) do
- "mandrill_events=%5B%7B%22event%22%3A%22hard_bounce%22%2C%22msg%22%3A%7B%22email%22%3A%22em%40il.com%22%2C%22diag%22%3A%225.1.1%22%2C%22bounce_description%22%3A%22smtp%3B+550-5.1.1+The+email+account+that+you+tried+to+reach+does+not+exist.%22%2C%22metadata%22%3A%7B%22message_id%22%3A%2212345%40il.com%22%7D%7D%7D%5D"
- end
+ context "with a valid verification key" do
+ before do
+ SiteSetting.mailpace_verification_key = "key"
+ WebhooksController.any_instance.stubs(:valid_mailpace_signature?).returns(true)
+ end
- it "works" do
- user = Fabricate(:user, email: email)
- email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+ it "processes hard bounces" do
+ post "/webhooks/mailpace.json", params: bounce_params
- post "/webhooks/mandrill.json",
- params: {
- mandrill_events: [
- {
- "event" => "hard_bounce",
- "msg" => {
- "email" => email,
- "diag" => "5.1.1",
- :"bounce_description" =>
- "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",
- "metadata" => {
- "message_id" => message_id,
- },
- },
- },
- ].to_json,
- }
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score)
+ end
- expect(response.status).to eq(200)
+ it "processes soft bounces" do
+ post "/webhooks/mailpace.json",
+ params: {
+ event: "email.deferred",
+ payload: {
+ status: "deferred",
+ to: email,
+ message_id: "<#{message_id}>",
+ },
+ }
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq("5.1.1")
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.soft_bounce_score)
+ end
end
+ end
- it "verifies signatures" do
- SiteSetting.mandrill_authentication_key = "wr_JeJNO9OI65RFDrvk3Zw"
+ describe "#mandrill" do
+ def mandrill_events_json(event: "hard_bounce", diag: "5.1.1")
+ [
+ {
+ "event" => event,
+ "msg" => {
+ "email" => email,
+ "diag" => diag,
+ "bounce_description" =>
+ "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",
+ "metadata" => {
+ "message_id" => message_id,
+ },
+ },
+ },
+ ].to_json
+ end
- post "/webhooks/mandrill.json",
- headers: {
- "X-Mandrill-Signature" => "Q5pCb903EjEqRZ99gZrlYKOfvIU=",
- },
- params: payload
+ it "processes webhooks with a deprecation warning when authentication key is missing" do
+ post "/webhooks/mandrill.json", params: { mandrill_events: mandrill_events_json }
expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")
end
- it "returns error if signature verification fails" do
- SiteSetting.mandrill_authentication_key = "wr_JeJNO9OI65RFDrvk3Zw"
+ it "returns 406 when signature is invalid" do
+ SiteSetting.mandrill_authentication_key = "key"
+ WebhooksController.any_instance.stubs(:valid_mandrill_signature?).returns(false)
- post "/webhooks/mandrill.json", headers: { "X-Mandrill-Signature" => "foo" }, params: payload
+ post "/webhooks/mandrill.json", params: { mandrill_events: mandrill_events_json }
expect(response.status).to eq(406)
end
- it "returns error if signature is invalid" do
- SiteSetting.mandrill_authentication_key = "foo"
-
- post "/webhooks/mandrill.json",
- headers: {
- "X-Mandrill-Signature" => "Q5pCb903EjEqRZ99gZrlYKOfvIU=",
- },
- params: payload
+ context "with a valid authentication key" do
+ before do
+ SiteSetting.mandrill_authentication_key = "key"
+ WebhooksController.any_instance.stubs(:valid_mandrill_signature?).returns(true)
+ end
- expect(response.status).to eq(406)
- end
+ it "processes hard bounces" do
+ post "/webhooks/mandrill.json", params: { mandrill_events: mandrill_events_json }
- it "returns error if signature header is missing" do
- SiteSetting.mandrill_authentication_key = "wr_JeJNO9OI65RFDrvk3Zw"
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")
+ end
- post "/webhooks/mandrill.json", params: payload
+ it "processes soft bounces" do
+ post "/webhooks/mandrill.json",
+ params: {
+ mandrill_events: mandrill_events_json(event: "soft_bounce", diag: "4.7.1"),
+ }
- expect(response.status).to eq(406)
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.soft_bounce_score, error_code: "4.7.1")
+ end
end
end
describe "#mandrill_head" do
- it "works" do
+ it "returns 200" do
head "/webhooks/mandrill.json"
-
expect(response.status).to eq(200)
end
end
describe "#postmark" do
- it "works" do
- user = Fabricate(:user, email: email)
- email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+ let(:bounce_params) { { "Type" => "HardBounce", "MessageID" => message_id, "Email" => email } }
- post "/webhooks/postmark.json",
- params: {
- "Type" => "HardBounce",
- "MessageID" => message_id,
- "Email" => email,
- }
- expect(response.status).to eq(200)
+ it "processes webhooks with a deprecation warning when webhook token is missing" do
+ post "/webhooks/postmark.json", params: bounce_params
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq(nil) # postmark doesn't give us this
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
- end
-
- it "soft bounces" do
- 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" => "SoftBounce",
- "MessageID" => message_id,
- "Email" => email,
- }
expect(response.status).to eq(200)
-
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq(nil) # postmark doesn't give us this
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)
+ expect_bounce(score: SiteSetting.hard_bounce_score)
end
- it "verifies signatures" 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)
+ context "with a valid webhook token" do
+ before { SiteSetting.postmark_webhook_token = "foo" }
- post "/webhooks/postmark.json?t=foo",
- params: {
- "Type" => "HardBounce",
- "MessageID" => message_id,
- "Email" => email,
- }
+ it "processes hard bounces" do
+ post "/webhooks/postmark.json?t=foo", params: bounce_params
- expect(response.status).to eq(200)
- expect(email_log.reload.bounced).to eq(true)
- end
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score)
+ end
- it "returns error if signature verification fails" 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)
+ it "processes soft bounces" do
+ post "/webhooks/postmark.json?t=foo",
+ params: {
+ "Type" => "SoftBounce",
+ "MessageID" => message_id,
+ "Email" => email,
+ }
- post "/webhooks/postmark.json?t=bar",
- params: {
- "Type" => "HardBounce",
- "MessageID" => message_id,
- "Email" => email,
- }
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.soft_bounce_score)
+ end
- expect(response.status).to eq(406)
- expect(email_log.reload.bounced).to eq(false)
- end
+ it "rejects wrong token" do
+ post "/webhooks/postmark.json?t=bar", params: bounce_params
- 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)
+ expect(response.status).to eq(406)
+ expect_no_bounce
+ end
- post "/webhooks/postmark.json",
- params: {
- "Type" => "HardBounce",
- "MessageID" => message_id,
- "Email" => email,
- }
+ it "rejects missing token param" do
+ post "/webhooks/postmark.json", params: bounce_params
- expect(response.status).to eq(406)
- expect(email_log.reload.bounced).to eq(false)
+ expect(response.status).to eq(406)
+ expect_no_bounce
+ end
end
end
describe "#sparkpost" do
- it "works" do
- user = Fabricate(:user, email: email)
- email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+ let(:bounce_params) do
+ {
+ "_json" => [
+ {
+ "msys" => {
+ "message_event" => {
+ "bounce_class" => 10,
+ "error_code" => "554",
+ "rcpt_to" => email,
+ "rcpt_meta" => {
+ "message_id" => message_id,
+ },
+ },
+ },
+ },
+ ],
+ }
+ end
- post "/webhooks/sparkpost.json",
- params: {
- "_json" => [
- {
- "msys" => {
- "message_event" => {
- "bounce_class" => 10,
- "error_code" => "554",
- "rcpt_to" => email,
- "rcpt_meta" => {
- "message_id" => message_id,
- },
- },
- },
- },
- ],
- }
+ it "processes webhooks with a deprecation warning when webhook token is missing" do
+ post "/webhooks/sparkpost.json", params: bounce_params
expect(response.status).to eq(200)
-
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
+ expect_bounce(score: SiteSetting.hard_bounce_score)
end
- it "verifies signatures" 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)
+ context "with a valid webhook token" do
+ before { SiteSetting.sparkpost_webhook_token = "foo" }
- post "/webhooks/sparkpost.json?t=foo",
- params: {
- "_json" => [
- {
- "msys" => {
- "message_event" => {
- "bounce_class" => 10,
- "error_code" => "554",
- "rcpt_to" => email,
- "rcpt_meta" => {
- "message_id" => message_id,
- },
- },
- },
- },
- ],
- }
+ it "processes hard bounces" do
+ post "/webhooks/sparkpost.json?t=foo", params: bounce_params
- expect(response.status).to eq(200)
- expect(email_log.reload.bounced).to eq(true)
- end
-
- it "returns error if signature verification fails" 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)
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score)
+ end
- post "/webhooks/sparkpost.json?t=bar",
- params: {
- "_json" => [
- {
- "msys" => {
- "message_event" => {
- "bounce_class" => 10,
- "error_code" => "554",
- "rcpt_to" => email,
- "rcpt_meta" => {
- "message_id" => message_id,
+ it "processes soft bounces" do
+ post "/webhooks/sparkpost.json?t=foo",
+ params: {
+ "_json" => [
+ {
+ "msys" => {
+ "message_event" => {
+ "bounce_class" => 20,
+ "error_code" => "450",
+ "rcpt_to" => email,
+ "rcpt_meta" => {
+ "message_id" => message_id,
+ },
},
},
},
- },
- ],
- }
+ ],
+ }
- expect(response.status).to eq(406)
- expect(email_log.reload.bounced).to eq(false)
- end
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.soft_bounce_score)
+ 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)
+ it "rejects wrong token" do
+ post "/webhooks/sparkpost.json?t=bar", params: bounce_params
- 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_no_bounce
+ end
- expect(response.status).to eq(406)
- expect(email_log.reload.bounced).to eq(false)
+ it "rejects missing token param" do
+ post "/webhooks/sparkpost.json", params: bounce_params
+
+ expect(response.status).to eq(406)
+ expect_no_bounce
+ end
end
end
@@ -635,10 +576,10 @@ RSpec.describe WebhooksController do
"Type" => "Notification",
"Message" => {
"notificationType" => "Bounce",
- :"bounce" => {
+ "bounce" => {
"bounceType" => "Permanent",
"reportingMTA" => "dns; email.example.com",
- :"bouncedRecipients" => [
+ "bouncedRecipients" => [
{
"emailAddress" => email,
"status" => "5.1.1",
@@ -651,7 +592,7 @@ RSpec.describe WebhooksController do
"feedbackId" => "00000138111222aa-33322211-cccc-cccc-cccc-ddddaaaa068a-000000",
"remoteMtaIp" => "127.0.2.0",
},
- :"mail" => {
+ "mail" => {
"timestamp" => "2016-01-27T14:59:38.237Z",
"source" => "[email protected]",
"sourceArn" => "arn:aws:ses:us-east-1:888888888888:identity/example.com",
@@ -690,19 +631,14 @@ RSpec.describe WebhooksController do
before { Jobs.run_immediately! }
- it "works" do
- user = Fabricate(:user, email: email)
- email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
-
+ it "processes bounce notifications" do
require "aws-sdk-sns"
Aws::SNS::MessageVerifier.any_instance.stubs(:authentic?).with(payload).returns(true)
post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => payload }
- expect(response.status).to eq(200)
- email_log.reload
- expect(email_log.bounced).to eq(true)
- expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
+ expect(response.status).to eq(200)
+ expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")
end
end
end
diff --git a/spec/services/problem_check/email_bounce_handling_spec.rb b/spec/services/problem_check/email_bounce_handling_spec.rb
new file mode 100644
index 00000000000..4d99fad3bb4
--- /dev/null
+++ b/spec/services/problem_check/email_bounce_handling_spec.rb
@@ -0,0 +1,150 @@
+# frozen_string_literal: true
+
+RSpec.describe ProblemCheck::EmailBounceHandling do
+ subject(:check) { described_class.new }
+
+ before do
+ SiteSetting.stubs(reply_by_email_enabled: reply_by_email_enabled)
+ Email::Sender.stubs(:bounceable_reply_address?).returns(bounceable_reply_address)
+ ActionMailer::Base.stubs(smtp_settings: { address: smtp_address })
+ end
+
+ context "when reply by email is enabled with VERP" do
+ let(:reply_by_email_enabled) { true }
+ let(:bounceable_reply_address) { true }
+ let(:smtp_address) { "smtp.mailgun.org" }
+
+ it { expect(check).to be_chill_about_it }
+ end
+
+ context "when reply by email is enabled without VERP" do
+ let(:reply_by_email_enabled) { true }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "smtp.mailgun.org" }
+
+ before { SiteSetting.stubs(mailgun_api_key: "") }
+
+ it { expect(check).to have_a_problem }
+ end
+
+ context "when reply by email is disabled with VERP address configured" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { true }
+ let(:smtp_address) { "smtp.mailgun.org" }
+
+ before { SiteSetting.stubs(mailgun_api_key: "") }
+
+ it { expect(check).to have_a_problem }
+ end
+
+ context "when using Mailgun without an API key" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "smtp.mailgun.org" }
+
+ before { SiteSetting.stubs(mailgun_api_key: "") }
+
+ it do
+ expect(check).to have_a_problem.with_priority("low").with_message(
+ I18n.t(
+ "dashboard.problem.email_bounce_handling.webhook_key_missing",
+ provider: "Mailgun",
+ setting: "mailgun_api_key",
+ base_path: Discourse.base_path,
+ ),
+ )
+ end
+ end
+
+ context "when using Mailgun with an API key" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "smtp.mailgun.org" }
+
+ before { SiteSetting.stubs(mailgun_api_key: "key-123") }
+
+ it { expect(check).to be_chill_about_it }
+ end
+
+ context "when using SendGrid without a verification key" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "smtp.sendgrid.net" }
+
+ before { SiteSetting.stubs(sendgrid_verification_key: "") }
+
+ it do
+ expect(check).to have_a_problem.with_priority("low").with_message(
+ I18n.t(
+ "dashboard.problem.email_bounce_handling.webhook_key_missing",
+ provider: "SendGrid",
+ setting: "sendgrid_verification_key",
+ base_path: Discourse.base_path,
+ ),
+ )
+ end
+ end
+
+ context "when using SendGrid with a verification key" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "smtp.sendgrid.net" }
+
+ before { SiteSetting.stubs(sendgrid_verification_key: "key-123") }
+
+ it { expect(check).to be_chill_about_it }
+ end
+
+ context "when using Mailjet without a webhook token" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "in-v3.mailjet.com" }
+
+ before { SiteSetting.stubs(mailjet_webhook_token: "") }
+
+ it { expect(check).to have_a_problem.with_priority("low") }
+ end
+
+ context "when using Mailpace without a verification key" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "smtp.mailpace.com" }
+
+ before { SiteSetting.stubs(mailpace_verification_key: "") }
+
+ it { expect(check).to have_a_problem.with_priority("low") }
+ end
+
+ context "when using Mailpace with a verification key" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "smtp.mailpace.com" }
+
+ before { SiteSetting.stubs(mailpace_verification_key: "key-123") }
+
+ it { expect(check).to be_chill_about_it }
+ end
+
+ context "when using AWS SES" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "email-smtp.us-east-1.amazonaws.com" }
+
+ it { expect(check).to be_chill_about_it }
+ end
+
+ context "when using an unknown provider" do
+ let(:reply_by_email_enabled) { false }
+ let(:bounceable_reply_address) { false }
+ let(:smtp_address) { "smtp.unknown-provider.com" }
+
+ it do
+ expect(check).to have_a_problem.with_priority("low").with_message(
+ I18n.t(
+ "dashboard.problem.email_bounce_handling.no_bounce_handling",
+ base_path: Discourse.base_path,
+ ),
+ )
+ end
+ end
+end
diff --git a/spec/services/problem_check/missing_mailgun_api_key_spec.rb b/spec/services/problem_check/missing_mailgun_api_key_spec.rb
deleted file mode 100644
index e52625fc323..00000000000
--- a/spec/services/problem_check/missing_mailgun_api_key_spec.rb
+++ /dev/null
@@ -1,41 +0,0 @@
-# frozen_string_literal: true
-
-RSpec.describe ProblemCheck::MissingMailgunApiKey do
- subject(:check) { described_class.new }
-
- describe ".call" do
- before do
- SiteSetting.stubs(reply_by_email_enabled: replies_enabled)
- ActionMailer::Base.stubs(smtp_settings: { address: mailgun_address })
- SiteSetting.stubs(mailgun_api_key: api_key)
- end
-
- context "when replies are disabled" do
- let(:replies_enabled) { false }
- let(:mailgun_address) { anything }
- let(:api_key) { anything }
-
- it { expect(check).to be_chill_about_it }
- end
-
- context "when not using Mailgun for replies" do
- let(:replies_enabled) { false }
- let(:mailgun_address) { nil }
- let(:api_key) { anything }
-
- it { expect(check).to be_chill_about_it }
- end
-
- context "when using Mailgun without an API key" do
- let(:replies_enabled) { true }
- let(:mailgun_address) { "smtp.mailgun.org" }
- let(:api_key) { nil }
-
- it do
- expect(check).to have_a_problem.with_priority("low").with_message(
- "The server is configured to send emails via Mailgun but you haven't provided an API key used to verify the webhook messages.",
- )
- end
- end
- end
-end