Browse Source

Improved error handling of invalid emails.

Martin Edenhofer 7 years ago
parent
commit
e376a0f872
2 changed files with 37 additions and 3 deletions
  1. 7 3
      app/models/channel/email_parser.rb
  2. 30 0
      test/unit/email_process_test.rb

+ 7 - 3
app/models/channel/email_parser.rb

@@ -381,7 +381,7 @@ class Channel::EmailParser
           filename = if mail_local[:subject].present?
                        "#{mail_local[:subject]}.eml"
                      elsif headers_store['Content-Description'].present?
-                       "#{headers_store['Content-Description']}.eml"
+                       "#{headers_store['Content-Description']}.eml".to_s.force_encoding('utf-8')
                      else
                        'Mail.eml'
                      end
@@ -415,7 +415,7 @@ class Channel::EmailParser
         map.each do |type, ext|
           next if headers_store['Content-Type'] !~ /^#{Regexp.quote(type)}/i
           filename = if headers_store['Content-Description'].present?
-                       "#{headers_store['Content-Description']}.#{ext[0]}"
+                       "#{headers_store['Content-Description']}.#{ext[0]}".to_s.force_encoding('utf-8')
                      else
                        "#{ext[1]}.#{ext[0]}"
                      end
@@ -661,11 +661,15 @@ returns
 
         # store attachments
         mail[:attachments]&.each do |attachment|
+          filename = attachment[:filename].force_encoding('utf-8')
+          if !filename.force_encoding('UTF-8').valid_encoding?
+            filename = filename.encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
+          end
           Store.add(
             object: 'Ticket::Article',
             o_id: article.id,
             data: attachment[:data],
-            filename: attachment[:filename],
+            filename: filename,
             preferences: attachment[:preferences]
           )
         end

+ 30 - 0
test/unit/email_process_test.rb

@@ -2669,6 +2669,36 @@ Some Text',
           ],
         },
       },
+      {
+        data: IO.binread('test/fixtures/mail64.box'),
+        success: true,
+        result: {
+          0 => {
+            priority: '2 normal',
+            title: 'AW: OTRS / Anfrage OTRS Einführung/Präsentation [Ticket#11545]',
+          },
+          1 => {
+            from: 'Martin Edenhofer <martin@example.de>',
+            sender: 'Customer',
+            type: 'email',
+            body: 'Enjoy!<div>
+<br><div>-Martin<br><span class="js-signatureMarker"></span><br>--<br>Old programmers never die. They just branch to a new address.<br>
+</div>
+<br><div><img src="cid:485376C9-2486-4351-B932-E2010998F579@home" style="width:640px;height:425px;"></div>
+</div>',
+          },
+        },
+        verify: {
+          users: [
+            {
+              firstname: 'Martin',
+              lastname: 'Edenhofer',
+              fullname: 'Martin Edenhofer',
+              email: 'martin@example.de',
+            },
+          ],
+        },
+      },
     ]
     assert_process(files)
   end