From 4ec64cf8507a67eb71e3014e5fd3a5e85f28d2c5 Mon Sep 17 00:00:00 2001 From: Martin Gruner Date: Tue, 12 Apr 2022 07:59:10 +0200 Subject: [PATCH] Fixes: #3867 - parsing incoming mails breaks on this line "Reply-To: <>". (cherry picked from commit e3e64f1087ff9904a0b99573ebd7e9a0e18f4629) --- .../channel/filter/reply_to_based_sender.rb | 4 +- .../filter/reply_to_based_sender_spec.rb | 66 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 spec/models/channel/filter/reply_to_based_sender_spec.rb diff --git a/app/models/channel/filter/reply_to_based_sender.rb b/app/models/channel/filter/reply_to_based_sender.rb index ecd206e24..b8904f34a 100644 --- a/app/models/channel/filter/reply_to_based_sender.rb +++ b/app/models/channel/filter/reply_to_based_sender.rb @@ -4,7 +4,9 @@ module Channel::Filter::ReplyToBasedSender def self.run(_channel, mail, _transaction_params) - reply_to = mail[:'reply-to'] + return if mail[:'reply-to'].blank? + + reply_to = mail[:'reply-to'].gsub('<>', '').strip return if reply_to.blank? setting = Setting.get('postmaster_sender_based_on_reply_to') diff --git a/spec/models/channel/filter/reply_to_based_sender_spec.rb b/spec/models/channel/filter/reply_to_based_sender_spec.rb new file mode 100644 index 000000000..0aae90550 --- /dev/null +++ b/spec/models/channel/filter/reply_to_based_sender_spec.rb @@ -0,0 +1,66 @@ +# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ + +require 'rails_helper' + +RSpec.describe Channel::Filter::ReplyToBasedSender, type: :channel_filter do + describe '.run' do + let(:mail_hash) { Channel::EmailParser.new.parse(<<~RAW.chomp) } + From: daffy.duck@acme.corp + To: batman@marvell.com + Subject: Anvil + Reply-To: #{reply_to} + + I can haz anvil! + RAW + + before do + Setting.set('postmaster_sender_based_on_reply_to', 'as_sender_of_email') + end + + context 'when empty reply-to' do + let(:reply_to) { '' } + + it 'keeps from' do + expect { filter(mail_hash) } + .not_to change { mail_hash[:from] } + end + end + + context 'when empty reply-to realname and invalid address' do + let(:reply_to) { '<>' } + + it 'keeps from' do + expect { filter(mail_hash) } + .not_to change { mail_hash[:from] } + end + end + + context 'when valid reply-to address' do + let(:reply_to) { '' } + + it 'sets from to reply-to address' do + expect { filter(mail_hash) } + .to change { mail_hash[:from] }.to('bugs.bunny@acme.corp') + end + end + + context 'when valid reply-to realname and address' do + let(:reply_to) { 'Bugs Bunny ' } + + it 'sets from to reply-to realname and address' do + expect { filter(mail_hash) } + .to change { mail_hash[:from] }.to('Bugs Bunny ') + end + end + + context 'when valid reply-to realname and invalid address' do + let(:reply_to) { '"Bugs Bunny" <>' } + + it 'sets from to reply-to realname' do + expect { filter(mail_hash) } + .to change { mail_hash[:from] }.to('"Bugs Bunny"') + end + end + + end +end