Skip to content

Commit

Permalink
base64 encode any signed parts
Browse files Browse the repository at this point in the history
- also keep disposition header for signed attachments
  • Loading branch information
jkraemer committed Feb 12, 2020
1 parent 2b5a4c8 commit d898102
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/mail/gpg/signed_part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ def self.build(cleartext_mail)
end
else
content_type cleartext_mail.content_type
body Mail::Utilities.to_crlf(cleartext_mail.body.raw_source)
if disposition = cleartext_mail.content_disposition
content_disposition disposition
end

# brute force approach to avoid messed up line endings that break
# signatures with Mail 2.7
body Mail::Encodings::Base64.encode cleartext_mail.body.to_s
body.encoding = 'base64'
end
end
end
Expand Down
32 changes: 32 additions & 0 deletions test/message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,38 @@ class MessageTest < MailGpgTestCase
@mail.gpg sign: true, password: 'abc'
end

context 'with attachment' do
setup do
p = Mail::Part.new do
body "and\nanother part euro €"
end
@mail.add_part p
# if we do not force it to binary, the line ending is changed to CRLF. WTF?
@attachment_data = "this is\n € not an image".force_encoding(Encoding::BINARY)
@mail.attachments['test.jpg'] = { mime_type: 'image/jpeg',
content: @attachment_data }

@mail.deliver
@signed = Mail.new @mails.first.to_s
@verified = @signed.verify
end

should 'verify signature' do
assert @verified.signature_valid?
end

should 'have original three parts' do
assert_equal 3, @verified.parts.size
assert_equal 'i am unencrypted', @verified.parts[0].body.to_s
assert_equal "and\r\nanother part euro €", @verified.parts[1].body.to_s.force_encoding('UTF-8')
assert attachment = @verified.parts[2]
assert attachment.attachment?
assert_equal "attachment; filename=test.jpg", attachment.content_disposition
assert_equal @attachment_data, attachment.body.to_s
end

end

context 'with multiple parts' do
setup do
p = Mail::Part.new do
Expand Down

0 comments on commit d898102

Please sign in to comment.