Skip to content

Commit edd9c9c

Browse files
committed
Improve resilience to unparsable outlook attachments
Sometimes we receive messages with outlook attachments that can't be parsed due to an issue in mapi [1]. There's a potential fix [2] but it conflicts [3] with an existing patch we apply [4]. This at least allows users to download the raw attachment, rather than us preventing the entire request from loading because we raise an exception. It's not easy to include an attachment in the specs to replicate a real error case due to the complexity of removing PII, so I've just stubbed the call to `.open` as we don't care about the specifics in this spec. `script/handle-mail-replies` needs an explicit require as we minimise the load path for that script. Part of #5783. [1] aquasync/ruby-msg#15 [2] aquasync/ruby-msg#16 [3] #5783 (comment) [4] mysociety/ruby-msg#3
1 parent 1f3f630 commit edd9c9c

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

lib/mail_handler/backends/mail_backend.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def cc(val = nil)
3535
module MailHandler
3636
module Backends
3737
module MailBackend
38+
include ConfigHelper
3839

3940
def backend
4041
'Mail'
@@ -186,7 +187,18 @@ def decode_attached_part(part, parent_mail)
186187
part.content_type = 'text/plain'
187188
end
188189
elsif is_outlook?(part)
189-
part.rfc822_attachment = mail_from_outlook(part.body.decoded)
190+
begin
191+
part.rfc822_attachment = mail_from_outlook(part.body.decoded)
192+
rescue Encoding::CompatibilityError => e
193+
if send_exception_notifications?
194+
data = { message: 'Exception while parsing outlook attachment.',
195+
parent_mail: parent_mail.inspect }
196+
ExceptionNotifier.notify_exception(e, data: data)
197+
end
198+
199+
part.rfc822_attachment = nil
200+
end
201+
190202
if part.rfc822_attachment.nil?
191203
# Attached mail didn't parse, so treat as binary
192204
part.content_type = 'application/octet-stream'

script/handle-mail-replies.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
$:.push(File.join($alaveteli_dir, 'lib'))
2626
load 'configuration.rb'
2727

28+
$:.push(File.join($alaveteli_dir, 'app', 'helpers'))
29+
require 'config_helper'
30+
2831
$:.push(File.join($alaveteli_dir, 'lib', 'mail_handler'))
2932
require 'mail_handler'
3033
require 'reply_handler'

spec/lib/mail_handler/backends/mail_backend_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,11 @@
286286

287287
end
288288

289-
289+
describe '#decode_attached_part' do
290+
it 'does not error if mapi cannot parse a part' do
291+
allow(Mapi::Msg).to receive(:open).and_raise(Encoding::CompatibilityError)
292+
mail = get_fixture_mail('incoming-request-oft-attachments.email')
293+
expect { decode_attached_part(mail.parts.last, mail) }.not_to raise_error
294+
end
295+
end
290296
end

0 commit comments

Comments
 (0)