Skip to content

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

SECURITY: Prevent any signed AWS SNS TopicARN from being accepted via webhooks

Nat61f12e13aa1b
diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb
index f86349fbaae..0b39e60c362 100644
--- a/app/controllers/webhooks_controller.rb
+++ b/app/controllers/webhooks_controller.rb
@@ -185,6 +185,9 @@ class WebhooksController < ActionController::Base
raw = request.raw_post
json = JSON.parse(raw)
+ return signature_failure unless Email::Sns.allowed_topic_arn?(json["TopicArn"])
+ return signature_failure unless Email::Sns.authentic?(raw)
+
case json["Type"]
when "SubscriptionConfirmation"
Jobs.enqueue(:confirm_sns_subscription, raw: raw, json: json)
diff --git a/app/jobs/regular/confirm_sns_subscription.rb b/app/jobs/regular/confirm_sns_subscription.rb
index bd1f0dcebf0..f8e191d5e56 100644
--- a/app/jobs/regular/confirm_sns_subscription.rb
+++ b/app/jobs/regular/confirm_sns_subscription.rb
@@ -9,8 +9,8 @@ module Jobs
return unless json = args[:json].presence
return unless subscribe_url = json["SubscribeURL"].presence
- require "aws-sdk-sns"
- return unless Aws::SNS::MessageVerifier.new.authentic?(raw)
+ return if !Email::Sns.allowed_topic_arn?(json["TopicArn"])
+ return unless Email::Sns.authentic?(raw)
uri =
begin
diff --git a/app/jobs/regular/process_sns_notification.rb b/app/jobs/regular/process_sns_notification.rb
index 09e965a6ea4..2785887da9c 100644
--- a/app/jobs/regular/process_sns_notification.rb
+++ b/app/jobs/regular/process_sns_notification.rb
@@ -20,32 +20,24 @@ module Jobs
return unless message_id = message.dig("mail", "messageId").presence
return unless bounce_type = message.dig("bounce", "bounceType").presence
- require "aws-sdk-sns"
- return unless Aws::SNS::MessageVerifier.new.authentic?(raw)
-
- message
- .dig("bounce", "bouncedRecipients")
- .each do |r|
- if email_log =
- EmailLog.order("created_at DESC").where(to_address: r["emailAddress"]).first
- email_log.update_columns(bounced: true, bounce_error_code: r["status"])
-
- if email_log.user&.email.present?
- if email_log.user.user_stat.bounce_score.to_s.start_with?("4.") ||
- bounce_type == "Transient"
- Email::Receiver.update_bounce_score(
- email_log.user.email,
- SiteSetting.soft_bounce_score,
- )
- else
- Email::Receiver.update_bounce_score(
- email_log.user.email,
- SiteSetting.hard_bounce_score,
- )
- end
- end
- end
+ return if !Email::Sns.allowed_topic_arn?(json["TopicArn"])
+ return unless Email::Sns.authentic?(raw)
+
+ Array(message.dig("bounce", "bouncedRecipients")).each do |r|
+ email_log = EmailLog.find_by(message_id: message_id, to_address: r["emailAddress"])
+ next if email_log.nil? || email_log.bounced?
+
+ email_log.update!(bounced: true, bounce_error_code: r["status"])
+
+ next if email_log.user&.email.blank?
+
+ if email_log.user.user_stat.bounce_score.to_s.start_with?("4.") ||
+ bounce_type == "Transient"
+ Email::Receiver.update_bounce_score(email_log.user.email, SiteSetting.soft_bounce_score)
+ else
+ Email::Receiver.update_bounce_score(email_log.user.email, SiteSetting.hard_bounce_score)
end
+ end
end
end
end
diff --git a/app/models/problem_check.rb b/app/models/problem_check.rb
index 78a00e350f9..6e335051162 100644
--- a/app/models/problem_check.rb
+++ b/app/models/problem_check.rb
@@ -80,6 +80,7 @@ class ProblemCheck
ProblemCheck::GroupEmailCredentials,
ProblemCheck::HostNames,
ProblemCheck::ImageMagick,
+ ProblemCheck::MissingAwsSnsTopicArn,
ProblemCheck::MissingMailgunApiKey,
ProblemCheck::OutOfDateThemes,
ProblemCheck::PollPop3Timeout,
diff --git a/app/services/problem_check/missing_aws_sns_topic_arn.rb b/app/services/problem_check/missing_aws_sns_topic_arn.rb
new file mode 100644
index 00000000000..56d64124c59
--- /dev/null
+++ b/app/services/problem_check/missing_aws_sns_topic_arn.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class ProblemCheck::MissingAwsSnsTopicArn < ProblemCheck
+ self.priority = "low"
+
+ SES_SMTP_PATTERN = /(email-smtp|amazonses).*amazonaws\.com\z/i
+
+ def call
+ return no_problem if SiteSetting.aws_sns_topic_arn_allowlist.present?
+ return no_problem unless smtp_looks_like_ses?
+
+ problem
+ end
+
+ private
+
+ def smtp_looks_like_ses?
+ ActionMailer::Base.smtp_settings[:address].to_s.match?(SES_SMTP_PATTERN)
+ end
+end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 7259db46558..473a4bdeb40 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1848,6 +1848,7 @@ en:
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."
+ missing_aws_sns_topic_arn: "The server is configured to send emails via Amazon SES but you haven't allowlisted any SNS topic ARNs. Bounce notifications will be rejected until you add the legitimate ARN to <a href='%{base_path}/admin/site_settings/category/email?filter=aws_sns_topic_arn_allowlist'>aws_sns_topic_arn_allowlist</a>."
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."
@@ -2491,6 +2492,7 @@ en:
ignore_by_title: "Ignore incoming emails based on their title."
mailgun_api_key: "Mailgun Secret API key used to verify webhook messages."
sendgrid_verification_key: "Sendgrid verification key used to verify webhook messages."
+ aws_sns_topic_arn_allowlist: "SNS topic ARNs allowed to send Amazon SES bounce webhook notifications."
mailjet_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/mailjet?t=supersecret"
mailpace_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/mailpace?t=supersecret"
mandrill_authentication_key: "Mandrill authentication key used to verify webhook messages."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 5e732013af8..8b1ba57a37c 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2190,6 +2190,10 @@ email:
sendgrid_verification_key:
default: ""
secret: true
+ aws_sns_topic_arn_allowlist:
+ default: ""
+ type: list
+ list_type: simple
mailjet_webhook_token:
default: ""
secret: true
diff --git a/lib/email/sns.rb b/lib/email/sns.rb
new file mode 100644
index 00000000000..399a5f046ff
--- /dev/null
+++ b/lib/email/sns.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module Email
+ class Sns
+ def self.authentic?(raw)
+ require "aws-sdk-sns"
+ Aws::SNS::MessageVerifier.new.authentic?(raw)
+ end
+
+ def self.allowed_topic_arn?(topic_arn)
+ return false if topic_arn.blank?
+ SiteSetting.aws_sns_topic_arn_allowlist.to_s.split("|").include?(topic_arn)
+ end
+ end
+end
diff --git a/spec/lib/email/sns_spec.rb b/spec/lib/email/sns_spec.rb
new file mode 100644
index 00000000000..759f430b169
--- /dev/null
+++ b/spec/lib/email/sns_spec.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+RSpec.describe Email::Sns do
+ describe ".allowed_topic_arn?" do
+ let(:topic_arn) { "arn:aws:sns:us-east-1:123456789012:discourse-bounces" }
+ let(:other_topic_arn) { "arn:aws:sns:us-east-1:999999999999:attacker-topic" }
+
+ before { SiteSetting.aws_sns_topic_arn_allowlist = topic_arn }
+
+ it "returns false for a blank topic arn" do
+ expect(described_class.allowed_topic_arn?(nil)).to eq(false)
+ expect(described_class.allowed_topic_arn?("")).to eq(false)
+ end
+
+ it "returns false for a topic arn that is not on the allowlist" do
+ expect(described_class.allowed_topic_arn?(other_topic_arn)).to eq(false)
+ end
+
+ it "returns true for a topic arn on the allowlist" do
+ expect(described_class.allowed_topic_arn?(topic_arn)).to eq(true)
+ end
+
+ it "matches any entry of a pipe-separated allowlist" do
+ SiteSetting.aws_sns_topic_arn_allowlist = "#{topic_arn}|#{other_topic_arn}"
+
+ expect(described_class.allowed_topic_arn?(topic_arn)).to eq(true)
+ expect(described_class.allowed_topic_arn?(other_topic_arn)).to eq(true)
+ end
+
+ it "returns false when the allowlist is empty" do
+ SiteSetting.aws_sns_topic_arn_allowlist = ""
+
+ expect(described_class.allowed_topic_arn?(topic_arn)).to eq(false)
+ end
+ end
+
+ describe ".authentic?" do
+ it "delegates to the AWS SNS message verifier" do
+ require "aws-sdk-sns"
+ raw = "{}"
+ Aws::SNS::MessageVerifier.any_instance.expects(:authentic?).with(raw).returns(true)
+
+ expect(described_class.authentic?(raw)).to eq(true)
+ end
+ end
+end
diff --git a/spec/requests/webhooks_controller_spec.rb b/spec/requests/webhooks_controller_spec.rb
index 210d3d2d033..eb7f5b440f1 100644
--- a/spec/requests/webhooks_controller_spec.rb
+++ b/spec/requests/webhooks_controller_spec.rb
@@ -791,9 +791,13 @@ RSpec.describe WebhooksController do
end
describe "#aws" do
+ let(:topic_arn) { "arn:aws:sns:us-east-1:123456789012:discourse-bounces" }
+ let(:other_topic_arn) { "arn:aws:sns:us-east-1:999999999999:attacker-topic" }
+ let(:bounce_status) { "5.1.1" }
let(:payload) do
{
"Type" => "Notification",
+ "TopicArn" => topic_arn,
"Message" => {
"notificationType" => "Bounce",
:bounce => {
@@ -802,7 +806,7 @@ RSpec.describe WebhooksController do
:bouncedRecipients => [
{
"emailAddress" => email,
- "status" => "5.1.1",
+ "status" => bounce_status,
"action" => "failed",
"diagnosticCode" => "smtp; 550 5.1.1 <#{email}>... User",
},
@@ -848,22 +852,144 @@ RSpec.describe WebhooksController do
}.to_json,
}.to_json
end
+ let(:subscription_confirmation_payload) do
+ {
+ "Type" => "SubscriptionConfirmation",
+ "TopicArn" => topic_arn,
+ "Token" => "abc123",
+ "Message" => "You have chosen to subscribe to the topic #{topic_arn}",
+ "SubscribeURL" =>
+ "https://sns.us-east-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=#{topic_arn}&Token=abc123",
+ }.to_json
+ end
- before { Jobs.run_immediately! }
+ before do
+ Jobs.run_immediately!
+ require "aws-sdk-sns"
+ Aws::SNS::MessageVerifier.any_instance.stubs(:authentic?).returns(true)
+ SiteSetting.aws_sns_topic_arn_allowlist = topic_arn
+ end
it "hard bounces" do
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
- 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.bounce_error_code).to eq("5.1.1")
expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
end
+
+ it "does not bounce an email log with a different SES message id" do
+ user = Fabricate(:user, email: email)
+ email_log =
+ Fabricate(:email_log, user: user, message_id: "other-message-id", to_address: email)
+
+ post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => payload }
+ expect(response.status).to eq(200)
+
+ expect(email_log.reload.bounced).to eq(false)
+ expect(email_log.user.user_stat.bounce_score).to eq(0)
+ end
+
+ it "does not increase the bounce score for duplicate notifications" do
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => payload }
+ expect(response.status).to eq(200)
+
+ post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => payload }
+ expect(response.status).to eq(200)
+
+ expect(email_log.reload.bounced).to eq(true)
+ expect(email_log.user.user_stat.reload.bounce_score).to eq(SiteSetting.hard_bounce_score)
+ end
+
+ context "with a non-normalized bounce status" do
+ let(:bounce_status) { "5.0.0 (permanent failure)" }
+
+ it "stores the normalized bounce error code" do
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => payload }
+ expect(response.status).to eq(200)
+
+ expect(email_log.reload.bounce_error_code).to eq("5.0.0")
+ end
+ end
+
+ context "when the allowlist is empty" do
+ before { SiteSetting.aws_sns_topic_arn_allowlist = "" }
+
+ it "rejects notifications with 406 and does not process the bounce" do
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => payload }
+ expect(response.status).to eq(406)
+
+ expect(email_log.reload.bounced).to eq(false)
+ expect(email_log.user.user_stat.bounce_score).to eq(0)
+ end
+ end
+
+ context "when the TopicArn is not on the allowlist" do
+ let(:payload_with_other_topic) do
+ parsed = JSON.parse(payload)
+ parsed["TopicArn"] = other_topic_arn
+ parsed.to_json
+ end
+
+ it "rejects with 406 and does not process the bounce" do
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => payload_with_other_topic }
+ expect(response.status).to eq(406)
+
+ expect(email_log.reload.bounced).to eq(false)
+ expect(email_log.user.user_stat.bounce_score).to eq(0)
+ end
+ end
+
+ context "when the SNS signature is invalid" do
+ before { Aws::SNS::MessageVerifier.any_instance.stubs(:authentic?).returns(false) }
+
+ it "rejects with 406" do
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => payload }
+ expect(response.status).to eq(406)
+
+ expect(email_log.reload.bounced).to eq(false)
+ end
+ end
+
+ context "with a SubscriptionConfirmation" do
+ before { Net::HTTP.stubs(:get).returns("") }
+
+ it "enqueues the confirmation job when the TopicArn is allowlisted" do
+ Jobs::ConfirmSnsSubscription.any_instance.expects(:execute).once
+
+ post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => subscription_confirmation_payload }
+ expect(response.status).to eq(200)
+ end
+
+ it "rejects with 406 when the TopicArn is not allowlisted" do
+ Jobs::ConfirmSnsSubscription.any_instance.expects(:execute).never
+
+ attacker_payload =
+ JSON.parse(subscription_confirmation_payload).merge("TopicArn" => other_topic_arn).to_json
+
+ post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => attacker_payload }
+ expect(response.status).to eq(406)
+ end
+ end
end
end
diff --git a/spec/services/problem_check/missing_aws_sns_topic_arn_spec.rb b/spec/services/problem_check/missing_aws_sns_topic_arn_spec.rb
new file mode 100644
index 00000000000..6fad6506b1b
--- /dev/null
+++ b/spec/services/problem_check/missing_aws_sns_topic_arn_spec.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+RSpec.describe ProblemCheck::MissingAwsSnsTopicArn do
+ subject(:check) { described_class.new }
+
+ describe ".call" do
+ before { ActionMailer::Base.stubs(smtp_settings: { address: smtp_address }) }
+
+ context "when the allowlist is configured" do
+ let(:smtp_address) { "email-smtp.us-east-1.amazonaws.com" }
+
+ before { SiteSetting.aws_sns_topic_arn_allowlist = "arn:aws:sns:us-east-1:123:topic" }
+
+ it { expect(check).to be_chill_about_it }
+ end
+
+ context "when SMTP is not Amazon SES" do
+ let(:smtp_address) { "smtp.sendgrid.net" }
+
+ before { SiteSetting.aws_sns_topic_arn_allowlist = "" }
+
+ it { expect(check).to be_chill_about_it }
+ end
+
+ context "when SMTP is Amazon SES and allowlist is empty" do
+ let(:smtp_address) { "email-smtp.us-east-1.amazonaws.com" }
+
+ before { SiteSetting.aws_sns_topic_arn_allowlist = "" }
+
+ it { expect(check).to have_a_problem.with_priority("low") }
+ end
+ end
+end