Skip to content

Commit

Permalink
Fix 7bit/base64 content transfer encoding mismatch
Browse files Browse the repository at this point in the history
Caused by picking the best transfer encoding twice in a row.

References #1128

Co-authored-by: Jeremy Daer <[email protected]>
  • Loading branch information
ahorek and jeremy committed Mar 5, 2018
1 parent 58f3bbf commit dead487
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Performance:

Bugs:
* Fix transfer encoding when message encoding is blank. (jakubonty, saks)
* Fix 7bit/base64 content transfer encoding mismatch. (ahorek)
* Fix UTF-8 attachment filename quoting. (ahorek)


Expand Down
8 changes: 7 additions & 1 deletion lib/mail/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ def encoded(transfer_encoding = nil)
([preamble] + encoded_parts).join(crlf_boundary) + end_boundary + epilogue.to_s
else
dec = Mail::Encodings.get_encoding(encoding)
enc = negotiate_best_encoding(transfer_encoding)
enc =
if Utilities.blank?(transfer_encoding)
dec
else
negotiate_best_encoding(transfer_encoding)
end

if dec.nil?
# Cannot decode, so skip normalization
raw_source
Expand Down
19 changes: 19 additions & 0 deletions spec/mail/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,25 @@ def message_headers_should_match(message, other)

end

it "rfc2046 can be decoded" do
mail = Mail.new
mail.body << Mail::Part.new.tap do |part|
part.content_disposition = 'attachment; filename="test.eml"'
part.content_type = 'message/rfc822'
part.body = "This is NOT plain text ASCII − かきくけこ" * 30
end

roundtripped = Mail.new(mail.encoded)
expect(roundtripped.content_transfer_encoding).to eq '7bit'
expect(roundtripped.parts.last.content_transfer_encoding).to eq ''

# Check that the part's transfer encoding isn't set to 7bit, causing
# the actual content to end up encoded with base64.
expect(roundtripped.encoded).to include('NOT plain')
expect(roundtripped.content_transfer_encoding).to eq '7bit'
expect(roundtripped.parts.last.content_transfer_encoding).to eq ''
end

# https://www.ietf.org/rfc/rfc2046.txt
# No encoding other than "7bit", "8bit", or "binary" is permitted for
# the body of a "message/rfc822" entity.
Expand Down

0 comments on commit dead487

Please sign in to comment.