SES/SNS bounce notifications no longer match EmailLog after TopicArn security hardening

סיכום מבוסס בינה מלאכותית

Users reported that SES bounce notifications delivered via SNS were no longer being processed correctly after recent security hardening updates. BryanV identified that while SNS notifications reached the Discourse webhook and passed signature/allowlist checks, the corresponding EmailLog entries were not marked as bounced. The root cause was identified as a mismatch in identifiers: AWS SES uses an assigned messageId in notifications, while Discourse’s EmailLog stores its own original RFC Message-ID. This regression was traced to commit 61f12e13 in June 2026, which reinstated a lookup method that had been changed in 2019 specifically to handle this difference.

Sailor confirmed the issue on a self-hosted instance, noting that bounce processing was silently disabled for SES SMTP users on the 2026.7 ESR. Although the webhook returned a 200 success status, the background job failed to find the email log and exited early. BryanV requested an ETA for a fix, providing a proposed patch that adjusts the identifier lookup to match Discourse’s original Message-ID.

Sam confirmed the fix was merged via PR #44995. Sailor verified that upgrading to release/2026.10 resolved the issue, with test bounces appearing in the admin logs within a minute. However, they cautioned that users remaining on the 2026.7 ESR would continue to experience silent bounce processing failures until they upgrade or a backport is provided.

I was trying to set up bounce notifications from AWS SES after getting a prompt about a missing ARN configuration for it after the latest upgrade. I configured the topic and added the ARN but a test bounce email never showed up. Confirmed that it hit the topic on AWS though. Below is an analysis and proposed patch from Codex (untested).

Summary

Amazon SES bounce notifications delivered through SNS are accepted by /webhooks/aws, but current Discourse does not mark the corresponding EmailLog as bounced when SES is used through SMTP.

Jobs::ProcessSnsNotification looks up the log using mail.messageId. AWS documents that this is an SES-assigned identifier, while Email::Sender stores Discourse’s original RFC Message-ID in EmailLog.message_id. The two identifiers differ.

This appears to be a regression introduced by the June 2026 security hardening in commit 61f12e13aa1b760f81d5ff60f12e3a7e77434b94. The topic allowlist, signature verification, recipient binding, and duplicate protection should remain intact; only the identifier used for the lookup needs to change.

Environment

  • Discourse commit: 2239124ce41df4ea23a21686a78342adb5f6b3b5
  • Amazon SES SMTP sending
  • SES bounce notification topic delivered through SNS to /webhooks/aws
  • aws_sns_topic_arn_allowlist contains the exact SNS topic ARN
  • SNS subscription is confirmed

Steps to reproduce

  1. Configure Discourse to send through the Amazon SES SMTP endpoint.
  2. Configure an SES bounce notification topic, with original headers included.
  3. Subscribe https://<discourse-host>/webhooks/aws to the topic.
  4. Add that topic ARN to aws_sns_topic_arn_allowlist.
  5. Send a Discourse email to bounce@simulator.amazonses.com.
  6. Confirm that SES publishes the bounce and SNS reports a successful HTTPS delivery.
  7. Open /admin/email-logs/bounced.

Actual result

The webhook returns success and SNS reports no failed delivery, but the email is absent from the bounced-email log and its Discourse bounce state is not updated.

Expected result

Discourse should match the SES bounce to the sent EmailLog using the original RFC Message-ID and bounced recipient, then update the bounce state and score.

Cause

Discourse stores the pre-delivery message ID:

email_log.message_id = @message.message_id

The SNS job currently uses the SES-assigned ID:

message_id = message.dig("mail", "messageId")

AWS distinguishes these fields:

  • mail.messageId is assigned by SES.
  • The original email’s Message-ID is available in mail.headers and mail.commonHeaders when original headers are enabled.

The current request spec gives mail.messageId, mail.headers Message-ID, mail.commonHeaders.messageId, and EmailLog.message_id the same fixture value, so it does not reproduce actual SES behavior.

There is direct project history for this mismatch: PR #7284 removed strict ID matching in 2019 because the SNS ID did not equal the ID in EmailLog. The June 2026 security fix restored strict matching but used the SES-assigned ID.

Proposed fix

Prefer mail.commonHeaders.messageId, normalize it with Email::MessageIdService.message_id_clean, and fall back to mail.messageId for compatibility when original headers are unavailable. Keep the existing (message_id, to_address) lookup, TopicArn allowlist, SNS signature verification, and duplicate handling.

The attached patch also changes the request fixture so the SES-assigned ID differs from the original RFC Message-ID.

diff --git a/app/jobs/regular/process_sns_notification.rb b/app/jobs/regular/process_sns_notification.rb
index 2785887d..21ebb1a2 100644
--- a/app/jobs/regular/process_sns_notification.rb
+++ b/app/jobs/regular/process_sns_notification.rb
@@ -17,7 +17,13 @@ module Jobs
         end
 
       return unless message && message["notificationType"] == "Bounce"
