From 88e3d9e574f9f6e0b9a0f9fd8243398538dd3655 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 23 Jun 2017 13:57:24 +0200 Subject: [PATCH] Improved error handling of emails with invalid byte sequence for utf8 encoding. --- app/models/channel/email_parser.rb | 1 + test/fixtures/mail60.box | 16 ++++++++++++++++ test/unit/email_process_test.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 test/fixtures/mail60.box diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 11b36d4a6..4ec022d3f 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -151,6 +151,7 @@ class Channel::EmailParser if data[:body].empty? && mail.text_part data[:body] = mail.text_part.body.decoded data[:body] = Encode.conv(mail.text_part.charset, data[:body]) + data[:body] = data[:body].to_s.force_encoding('utf-8') if !data[:body].valid_encoding? data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?') diff --git a/test/fixtures/mail60.box b/test/fixtures/mail60.box new file mode 100644 index 000000000..8dde96cac --- /dev/null +++ b/test/fixtures/mail60.box @@ -0,0 +1,16 @@ +From: Martin Edenhofer +Content-Type: multipart/alternative; boundary="Apple-Mail=_EB2F27C4-F4CD-40C9-82F1-D115D4FFA394" +Subject: abc +Date: Fri, 4 May 2012 14:01:03 +0200 +Message-Id: +To: metest@znuny.com +Mime-Version: 1.0 (Apple Message framework v1257) + +--Apple-Mail=_EB2F27C4-F4CD-40C9-82F1-D115D4FFA394 +Content-Transfer-Encoding: base64 +Content-Type: text/plain; + charset=iso-gb2312 + +SGVyZSBpdCBnb2VzIC0gw6TDtsO8IC0g5beu5Ye65Lq6SGVyZSBpdCBnb2VzIC0g5Pb8IC0gaGkgrQ== + +--Apple-Mail=_EB2F27C4-F4CD-40C9-82F1-D115D4FFA394-- diff --git a/test/unit/email_process_test.rb b/test/unit/email_process_test.rb index 7a067cd42..3e4627d50 100644 --- a/test/unit/email_process_test.rb +++ b/test/unit/email_process_test.rb @@ -2449,6 +2449,32 @@ Some Text', ], }, }, + { + data: IO.binread('test/fixtures/mail60.box'), + success: true, + result: { + 0 => { + priority: '2 normal', + title: 'abc', + }, + 1 => { + from: 'Martin Edenhofer ', + sender: 'Customer', + type: 'email', + body: 'Here it goes - ?????? - ?????????Here it goes - ??? - hi ?', + }, + }, + verify: { + users: [ + { + firstname: 'Martin', + lastname: 'Edenhofer', + fullname: 'Martin Edenhofer', + email: 'martin@example.com', + }, + ], + }, + }, ] assert_process(files) end