Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: use Base64.strict_encode64 instead of Base64.encode64 #495

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/sendgrid/helpers/mail/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def encode(io)
# in binary mode, but at least we can faithfully convey the
# bytes.
str = str.encode('UTF-8') unless io.respond_to?(:binmode?) && io.binmode?
Base64.encode64 str
Base64.strict_encode64 str
end
end
end
2 changes: 1 addition & 1 deletion mail_helper_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ plain_text_content = 'and easy to do anywhere, even with Ruby'
html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
msg = SendGrid::Message.new(from, to, subject, plain_text_content, html_content)
bytes = File.read('/path/to/the/attachment.pdf')
encoded = Base64.encode64(bytes)
encoded = Base64.strict_encode64(bytes)
msg.add_attachment('balance_001.pdf',
encoded,
'application/pdf',
Expand Down
4 changes: 2 additions & 2 deletions test/sendgrid/helpers/mail/test_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def test_io_enocding
attachment.content = StringIO.new(SAMPLE_INPUT)

expected = {
"content" => "RXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLD\nvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCdu\nIGltIEZyw7xobGluZwpFcyBibMO8aHQgc28gZ3LDvG4gd2llIEJsw7x0ZW4g\nYmzDvGgnbiBpbSBGcsO8aGxpbmcKRXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBC\nbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8\nbiB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwo=\n"
"content" => "RXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwpFcyBibMO8aHQgc28gZ3LDvG4gd2llIEJsw7x0ZW4gYmzDvGgnbiBpbSBGcsO8aGxpbmcKRXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwo="
}

json = attachment.to_json

# Double check that the decoded json matches original input.
decoded = Base64.decode64(json["content"]).force_encoding('UTF-8').encode
decoded = Base64.strict_decode64(json["content"]).force_encoding('UTF-8').encode

assert_equal(decoded, SAMPLE_INPUT)

Expand Down
Loading