Skip to content

Commit

Permalink
Fix embedded attachment processing
Browse files Browse the repository at this point in the history
For attachments which are embedded within a RFC822 attachment, were
unable process some attachment if they were received a long time ago.

This is down to changes in the underlying mail gem which results in
slight difference in the headers which get appended to the attachment
body.

This change fixes this by only matching against the body of embedded
attachments, ignoring headers and any slight changes.

Fixes #7876
  • Loading branch information
gbp committed Aug 29, 2023
1 parent 648ad8e commit 288518b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 6 additions & 5 deletions lib/mail_handler/backends/mail_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,13 @@ def extract_attached_message_headers(leaf)

# Generate a hash of the attributes associated with each significant part
# of a Mail object
def get_attachment_attributes(mail)
def get_attachment_attributes(mail, extract_headers: true)
get_attachment_leaves(mail).map do |leaf|
body = get_part_body(leaf)

if leaf.within_rfc822_attachment
within_rfc822_subject = get_within_rfc822_subject(leaf)
body = extract_attached_message_headers(leaf)
body = extract_attached_message_headers(leaf) if extract_headers
end

leaf_attributes = { url_part_number: leaf.url_part_number,
Expand All @@ -394,7 +394,7 @@ def attachment_body_for_hexdigest(mail, hexdigest:)
end

def attempt_to_find_original_attachment_attributes(mail, body:, nested: false)
all_attributes = get_attachment_attributes(mail)
all_attributes = get_attachment_attributes(mail, extract_headers: false)

attributes = all_attributes.find do |attrs|
# ensure both bodies have the same line endings
Expand All @@ -409,9 +409,10 @@ def attempt_to_find_original_attachment_attributes(mail, body:, nested: false)

return attributes if nested

mail_body = Mail.new(body).body.to_s
attributes ||= attempt_to_find_original_attachment_attributes(
mail, body: Mail.new(body).to_s, nested: true
)
mail, body: mail_body, nested: true
) unless mail_body.empty?

attributes
end
Expand Down
16 changes: 9 additions & 7 deletions spec/lib/mail_handler/backends/mail_backend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,16 @@
let(:mail) do
mail_attachment = Mail.new(
<<~EML
Date: Tue, 08 Aug 2023 10:00:00 +0000
Message-ID: <64d611ca31906_ccf71e5039542@localhost>
Subject: Attached email
Hello world
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: 'mail.txt', content: mail_attachment
add_file filename: 'mail.eml', content: mail_attachment
end
end

Expand All @@ -465,14 +466,15 @@
it { is_expected.to include(body: "bar\nbar") }
end

context 'when body missing leading zero on dates' do
context 'when attached email headers are different' do
let(:body) do
<<~EML
Date: Tue, 8 Aug 2023 10:00:00 +0000
Message-ID: <64d611ca31906_ccf71e5039542@localhost>
Subject: A different subject
Hello world
EML
end
it { expect(attributes[:body]).to include('08 Aug 2023') }
it { is_expected.to include(body: "Hello world\n") }
end

context 'when body does not match' do
Expand Down

0 comments on commit 288518b

Please sign in to comment.