Skip to content

Commit

Permalink
Deprecate the loose base64 decoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Dec 30, 2023
1 parent 7c61cf4 commit 76702aa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
- Fix key base equality and spaceship operators [#569](https://github.com/jwt/ruby-jwt/pull/569) - [@magneland](https://github.com/magneland).
- Remove explicit base64 require from x5c_key_finder [#580](https://github.com/jwt/ruby-jwt/pull/580) - [@anakinj](https://github.com/anakinj).
- Performance improvements and cleanup of tests [#581](https://github.com/jwt/ruby-jwt/pull/581) - [@anakinj](https://github.com/anakinj).
- Explicit dependency to the base64 gem [#582](https://github.com/jwt/ruby-jwt/pull/582) - [@anakinj](https://github.com/anakinj).
- Deprecation warning for decoding content not compliant with RFC 4648 [#582](https://github.com/jwt/ruby-jwt/pull/582) - [@anakinj](https://github.com/anakinj).
- Your contribution here

## [v2.7.1](https://github.com/jwt/ruby-jwt/tree/v2.8.0) (2023-06-09)
Expand Down
16 changes: 14 additions & 2 deletions lib/jwt/base64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@
require 'base64'

module JWT
# Base64 helpers
# Base64 encoding and decoding
class Base64
class << self
# Encode a string with URL-safe Base64 complying with RFC 4648 (not padded).
def url_encode(str)
::Base64.encode64(str).tr('+/', '-_').gsub(/[\n=]/, '')
::Base64.urlsafe_encode64(str, padding: false)
end

# Decode a string with URL-safe Base64 complying with RFC 4648.
# Deprecated support for RFC 2045 remains for now. ("All line breaks or other characters not found in Table 1 must be ignored by decoding software")
def url_decode(str)
::Base64.urlsafe_decode64(str)
rescue ArgumentError => e
raise unless e.message == 'invalid base64'

warn('[DEPRECATION] Invalid base64 input detected, could be because of invalid padding, trailing whitespaces or newline chars. Graceful handling of invalid input will be dropped in the next major version of ruby-jwt')
loose_urlsafe_decode64(str)
end

def loose_urlsafe_decode64(str)
str += '=' * (4 - str.length.modulo(4))
::Base64.decode64(str.tr('-_', '+/'))
end
Expand Down
2 changes: 2 additions & 0 deletions ruby-jwt.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Gem::Specification.new do |spec|
spec.executables = []
spec.require_paths = %w[lib]

spec.add_dependency 'base64'

spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
Expand Down
2 changes: 1 addition & 1 deletion spec/jwt/x5c_key_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

let(:crl) { issue_crl([], issuer: root_certificate, issuer_key: root_key) }

let(:x5c_header) { [Base64.strict_encode64(leaf_certificate.to_der)] }
let(:x5c_header) { [Base64.encode64(leaf_certificate.to_der)] }
subject(:keyfinder) { described_class.new([root_certificate], [crl]).from(x5c_header) }

it 'returns the public key from a certificate that is signed by trusted roots and not revoked' do
Expand Down

0 comments on commit 76702aa

Please sign in to comment.