-      return unless message_id = message.dig("mail", "messageId").presence
+      message_id =
+        message.dig("mail", "commonHeaders", "messageId").presence ||
+          message.dig("mail", "messageId").presence
+      return unless message_id
+
+      message_id = Email::MessageIdService.message_id_clean(message_id.strip)
+
       return unless bounce_type = message.dig("bounce", "bounceType").presence
 
       return if !Email::Sns.allowed_topic_arn?(json["TopicArn"])
diff --git a/spec/requests/webhooks_controller_spec.rb b/spec/requests/webhooks_controller_spec.rb
index eb7f5b44..8c77b337 100644
--- a/spec/requests/webhooks_controller_spec.rb
+++ b/spec/requests/webhooks_controller_spec.rb
@@ -794,6 +794,7 @@ RSpec.describe WebhooksController 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(:ses_message_id) { "000001378603177f-7a5433e7-8edb-42ae-af10-f0181f34d6ee-000000" }
     let(:payload) do
       {
         "Type" => "Notification",
@@ -823,7 +824,7 @@ RSpec.describe WebhooksController do
             "sourceIp" => "127.0.3.0",
             "sendingAccountId" => "123456789012",
             "callerIdentity" => "IAM_user_or_role_name",
-            "messageId" => message_id,
+            "messageId" => ses_message_id,
             "destination" => [email, "jane@example.com", "mary@example.com", "richard@example.com"],
             "headersTruncated" => false,
             "headers" => [
@@ -833,7 +834,7 @@ RSpec.describe WebhooksController do
                 "value" =>
                   "\"Test\" <#{email}>, \"Jane Doe\" <jane@example.com>, \"Mary Doe\" <mary@example.com>, \"Richard Doe\" <richard@example.com>",
               },
-              { "name" => "Message-ID", "value" => message_id },
+              { "name" => "Message-ID", "value" => "<#{message_id}>" },
               { "name" => "Subject", "value" => "Hello" },
               { "name" => "Content-Type", "value" => "text/plain; charset=\"UTF-8\"" },
               { "name" => "Content-Transfer-Encoding", "value" => "base64" },
@@ -845,7 +846,7 @@ RSpec.describe WebhooksController do
               "to" => [
                 "\"Test\" <#{email}>, Jane Doe <jane@example.com>, Mary Doe <mary@example.com>, Richard Doe <richard@example.com>",
               ],
-              "messageId" => message_id,
+              "messageId" => "<#{message_id}>",
               "subject" => "Hello",
             },
           },
@@ -870,7 +871,7 @@ RSpec.describe WebhooksController do
       SiteSetting.aws_sns_topic_arn_allowlist = topic_arn
     end
 
-    it "hard bounces" do
+    it "hard bounces using the original message ID" do
       user = Fabricate(:user, email: email)
       email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
 
@@ -883,7 +884,7 @@ RSpec.describe WebhooksController do
       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
+    it "does not bounce an email log with a different original message ID" do
       user = Fabricate(:user, email: email)
       email_log =
         Fabricate(:email_log, user: user, message_id: "other-message-id", to_address: email)

References

לייק 1

Reproduced on a self-hosted install running v2026.7.3 (release/2026.7), SES SMTP in us-east-1, embedded PostgreSQL 18.

Setup

  • aws_sns_topic_arn_allowlist contains the exact topic ARN (verified against the SNS console).
  • SNS HTTPS subscription to /webhooks/aws confirmed; the same subscription processed bounces correctly from March to June 2026 on release/2026.4 and 2026.5.0.

Observation
Two test messages to bounce@simulator.amazonses.com. Each produced one SNS POST that reached the container:

"POST /webhooks/aws HTTP/1.1" "Amazon Simple Notification Service Agent" 200 402

Nothing at /logs, nothing under /admin/email-logs/bounced. Since WebhooksController#aws returns 406 on an allowlist or signature failure, the 200 confirms both checks pass and Jobs::ProcessSnsNotification is enqueued; the job then exits at next if email_log.nil? because mail.messageId (SES-assigned) never equals EmailLog.message_id (Discourse’s own Message-ID, set in Email::Sender at email_log.message_id = @message.message_id).

The git history agrees with the analysis above: #7284 (2019) dropped the ID match for exactly this reason, and 61f12e1 (June 2026) reinstated it using the SES-assigned ID. On release/2026.7 the job is unchanged since that commit.

Net effect for SES self-hosters on the current ESR: bounce processing is silently disabled, and the new dashboard notice steers admins into a configuration that then does nothing. A backport to release/2026.7 would be welcome once a fix lands.

2 לייקים

Any possible ETA on this? It seems like a tiny fix and has a patch ready including tests. I’d really like to close out our infra updates including the bounce notification.

Will be fixed per:

2 לייקים

Thanks @sam. Confirmed on my self-hosted forum: after moving to release/2026.10 (2026.10.0) on 2026-10-31, a test email to bounce@simulator.amazonses.com showed up under Admin > Email logs > Bounced within a minute, the first bounce recorded since we went to 2026.7. Our SNS notifications have “Include original email headers” on.

For anyone still on the 2026.7 ESR: the fix is not on release/2026.7 as of today. process_sns_notification.rb there still looks up the SES-assigned messageId, so bounces stay unrecorded until you move to 2026.10 or the next ESR.

לייק 1