FIX: Don't create staged users when processing forwarded email fails

FIX: Don’t create staged users when processing forwarded email fails

diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index 9af3d15..904f833 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -802,13 +802,13 @@ module Email
 
       return false if email.blank? || !email["@"]
 
-      embedded_user = find_or_create_user(email, display_name)
       raw = try_to_encode(embedded.decoded, "UTF-8").presence || embedded.to_s
       title = embedded.subject.presence || subject
 
       case destination[:type]
       when :group
         group = destination[:obj]
+        embedded_user = find_or_create_user(email, display_name)
         post = create_topic(user: embedded_user,
                             raw: raw,
                             title: title,
@@ -825,6 +825,7 @@ module Email
         return false if user.staged? && !category.email_in_allow_strangers
         return false if !user.has_trust_level?(SiteSetting.email_in_min_trust)
 
+        embedded_user = find_or_create_user(email, display_name)
         post = create_topic(user: embedded_user,
                             raw: raw,
                             title: title,
diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb
index a834f7c..c288a0e 100644
--- a/spec/components/email/receiver_spec.rb
+++ b/spec/components/email/receiver_spec.rb
@@ -1043,7 +1043,13 @@ describe Email::Receiver do
         staged_user_count = User.where(staged: true).count
         User.expects(:create).never
         User.expects(:create!).never
-        expect { process(email_name) }.to raise_error(expected_exception)
+
+        if expected_exception
+          expect { process(email_name) }.to raise_error(expected_exception)
+        else
+          process(email_name)
+        end
+
         expect(User.where(staged: true).count).to eq(staged_user_count)
       end
     end
@@ -1159,6 +1165,24 @@ describe Email::Receiver do
       context "when the email address isn't matching the one we sent the notification to" do
         include_examples "does not create staged users", :reply_user_not_matching, Email::Receiver::ReplyUserNotMatchingError
       end
+
+      context "when forwarded emails are enabled" do
+        before do
+          SiteSetting.enable_forwarded_emails = true
+        end
+
+        context "when a reply contains a forwareded email" do
+          include_examples "does not create staged users", :reply_and_forwarded
+        end
+
+        context "forwarded email to category that doesn't allow strangers" do
+          before do
+            category.update!(email_in: "team@bar.com", email_in_allow_strangers: false)
+          end
+
+          include_examples "cleans up staged users", :forwarded_email_1, Email::Receiver::StrangersNotAllowedError
+        end
+      end
     end
 
     context "replying without key is allowed" do
diff --git a/spec/fixtures/emails/reply_and_forwarded.eml b/spec/fixtures/emails/reply_and_forwarded.eml
new file mode 100644
index 0000000..615cf12
Binary files /dev/null and b/spec/fixtures/emails/reply_and_forwarded.eml differ

GitHub sha: 4a1755b7

1 Like

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