From e376a0f8728d19261dac19fc5c1f1ca564fed4a1 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 2 Jan 2018 14:28:34 +0100 Subject: [PATCH] Improved error handling of invalid emails. --- app/models/channel/email_parser.rb | 10 +++++++--- test/unit/email_process_test.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index b4092f593..d440f8e38 100644 --- a/app/models/channel/email_parser.rb +++ b/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 diff --git a/test/unit/email_process_test.rb b/test/unit/email_process_test.rb index f1f69b467..afe0031f9 100644 --- a/test/unit/email_process_test.rb +++ b/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 ', + sender: 'Customer', + type: 'email', + body: 'Enjoy!
+
-Martin

--
Old programmers never die. They just branch to a new address.
+
+
+
', + }, + }, + verify: { + users: [ + { + firstname: 'Martin', + lastname: 'Edenhofer', + fullname: 'Martin Edenhofer', + email: 'martin@example.de', + }, + ], + }, + }, ] assert_process(files) end