Skip to content

Commit 29633b1

Browse files
committed
Drop loose base64 decoding
1 parent f9fac27 commit 29633b1

File tree

3 files changed

+7
-45
lines changed

3 files changed

+7
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Drop support for the HS512256 algorithm [#650](https://github.com/jwt/ruby-jwt/pull/650) ([@anakinj](https://github.com/anakinj))
1010
- Remove deprecated claim verification methods [#654](https://github.com/jwt/ruby-jwt/pull/654) ([@anakinj](https://github.com/anakinj))
1111
- Remove dependency to rbnacl [#655](https://github.com/jwt/ruby-jwt/pull/655) ([@anakinj](https://github.com/anakinj))
12+
- Support only stricter base64 decoding (RFC 4648) [#658](https://github.com/jwt/ruby-jwt/pull/658) ([@anakinj](https://github.com/anakinj))
1213

1314
Take a look at the [upgrade guide](UPGRADING.md) for more details.
1415

lib/jwt/base64.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,13 @@ def url_encode(str)
1414
end
1515

1616
# Decode a string with URL-safe Base64 complying with RFC 4648.
17-
# 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")
1817
# @api private
1918
def url_decode(str)
2019
::Base64.urlsafe_decode64(str)
2120
rescue ArgumentError => e
2221
raise unless e.message == 'invalid base64'
23-
raise Base64DecodeError, 'Invalid base64 encoding' if JWT.configuration.strict_base64_decoding
2422

25-
loose_urlsafe_decode64(str).tap do
26-
Deprecations.warning('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', only_if_valid: true)
27-
end
28-
end
29-
30-
def loose_urlsafe_decode64(str)
31-
str += '=' * (4 - str.length.modulo(4))
32-
::Base64.decode64(str.tr('-_', '+/'))
23+
raise Base64DecodeError, 'Invalid base64 encoding'
3324
end
3425
end
3526
end

spec/jwt/jwt_spec.rb

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,14 @@
552552
end
553553

554554
context 'a token with invalid Base64 segments' do
555-
it 'raises JWT::DecodeError' do
556-
expect { JWT.decode('hello.there.world') }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
555+
it 'raises JWT::Base64DecodeError' do
556+
expect { JWT.decode('hello.there.world') }.to raise_error(JWT::Base64DecodeError, 'Invalid base64 encoding')
557557
end
558558
end
559559

560560
context 'a token with two segments but does not require verifying' do
561561
it 'raises something else than "Not enough or too many segments"' do
562-
expect { JWT.decode('ThisIsNotAValidJWTToken.second', nil, false) }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
562+
expect { JWT.decode('ThisIsNotAValidJWTToken.second', nil, false) }.to raise_error(JWT::Base64DecodeError, 'Invalid base64 encoding')
563563
end
564564
end
565565

@@ -736,8 +736,8 @@
736736

737737
context 'when token ends with a newline char' do
738738
let(:token) { "#{JWT.encode(payload, 'secret', 'HS256')}\n" }
739-
it 'ignores the newline and decodes the token' do
740-
expect(JWT.decode(token, 'secret', true, algorithm: 'HS256')).to include(payload)
739+
it 'raises an error' do
740+
expect { JWT.decode(token, 'secret', true, algorithm: 'HS256') }.to raise_error(JWT::Base64DecodeError, 'Invalid base64 encoding')
741741
end
742742
end
743743

@@ -949,34 +949,4 @@ def verify(*)
949949
end
950950
end
951951
end
952-
953-
context 'when invalid token is valid loose base64' do
954-
it 'does not output deprecations warnings' do
955-
expect do
956-
JWT.decode("#{JWT.encode('a', 'b')} 9", 'b')
957-
rescue JWT::VerificationError
958-
nil
959-
end.not_to output(/DEPRECATION/).to_stderr
960-
end
961-
end
962-
963-
context 'when valid token is invalid strict base64 and decoded with the correct key' do
964-
it 'does outputs deprecation warning' do
965-
expect { JWT.decode("#{JWT.encode('payload', 'key')} ", 'key') }.to output(/DEPRECATION/).to_stderr
966-
end
967-
end
968-
969-
context 'when valid token is invalid strict base64 and decoded with the incorrect key' do
970-
it 'does not output deprecation warning, even when decoded with the correct key' do
971-
token = JWT.encode('payload', 'key')
972-
expect do
973-
begin
974-
JWT.decode("#{token} ", 'incorrect')
975-
rescue JWT::VerificationError
976-
nil
977-
end
978-
JWT.decode(token, 'key')
979-
end.not_to output(/DEPRECATION/).to_stderr
980-
end
981-
end
982952
end

0 commit comments

Comments
 (0)