Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix processing of attachments encoded as Latin 1 #7916

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/mail_handler/backends/mail_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,14 @@ def attempt_to_find_original_attachment_attributes(mail, body:, nested: false)
all_attributes = get_attachment_attributes(mail)

def calculate_hexdigest(body)
# ensure bodies have the same line endings
Digest::MD5.hexdigest(Mail::Utilities.binary_unsafe_to_lf(
body.rstrip
))
# ensure bodies have the same line endings and are encoded the same
Digest::MD5.hexdigest(
Mail::Utilities.binary_unsafe_to_lf(
convert_string_to_utf8(
body.rstrip
).string
)
)
end

hexdigest = calculate_hexdigest(body)
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/mail_handler/backends/mail_backend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@
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: 'latin1.txt', content: "naïve"
add_file filename: 'mail.eml', content: mail_attachment
add_file filename: 'uuencoded.eml', content: mail_with_uuencoded
end
Expand Down Expand Up @@ -492,6 +493,11 @@
it { is_expected.to include(body: "Aberdâr\n") }
end

context 'when binary body encoded as Latin 1 / ISO-8859-1' do
let(:body) { "na\xEFve".b }
it { is_expected.to include(body: "naïve") }
end

context 'when attached email headers are different' do
let(:body) do
<<~EML
Expand Down
Loading