Fixed #2390 - Add a filter to not show emails with potential issue - Display a…

This commit is contained in:
Billy Zhou 2018-12-19 03:54:03 +01:00
parent 0c97a66d6a
commit 77794e0b26
2 changed files with 32 additions and 2 deletions

View file

@ -6,6 +6,7 @@ class Channel::EmailParser
EMAIL_REGEX = /.+@.+/ EMAIL_REGEX = /.+@.+/
RECIPIENT_FIELDS = %w[to cc delivered-to x-original-to envelope-to].freeze RECIPIENT_FIELDS = %w[to cc delivered-to x-original-to envelope-to].freeze
SENDER_FIELDS = %w[from reply-to return-path sender].freeze SENDER_FIELDS = %w[from reply-to return-path sender].freeze
EXCESSIVE_LINKS_MSG = 'This message cannot be displayed because it contains over 5,000 links. Download the raw message below and open it via an Email client if you still wish to view it.'.freeze
=begin =begin
@ -542,9 +543,13 @@ process unprocessable_mails (tmp/unprocessable_mail/*.eml) again
body_text = body_text.utf8_encode(from: message.charset, fallback: :read_as_sanitized_binary) body_text = body_text.utf8_encode(from: message.charset, fallback: :read_as_sanitized_binary)
body_text = Mail::Utilities.to_lf(body_text) body_text = Mail::Utilities.to_lf(body_text)
return body_text.html2html_strict if options[:strict_html] # plaintext body requires no processing
return body_text if !options[:strict_html]
body_text # Issue #2390 - emails with >5k HTML links should be rejected
return EXCESSIVE_LINKS_MSG if body_text.scan(/<a[[:space:]]/i).count >= 5_000
body_text.html2html_strict
end end
def collect_attachments(mail) def collect_attachments(mail)

View file

@ -138,5 +138,30 @@ RSpec.describe Channel::EmailParser, type: :model do
end end
end end
end end
context 'mail with links' do
def mock_mail(number_of_links)
link = '<a href="https://zammad.com/">Dummy Link</a> '
mail = Mail.new
mail.html_part = "<html><body>#{link * number_of_links}</body></html>"
mail
end
let(:mail_10) { mock_mail(10).to_s }
let(:mail_5k) { mock_mail(5001).to_s }
# regression test for issue 2390 - Add a postmaster filter to not show emails with potential issue
it '(>5k links) are replaced by a warning message' do
expect( described_class.new.parse(mail_5k)[:body] )
.to eql( Channel::EmailParser::EXCESSIVE_LINKS_MSG )
end
it '(10 links) are not touched' do
expect( described_class.new.parse(mail_10)[:body] )
.to start_with( '<a href="https://zammad.com/"' )
end
end
end end
end end