Skip to content

Commit

Permalink
fix: Raise error if email to_header is invalid (#8688)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnu-narayanan committed Feb 21, 2024
1 parent 9911c5d commit d53097f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/mailboxes/application_mailbox.rb
Expand Up @@ -37,10 +37,24 @@ def in_reply_to_matches?(in_reply_to)
# checks if follow this pattern send it to reply_mailbox
# reply+<conversation-uuid>@<mailer-domain.com>
def reply_uuid_mail?(inbound_mail)
validate_to_address(inbound_mail)

inbound_mail.mail.to&.any? do |email|
conversation_uuid = email.split('@')[0]
conversation_uuid.match?(REPLY_EMAIL_UUID_PATTERN)
end
end

# if mail.to returns a string, then it is a malformed `to` header
# valid `to` header will be of type Mail::AddressContainer
# validate if the to address is of type string
def validate_to_address(inbound_mail)
to_address_class = inbound_mail.mail.to&.class

return if to_address_class == Mail::AddressContainer

Rails.logger.error "Email to address header is malformed `#{inbound_mail.mail.to}`"
raise StandardError, "Invalid email to address header #{inbound_mail.mail.to}"
end
end
end
29 changes: 29 additions & 0 deletions spec/fixtures/files/mail_with_invalid_to.eml
@@ -0,0 +1,29 @@
X-Original-To: bd84c730a1ac7833e4d27253804516f7@reply.chatwoot.com
Received: from mail.planetmars.com (mxd [192.168.1.1]) by mx.sendgrid.net with ESMTP id AAAA-bCCCCCC5DeeeFFgg for <bd84c730a1ac7833e4d27253804516f7@reply.chatwoot.com>; Sun, 31 Dec 2023 22:32:23.586 +0000 (UTC)
From: "Mark Whatney" <mark@planetmars.com>
To: <vishnu@chatwoot.com>vishnu@chatwoot.com
Subject: stranded in mars
Date: Mon, 1 Jan 2024 06:31:44 +0800
Message-ID: <1234560e0123c05b4bbf83c828b1688a93c7@com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary=15688136a4ad411d82b004fae6e46549
X-Exim-Id: 1234560e0123c05b4bbf83c828b1688a93c7

This is a multipart message in MIME format.

--15688136a4ad411d82b004fae6e46549
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
hey v, can i get some help over here?
--15688136a4ad411d82b004fae6e46549
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable

hey v, can i get some help over here?

--15688136a4ad411d82b004fae6e46549--
29 changes: 29 additions & 0 deletions spec/fixtures/files/mail_with_invalid_to_2.eml
@@ -0,0 +1,29 @@
X-Original-To: bd84c730a1ac7833e4d27253804516f7@reply.chatwoot.com
Received: from mail.planetmars.com (mxd [192.168.1.1]) by mx.sendgrid.net with ESMTP id AAAA-bCCCCCC5DeeeFFgg for <bd84c730a1ac7833e4d27253804516f7@reply.chatwoot.com>; Sun, 31 Dec 2023 22:32:23.586 +0000 (UTC)
From: "Mark Whatney" <mark@planetmars.com>
To: vishnu@chatwoot.com www.chatwoot.com
Subject: stranded in mars
Date: Mon, 1 Jan 2024 06:31:44 +0800
Message-ID: <1234560e0123c05b4bbf83c828b1688a93c7@com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary=15688136a4ad411d82b004fae6e46549
X-Exim-Id: 1234560e0123c05b4bbf83c828b1688a93c7

This is a multipart message in MIME format.

--15688136a4ad411d82b004fae6e46549
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
hey v, can i get some help over here?
--15688136a4ad411d82b004fae6e46549
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable

hey v, can i get some help over here?

--15688136a4ad411d82b004fae6e46549--
18 changes: 18 additions & 0 deletions spec/mailboxes/application_mailbox_spec.rb
Expand Up @@ -10,6 +10,8 @@
let(:reply_mail_without_uuid) { create_inbound_email_from_fixture('reply.eml') }
let(:reply_mail_with_in_reply_to) { create_inbound_email_from_fixture('in_reply_to.eml') }
let(:support_mail) { create_inbound_email_from_fixture('support.eml') }
let(:mail_with_invalid_to_address) { create_inbound_email_from_fixture('mail_with_invalid_to.eml') }
let(:mail_with_invalid_to_address_2) { create_inbound_email_from_fixture('mail_with_invalid_to_2.eml') }

describe 'Default' do
it 'catchall mails route to Default Mailbox' do
Expand Down Expand Up @@ -65,5 +67,21 @@
described_class.route reply_cc_mail
end
end

describe 'Invalid Mail To Address' do
it 'raises error when mail.to header is malformed' do
expect do
described_class.route mail_with_invalid_to_address
end.to raise_error(StandardError,
'Invalid email to address header <vishnu@chatwoot.com>vishnu@chatwoot.com')
end

it 'raises another error when mail.to header is malformed' do
expect do
described_class.route mail_with_invalid_to_address_2
end.to raise_error(StandardError,
'Invalid email to address header vishnu@chatwoot.com www.chatwoot.com')
end
end
end
end

0 comments on commit d53097f

Please sign in to comment.