FIX: incoming email matches the wrong user if null bounce key available in db

FIX: incoming email matches the wrong user if null bounce key available in db

From fc0b7c9e2619add7750f572c6a51de1b81ab3b66 Mon Sep 17 00:00:00 2001
From: Vinoth Kannan <vinothkannan@vinkas.com>
Date: Fri, 30 Nov 2018 12:29:51 +0530
Subject: [PATCH] FIX: incoming email matches the wrong user if null bounce key
 available in db


diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index 589d279..457ffa4 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -69,7 +69,7 @@ module Email
         begin
           return if IncomingEmail.exists?(message_id: @message_id)
           ensure_valid_address_lists
-          @from_email, @from_display_name = parse_from_field(@mail)
+          @from_email, @from_display_name = parse_from_field
           @from_user = User.find_by_email(@from_email)
           @incoming_email = create_incoming_email
           process_internal
@@ -249,6 +249,7 @@ module Email
     end
 
     def email_log
+      return nil if bounce_key.blank?
       @email_log ||= EmailLog.find_by(bounce_key: bounce_key)
     end
 
@@ -472,7 +473,9 @@ module Email
       reply.split(previous_replies_regex)[0]
     end
 
-    def parse_from_field(mail)
+    def parse_from_field(mail = nil)
+      mail ||= @mail
+
       if mail.bounced?
         Array.wrap(mail.final_recipient).each do |from|
           return extract_from_address_and_name(from)
diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb
index 50ab064..0488974 100644
--- a/spec/components/email/receiver_spec.rb
+++ b/spec/components/email/receiver_spec.rb
@@ -163,6 +163,14 @@ describe Email::Receiver do
     expect(IncomingEmail.last.error).to eq("RuntimeError")
   end
 
+  it "matches the correct user" do
+    user = Fabricate(:user)
+    email_log = Fabricate(:email_log, to_address: user.email, user: user, bounce_key: nil)
+    email, name = Email::Receiver.new(email(:existing_user)).parse_from_field
+    expect(email).to eq("existing@bar.com")
+    expect(name).to eq("Foo Bar")
+  end
+
   it "strips null bytes from the subject" do
     expect do
       process(:null_byte_in_subject)

GitHub

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This is kinda bad and should be backported. @vinothkannans, can you take care of it?

No, it won’t affect the stable version. It’s a bug from my recent change in commit.

Beta needs it at least :wink:

Ah, we released new beta 16 hours ago. Backporting…

2 Likes

It’s done. I hope the backport is added correctly. It says beta branch is 404 commits ahead. But it all looks like the version bumps and other backports.

1 Like

Looks good :+1:

Thanks

1 Like