chore: Handle invalid email address in IMAP channel (#9450)

This commit is contained in:
Sojan Jose
2024-05-09 20:25:26 -07:00
committed by GitHub
parent 05f0398b86
commit 4284c123a6
2 changed files with 25 additions and 0 deletions

View File

@@ -12,6 +12,9 @@ class Imap::ImapMailbox
# prevent loop from chatwoot notification emails
return if notification_email_from_chatwoot?
# Stop processing if email format doesn't match Chatwoot supported mail format
return unless email_from_valid_email?
ActiveRecord::Base.transaction do
find_or_create_contact
find_or_create_conversation
@@ -34,6 +37,18 @@ class Imap::ImapMailbox
@processed_mail = MailPresenter.new(@inbound_mail, @account)
end
def email_from_valid_email?
Rails.logger.info("Processing Email from: #{@processed_mail.original_sender} : inbox #{@inbox.id}")
# validate email with Devise.email_regexp
if Devise.email_regexp.match?(@processed_mail.original_sender)
true
else
Rails.logger.error("Email from: #{@processed_mail.original_sender} : inbox #{@inbox.id} is invalid")
false
end
end
def find_conversation_by_in_reply_to
return if in_reply_to.blank?

View File

@@ -54,6 +54,16 @@ RSpec.describe Imap::ImapMailbox do
end
end
context 'when a new email with invalid from' do
let(:inbound_mail) { create_inbound_email_from_mail(from: 'invalidemail', to: 'imap@gmail.com', subject: 'Hello!') }
it 'does not create a new conversation' do
allow(Rails.logger).to receive(:error)
class_instance.process(inbound_mail.mail, channel)
expect(Rails.logger).to have_received(:error).with("Email from: invalidemail : inbox #{inbox.id} is invalid")
end
end
context 'when a reply for existing email conversation' do
let(:prev_conversation) { create(:conversation, account: account, inbox: channel.inbox, assignee: agent) }
let(:reply_mail) do