Skip to content

Commit

Permalink
Fix uuencoded attachment processing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gbp committed Sep 15, 2023
1 parent df7d524 commit f98545a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/mail_handler/backends/mail_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions spec/lib/mail_handler/backends/mail_backend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) }
Expand Down

0 comments on commit f98545a

Please sign in to comment.