From fd2b502e0f1c102b5e89a30aefc1c1825bd52a2a Mon Sep 17 00:00:00 2001 From: Yuji Hanamura Date: Thu, 26 Dec 2024 17:04:45 +0900 Subject: [PATCH] fix: use `Base64.strict_encode64` instead of `Base64.encode64` Attachments must be base64 encoded, using `Base64.strict_encode64` where no line feeds are added. --- lib/sendgrid/helpers/mail/attachment.rb | 2 +- mail_helper_v3.md | 2 +- test/sendgrid/helpers/mail/test_attachment.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/sendgrid/helpers/mail/attachment.rb b/lib/sendgrid/helpers/mail/attachment.rb index 0386fc75..15c7c00d 100644 --- a/lib/sendgrid/helpers/mail/attachment.rb +++ b/lib/sendgrid/helpers/mail/attachment.rb @@ -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 diff --git a/mail_helper_v3.md b/mail_helper_v3.md index 6ede7d99..a6cc720f 100644 --- a/mail_helper_v3.md +++ b/mail_helper_v3.md @@ -307,7 +307,7 @@ plain_text_content = 'and easy to do anywhere, even with Ruby' html_content = 'and easy to do anywhere, even with Ruby' 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', diff --git a/test/sendgrid/helpers/mail/test_attachment.rb b/test/sendgrid/helpers/mail/test_attachment.rb index 0fecca00..28bf60eb 100644 --- a/test/sendgrid/helpers/mail/test_attachment.rb +++ b/test/sendgrid/helpers/mail/test_attachment.rb @@ -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)