Browse Source

Fixes #3697 - Mix of binary encoded ISO-8859-1 data in header fields (e.g. to) fails mail processing

Mantas 3 years ago
parent
commit
8b5596647a
3 changed files with 34 additions and 6 deletions
  1. 9 6
      app/models/channel/email_parser.rb
  2. 16 0
      test/data/mail/mail105.box
  3. 9 0
      test/data/mail/mail105.yml

+ 9 - 6
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

+ 16 - 0
test/data/mail/mail105.box

@@ -0,0 +1,16 @@
+From: Martin Edenhofer <martin@example.com>
+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: <BC182994-03FA-4DC5-8202-98CBFACA0887@example.com>
+To: metest@znuny.com, "=?iso-8859-1?Q?G=FCnther_John_=7C_Example_GmbH?=" <k.günther@example.com>
+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.

+ 9 - 0
test/data/mail/mail105.yml

@@ -0,0 +1,9 @@
+--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
+from: Martin Edenhofer <martin@example.com>
+from_email: martin@example.com
+from_display_name: Martin Edenhofer
+to: metest@znuny.com, "Günther John | Example GmbH" <k.günther@example.com>
+subject: aaäöüßad asd
+body: no visible content
+content_type: text/plain
+attachments: []