Skip to content

Commit 39aa57a

Browse files
committed
Deprecate the loose base64 decoding.
1 parent 9090e78 commit 39aa57a

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- Remove explicit base64 require from x5c_key_finder [#580](https://github.com/jwt/ruby-jwt/pull/580) - [@anakinj](https://github.com/anakinj).
1818
- Performance improvements and cleanup of tests [#581](https://github.com/jwt/ruby-jwt/pull/581) - [@anakinj](https://github.com/anakinj).
1919
- Repair EC x/y coordinates when importing JWK [#585](https://github.com/jwt/ruby-jwt/pull/585) - [@julik](https://github.com/julik).
20+
- Explicit dependency to the base64 gem [#582](https://github.com/jwt/ruby-jwt/pull/582) - [@anakinj](https://github.com/anakinj).
21+
- Deprecation warning for decoding content not compliant with RFC 4648 [#582](https://github.com/jwt/ruby-jwt/pull/582) - [@anakinj](https://github.com/anakinj).
2022
- Your contribution here
2123

2224
## [v2.7.1](https://github.com/jwt/ruby-jwt/tree/v2.8.0) (2023-06-09)

lib/jwt/base64.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,26 @@
33
require 'base64'
44

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

14+
# Decode a string with URL-safe Base64 complying with RFC 4648.
15+
# 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")
1316
def url_decode(str)
17+
::Base64.urlsafe_decode64(str)
18+
rescue ArgumentError => e
19+
raise unless e.message == 'invalid base64'
20+
21+
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')
22+
loose_urlsafe_decode64(str)
23+
end
24+
25+
def loose_urlsafe_decode64(str)
1426
str += '=' * (4 - str.length.modulo(4))
1527
::Base64.decode64(str.tr('-_', '+/'))
1628
end

ruby-jwt.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Gem::Specification.new do |spec|
3131
spec.executables = []
3232
spec.require_paths = %w[lib]
3333

34+
spec.add_dependency 'base64'
35+
3436
spec.add_development_dependency 'appraisal'
3537
spec.add_development_dependency 'bundler'
3638
spec.add_development_dependency 'rake'

0 commit comments

Comments
 (0)