From 515778eabbb6a941962dc04b96759949aa165aae Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Thu, 21 Nov 2024 10:58:54 +0800 Subject: [PATCH] chore: Disable throwing error for malformed to address (#10464) We don't need to raise error on sentry for malformed to address as it is already logged. Fixes: https://linear.app/chatwoot/issue/CW-3151/standarderror-invalid-email-to-address-header-standarderror --- app/mailboxes/application_mailbox.rb | 13 +++++------- spec/mailboxes/application_mailbox_spec.rb | 24 ++++++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/mailboxes/application_mailbox.rb b/app/mailboxes/application_mailbox.rb index 6a1902ce4..4fe931b9f 100644 --- a/app/mailboxes/application_mailbox.rb +++ b/app/mailboxes/application_mailbox.rb @@ -8,12 +8,12 @@ class ApplicationMailbox < ActionMailbox::Base # routes as a reply to existing conversations routing( - ->(inbound_mail) { reply_uuid_mail?(inbound_mail) || in_reply_to_mail?(inbound_mail) } => :reply + ->(inbound_mail) { valid_to_address?(inbound_mail) && (reply_uuid_mail?(inbound_mail) || in_reply_to_mail?(inbound_mail)) } => :reply ) # routes as a new conversation in email channel routing( - ->(inbound_mail) { EmailChannelFinder.new(inbound_mail.mail).perform.present? } => :support + ->(inbound_mail) { valid_to_address?(inbound_mail) && EmailChannelFinder.new(inbound_mail.mail).perform.present? } => :support ) # catchall @@ -37,8 +37,6 @@ class ApplicationMailbox < ActionMailbox::Base # checks if follow this pattern send it to reply_mailbox # reply+@ 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) @@ -48,13 +46,12 @@ class ApplicationMailbox < ActionMailbox::Base # 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) + def valid_to_address?(inbound_mail) to_address_class = inbound_mail.mail.to&.class - - return if to_address_class == Mail::AddressContainer + return true 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}" + false end end end diff --git a/spec/mailboxes/application_mailbox_spec.rb b/spec/mailboxes/application_mailbox_spec.rb index 733ec5523..f4f28d811 100644 --- a/spec/mailboxes/application_mailbox_spec.rb +++ b/spec/mailboxes/application_mailbox_spec.rb @@ -69,18 +69,26 @@ RSpec.describe ApplicationMailbox do 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') + let(:logger) { double } + + before do + allow(Rails).to receive(:logger).and_return(logger) + allow(logger).to receive(:error) end - it 'raises another error when mail.to header is malformed' do + it 'will not raise error when mail.to header is malformed format 1' do + expect(logger).to receive(:error).with("Email to address header is malformed `#{mail_with_invalid_to_address.mail.to}`") + expect do + described_class.route mail_with_invalid_to_address + end.not_to raise_error + end + + it 'will not raise error when mail.to header is malformed format 2' do + expect(logger).to receive(:error).with("Email to address header is malformed `#{mail_with_invalid_to_address_2.mail.to}`") + 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.not_to raise_error end end end