From 8b5596647a714a71e404b3fcec351fc2b0b50e15 Mon Sep 17 00:00:00 2001 From: Mantas Date: Tue, 17 Aug 2021 15:40:23 +0300 Subject: [PATCH] Fixes #3697 - Mix of binary encoded ISO-8859-1 data in header fields (e.g. to) fails mail processing --- app/models/channel/email_parser.rb | 15 +++++++++------ test/data/mail/mail105.box | 16 ++++++++++++++++ test/data/mail/mail105.yml | 9 +++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 test/data/mail/mail105.box create mode 100644 test/data/mail/mail105.yml diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 427a13562..9c132dc48 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -563,17 +563,20 @@ process unprocessable_mails (tmp/unprocessable_mail/*.eml) again imported_fields = mail.header.fields.map do |f| begin value = if f.value.match?(ISO2022JP_REGEXP) - header_field_unpack_japanese(f) + value = header_field_unpack_japanese(f) else - f.to_utf8 + f.decoded.to_utf8 end - - if value.blank? - value = f.decoded.to_utf8 - end # fields that cannot be cleanly parsed fallback to the empty string rescue Mail::Field::IncompleteParseError value = '' + rescue Encoding::CompatibilityError => e + try_iso88591 = f.value.force_encoding('iso-8859-1').encode('utf-8') + + raise e if !try_iso88591.is_utf8? + + f.value = try_iso88591 + value = f.decoded.to_utf8 rescue value = f.decoded.to_utf8(fallback: :read_as_sanitized_binary) end diff --git a/test/data/mail/mail105.box b/test/data/mail/mail105.box new file mode 100644 index 000000000..148b5aee0 --- /dev/null +++ b/test/data/mail/mail105.box @@ -0,0 +1,16 @@ +From: Martin Edenhofer +Content-Transfer-Encoding: quoted-printable +Subject: =?iso-8859-1?Q?aa=E4=F6=FC=DFad_asd?= +X-Universally-Unique-Identifier: d12c15d2-e6d6-4ccd-86c7-abc2c3d0a2a2 +Date: Fri, 4 May 2012 14:01:03 +0200 +Message-Id: +To: metest@znuny.com, "=?iso-8859-1?Q?G=FCnther_John_=7C_Example_GmbH?=" +Mime-Version: 1.0 (Apple Message framework v1257) +Content-Type: multipart/mixed; boundary="----_=_NextPart_000_03BED81D.6103DF93" + +=E4=F6=FC=DF ad asd + +-Martin + +-- +Old programmers never die. They just branch to a new address. diff --git a/test/data/mail/mail105.yml b/test/data/mail/mail105.yml new file mode 100644 index 000000000..38edc88fa --- /dev/null +++ b/test/data/mail/mail105.yml @@ -0,0 +1,9 @@ +--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess +from: Martin Edenhofer +from_email: martin@example.com +from_display_name: Martin Edenhofer +to: metest@znuny.com, "Günther John | Example GmbH" +subject: aaäöüßad asd +body: no visible content +content_type: text/plain +attachments: []