Skip to content

Commit

Permalink
FIX: Handle forwarded email quotes around Reply-To display name (#14384)
Browse files Browse the repository at this point in the history
The display name can have quotes around it, which does not work
with our current comparison of a from field (in this case Reply-To)
and another header (X-Original-From), because we are not comparing
the two values in the same way. This causes an issue where the
commit here: b88d8c8 will not
work properly; the forwarded email gets the From address instead
of the Reply-To address as intended.
  • Loading branch information
martin-brennan committed Sep 20, 2021
1 parent 36bd6e8 commit 4fb7d04
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/email/receiver.rb
Expand Up @@ -637,7 +637,10 @@ def extract_from_fields_from_header(mail_object, header, comparison_headers: [])

comparison_failed = false
comparison_headers.each do |comparison_header|
comparison_failed = true if address_field.to_s != mail_object[comparison_header].to_s
if mail_object[comparison_header].to_s != "#{from_display_name} <#{from_address}>"
comparison_failed = true
break
end
end

next if comparison_failed
Expand Down
18 changes: 14 additions & 4 deletions spec/components/email/receiver_spec.rb
Expand Up @@ -870,19 +870,29 @@ def create_post_reply_key(value)
end

describe "reply-to header" do
it "handles emails where there is a reply-to address, using that instead of the from address" do
it "handles emails where there is a Reply-To address, using that instead of the from address, if X-Original-From is present" do
SiteSetting.block_auto_generated_emails = false
expect { process(:reply_to_different_to_from) }.to change(Topic, :count)
expect { process(:reply_to_different_to_from) }.to change(Topic, :count).by(1)
user = User.last
incoming = IncomingEmail.find_by(message_id: "3848c3m98r439c348mc349@test.mailinglist.com")
topic = incoming.topic
expect(incoming.from_address).to eq("arthurmorgan@reddeadtest.com")
expect(user.email).to eq("arthurmorgan@reddeadtest.com")
end

it "allows for quotes around the display name of the Reply-To address" do
SiteSetting.block_auto_generated_emails = false
expect { process(:reply_to_different_to_from_quoted_display_name) }.to change(Topic, :count).by(1)
user = User.last
incoming = IncomingEmail.find_by(message_id: "3848c3m98r439c348mc349@test.mailinglist.com")
topic = incoming.topic
expect(incoming.from_address).to eq("johnmarston@reddeadtest.com")
expect(user.email).to eq("johnmarston@reddeadtest.com")
end

it "does not use the reply-to address if an X-Original-From header is not present" do
SiteSetting.block_auto_generated_emails = false
expect { process(:reply_to_different_to_from_no_x_original) }.to change(Topic, :count)
expect { process(:reply_to_different_to_from_no_x_original) }.to change(Topic, :count).by(1)
user = User.last
incoming = IncomingEmail.find_by(message_id: "3848c3m98r439c348mc349@test.mailinglist.com")
topic = incoming.topic
Expand All @@ -892,7 +902,7 @@ def create_post_reply_key(value)

it "does not use the reply-to address if the X-Original-From header is different from the reply-to address" do
SiteSetting.block_auto_generated_emails = false
expect { process(:reply_to_different_to_from_x_original_different) }.to change(Topic, :count)
expect { process(:reply_to_different_to_from_x_original_different) }.to change(Topic, :count).by(1)
user = User.last
incoming = IncomingEmail.find_by(message_id: "3848c3m98r439c348mc349@test.mailinglist.com")
topic = incoming.topic
Expand Down
@@ -0,0 +1,16 @@
From: 'Marston, John' via Western Support <westernsupport@test.mailinglist.com>
Reply-To: =?iso-8859-1?Q?Marston=2C_John?= <johnmarston@reddeadtest.com>
To: team@bar.com
Subject: I need some support
Date: Fri, 15 Jan 2016 00:12:43 +0100
Message-ID: <3848c3m98r439c348mc349@test.mailinglist.com>
Mime-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
X-Original-From: Marston, John <johnmarston@reddeadtest.com>
Precedence: list
Mailing-list: list westernsupport@test.mailinglist.com; contact
westernsupport+owners@test.mailinglist.com
List-ID: <westernsupport@test.mailinglist.com>

Ain't no trouble.

0 comments on commit 4fb7d04

Please sign in to comment.