From acb06e7df670b2dc1556ee3975d41862d602cc92 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Mon, 15 Nov 2021 19:15:51 +0530 Subject: [PATCH] chore: Prevent notification email loop (#3386) Configuring an agent email also as a support email inbox leads to conversations getting created in a loop if notifications were also configured to the same email. --- app/mailboxes/mailbox_helper.rb | 5 + app/mailboxes/support_mailbox.rb | 3 + spec/fixtures/files/notification.eml | 169 +++++++++++++++++++++++++ spec/mailboxes/support_mailbox_spec.rb | 13 ++ 4 files changed, 190 insertions(+) create mode 100644 spec/fixtures/files/notification.eml diff --git a/app/mailboxes/mailbox_helper.rb b/app/mailboxes/mailbox_helper.rb index 55f7663ff..15379bb86 100644 --- a/app/mailboxes/mailbox_helper.rb +++ b/app/mailboxes/mailbox_helper.rb @@ -28,4 +28,9 @@ module MailboxHelper end @message.save! end + + def notification_email_from_chatwoot? + # notification emails are send via mailer sender email address. so it should match + @processed_mail.original_sender == Mail::Address.new(ENV.fetch('MAILER_SENDER_EMAIL', 'Chatwoot ')).address + end end diff --git a/app/mailboxes/support_mailbox.rb b/app/mailboxes/support_mailbox.rb index 887232dd6..9de2f79c2 100644 --- a/app/mailboxes/support_mailbox.rb +++ b/app/mailboxes/support_mailbox.rb @@ -7,6 +7,9 @@ class SupportMailbox < ApplicationMailbox :decorate_mail def process + # prevent loop from chatwoot notification emails + return if notification_email_from_chatwoot? + ActiveRecord::Base.transaction do find_or_create_contact find_or_create_conversation diff --git a/spec/fixtures/files/notification.eml b/spec/fixtures/files/notification.eml new file mode 100644 index 000000000..98ecbe34a --- /dev/null +++ b/spec/fixtures/files/notification.eml @@ -0,0 +1,169 @@ +Delivered-To: sojan@chatwoot.com +Received: by 2002:ac9:4481:0:0:0:0:0 with SMTP id s1csp1820446och; + Sat, 1 Feb 2020 05:23:53 -0800 (PST) +X-Google-Smtp-Source: APXvYqxV20fwjufESyTOPFd/uwBzzlZLYt5TXijaypShpt1RnEjy7Jw2JtohXqAUnJb4JiRM0FPA +X-Received: by 2002:a63:e80d:: with SMTP id s13mr16094686pgh.134.1580563433783; + Sat, 01 Feb 2020 05:23:53 -0800 (PST) +ARC-Seal: i=1; a=rsa-sha256; t=1580563433; cv=none; + d=google.com; s=arc-20160816; + b=j8nqQpLKON6089iHd6tWOjgVPNwLapRZawpbxtOqKLw7YIofOGRSR7ktTwC6xEf0k2 + E7bGbM3rNUfSuL8b90TF8BqYhGPqZWNVQmdWgfSeYzxOll6o+ilN1XaPu8tiEwn32dv3 + /GdAIvKJcV2FmTmm/1mn7nHRcrTt8essC4/LHcTmAjivYwMkFFAAmaW/x3cGjQoVqweQ + YjLHIvJ4Gf8VA59BTQW6v5mp40LTJgqhMI0KCtF823wpjh+4hb2ZzC3tDFOI755Yb+Eu + UrQHhRzxIblUa2HjNfCXIaTbPKsAwKQYeMtxciGGThxNqOVtH7QAfYHAueu9aGwEgd54 + r5Dw== +ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; + h=feedback-id:content-transfer-encoding:mime-version:subject + :message-id:to:from:date:dkim-signature:dkim-signature; + bh=KJNEbnNwTq190bQAUmxmxwtVMZr3HECa101UQNDCVw0=; + b=pq1j5E9GCLApAY+LvKyaGu4XavbKvwFp9GWNsA25PVwkicme7mFHR+3U3PBO3qQwyu + lnAGFv8CneaOAOEbuxncmJnTX8qnrtw0K6vfPN1POAiIgn/WVg1ib/mfj3Tw7YSgLd/i + O6UyxWKS+AYu+CiHAxhGLHFue3tJ8Vba3XFRoSHTbDK0Y8XT5XK3Ex0Lx8QdiDseTkI/ + HXIxYC+73WeLiZNsvhQdM0gftESktx41baytvHUNMEUu+VMhI3fgdkEUDa3pU64yLD/p + lQSmxl9i1fD/9KOWZFvs6AdpHjpHfuKk7tNiMAlvwHRT2PFKVZqmN7ZWVtsotdjZ/LBS + Tkpg== +ARC-Authentication-Results: i=1; mx.google.com; + dkim=pass header.i=@chatwoot.com header.s=jywwku4pypva75vx2ayl2qfvg46arq6j header.b="S/vIfBFO"; + dkim=pass header.i=@amazonses.com header.s=gdwg2y3kokkkj5a55z2ilkup5wp5hhxx header.b=b0rWlX4V; + spf=pass (google.com: domain of 0101017000ec0605-2b97eb32-482d-4357-8715-29b1d4a7c38b-000000@us-west-2.amazonses.com designates 54.240.27.27 as permitted sender) smtp.mailfrom=0101017000ec0605-2b97eb32-482d-4357-8715-29b1d4a7c38b-000000@us-west-2.amazonses.com +Return-Path: <0101017000ec0605-2b97eb32-482d-4357-8715-29b1d4a7c38b-000000@us-west-2.amazonses.com> +Received: from a27-27.smtp-out.us-west-2.amazonses.com (a27-27.smtp-out.us-west-2.amazonses.com. [54.240.27.27]) + by mx.google.com with ESMTPS id az9si3221498plb.69.2020.02.01.05.23.53 + for + (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); + Sat, 01 Feb 2020 05:23:53 -0800 (PST) +Received-SPF: pass (google.com: domain of 0101017000ec0605-2b97eb32-482d-4357-8715-29b1d4a7c38b-000000@us-west-2.amazonses.com designates 54.240.27.27 as permitted sender) client-ip=54.240.27.27; +Authentication-Results: mx.google.com; + dkim=pass header.i=@chatwoot.com header.s=jywwku4pypva75vx2ayl2qfvg46arq6j header.b="S/vIfBFO"; + dkim=pass header.i=@amazonses.com header.s=gdwg2y3kokkkj5a55z2ilkup5wp5hhxx header.b=b0rWlX4V; + spf=pass (google.com: domain of 0101017000ec0605-2b97eb32-482d-4357-8715-29b1d4a7c38b-000000@us-west-2.amazonses.com designates 54.240.27.27 as permitted sender) smtp.mailfrom=0101017000ec0605-2b97eb32-482d-4357-8715-29b1d4a7c38b-000000@us-west-2.amazonses.com +DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=jywwku4pypva75vx2ayl2qfvg46arq6j; d=chatwoot.com; t=1580563433; h=Date:From:To:Message-ID:Subject:Mime-Version:Content-Type:Content-Transfer-Encoding; bh=sidGViYBMVWvZmddU9y4tJbPdSakZIQ0SRFD7VFPP88=; b=S/vIfBFOWSSX45lxfB5jZsT0/Jb9NZefnlLdmF8yicPNiV2/H8HtDAqyO3JqHLfM fPefZPKhYhzBqfxoEEFAY/vr7ov8XT+D4Y+VCv1zl0fiyytTntJ/uQztF5Eg61j+Tqj JPgRqJRdfW0g+E06CH1vhVPVzD8jIpUGnZ6gVEl4= +DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=gdwg2y3kokkkj5a55z2ilkup5wp5hhxx; d=amazonses.com; t=1580563433; h=Date:From:To:Message-ID:Subject:Mime-Version:Content-Type:Content-Transfer-Encoding:Feedback-ID; bh=sidGViYBMVWvZmddU9y4tJbPdSakZIQ0SRFD7VFPP88=; b=b0rWlX4Vo8FpaGzSe57iuHhxj/d57qliYmPmv0mBn/0ZEmGjkBlNYbE0An7T3Jmk Pu2TIOr71f9H/DLnMw9n8ro4Orl/ISn26uYfttP4u6iCOCEDV6/BC3xvlpSvg7R9Sin yHhY0LWM/F5zTFPO7EfOt+ibS33LXOKFseQVzpfI= +Date: Sat, 1 Feb 2020 13:23:53 +0000 +From: accounts@chatwoot.com +To: sojan@chatwoot.com +Message-ID: <0101017000ec0605-2b97eb32-482d-4357-8715-29b1d4a7c38b-000000@us-west-2.amazonses.com> +Subject: Sojan, A new conversation [ID - 873] has been assigned to you. +Mime-Version: 1.0 +Content-Type: text/html; charset=UTF-8 +Content-Transfer-Encoding: 7bit +X-SES-Outgoing: 2020.02.01-54.240.27.27 +Feedback-ID: 1.us-west-2.dvcdkuZzAcrrEXRRdlOP0Y5qm4KoH22dS4zlDs+reGg=:AmazonSES + + + + + + + + + + + + + + + + + + + + +
+
+ + + + +
+ + +

Hi Sojan,

+ +

Time to save the world. A new conversation has been assigned to you

+ +

Click here to get cracking.

+ + + + + +
+ — Team Chatwoot
+ https://www.chatwoot.com +
+
+ +
+
+ + + \ No newline at end of file diff --git a/spec/mailboxes/support_mailbox_spec.rb b/spec/mailboxes/support_mailbox_spec.rb index 6b7e91fe3..1dda7be31 100644 --- a/spec/mailboxes/support_mailbox_spec.rb +++ b/spec/mailboxes/support_mailbox_spec.rb @@ -3,6 +3,19 @@ require 'rails_helper' RSpec.describe SupportMailbox, type: :mailbox do include ActionMailbox::TestHelper + describe 'when a chatwoot notification email is received' do + let(:account) { create(:account) } + let!(:channel_email) { create(:channel_email, email: 'sojan@chatwoot.com', account: account) } + let(:notification_mail) { create_inbound_email_from_fixture('notification.eml') } + let(:described_subject) { described_class.receive notification_mail } + let(:conversation) { Conversation.where(inbox_id: channel_email.inbox).last } + + it 'shouldnt create a conversation in the channel' do + described_subject + expect(conversation.present?).to eq(false) + end + end + describe 'add mail as a new ticket in the email inbox' do let(:account) { create(:account) } let(:agent) { create(:user, email: 'agent1@example.com', account: account) }