From f98545ab9e9dd4a3ba84385956ad429e7a6c1fca Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Mon, 11 Sep 2023 17:02:08 +0100 Subject: [PATCH] Fix uuencoded attachment processing For attachments which are encoded with Unix-to-Unix encoding we are unable process some attachments in order to apply masks or censor rules to them. This is down to when the attachments are extracted and saved into the database. It happens outside of our `MailHandler` library which we're using to retrieve the original unmasked attachment body for processing. This change fixes this by checking plain text attachments for begin/end markers and calculating the relevant hexdigest for comparison. Fixes #7877 --- lib/mail_handler/backends/mail_backend.rb | 11 +++++++++++ .../backends/mail_backend_spec.rb | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index b8fff3ee42..945704f725 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -424,6 +424,17 @@ def caluclate_hexdigest(body) mail, body: mail_body, nested: true ) unless mail_body.empty? + return attributes if attributes + + # check uuencoded attachments which can be located in plain text + uuencoded_attributes = all_attributes.inject([]) do |acc, attrs| + next acc unless attrs[:content_type] == 'text/plain' + acc += uudecode(attrs[:body], attrs[:url_part_number]) + end + attributes ||= uuencoded_attributes.find do |attrs| + attrs[:hexdigest] == hexdigest + end + attributes end diff --git a/spec/lib/mail_handler/backends/mail_backend_spec.rb b/spec/lib/mail_handler/backends/mail_backend_spec.rb index 648e8f34ee..09fdc6fbd1 100644 --- a/spec/lib/mail_handler/backends/mail_backend_spec.rb +++ b/spec/lib/mail_handler/backends/mail_backend_spec.rb @@ -443,12 +443,26 @@ EML ).to_s + mail_with_uuencoded = Mail.new( + <<~EML + Subject: Cat + + Hi Cat! + + begin 644 cat.txt + #0V%T + ` + end + EML + ).to_s + Mail.new do add_file filename: 'crlf.txt', content: "foo\r\nfoo" add_file filename: 'lf.txt', content: "bar\nbar" add_file filename: 'crlf-non-ascii.txt', content: "Aberdâr\r\n" add_file filename: 'lf-non-ascii.txt', content: "Aberdâr\n" add_file filename: 'mail.eml', content: mail_attachment + add_file filename: 'uuencoded.eml', content: mail_with_uuencoded end end @@ -494,6 +508,11 @@ it { is_expected.to include(body: "bar\nbar") } end + context 'when body has been uuencoded' do + let(:body) { "Cat" } + it { is_expected.to include(body: "Cat") } + end + context 'when body does not match' do let(:body) { 'this does not match' } it { is_expected.to eq(nil) }