Skip to content

Commit f140f28

Browse files
authored
Remove Ruby <2.3 base64 decoding workaround (#9)
In older Ruby versions, WebPush.decode64 was introduced to wrap Base64.urlsafe_decode64 to prevent errors decoding "unpadded" urlsafe-encoded input. Since Ruby 2.3, Base64.urlsafe_decode64 can now gracefully decode padded and unpadded input.
1 parent fcb8c78 commit f140f28

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/web_push.rb

-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ def encode64(bytes)
5656
end
5757

5858
def decode64(str)
59-
# For Ruby < 2.3, Base64.urlsafe_decode64 strict decodes and will raise errors if encoded value is not properly padded
60-
# Implementation: http://ruby-doc.org/stdlib-2.3.0/libdoc/base64/rdoc/Base64.html#method-i-urlsafe_decode64
61-
str = str.ljust((str.length + 3) & ~3, '=') if !str.end_with?('=') && str.length % 4 != 0
62-
6359
Base64.urlsafe_decode64(str)
6460
end
6561

spec/web_push_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
expect(WebPush::VERSION).not_to be nil
66
end
77

8+
describe '.decode64' do
9+
let(:a_padded_key) { "YWJjZGU=" }
10+
11+
it 'urlsafe decodes padded base64 string' do
12+
expect(WebPush.decode64(a_padded_key)).to eq("abcde")
13+
end
14+
15+
it 'urlsafe decodes unpadded base64 string' do
16+
expect(WebPush.decode64(a_padded_key.delete("="))).to eq("abcde")
17+
end
18+
end
19+
820
shared_examples 'web push protocol standard error handling' do
921
it 'raises InvalidSubscription if the API returns a 404 Error' do
1022
stub_request(:post, expected_endpoint)

0 commit comments

Comments
 (0)