Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(JWT::decode): raise JWT::DecodeError on invalid signature #484

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/jwt/decode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def none_algorithm?

def decode_crypto
@signature = Base64.urlsafe_decode64(@segments[2] || '')
rescue ArgumentError
raise(JWT::DecodeError, 'Invalid segment encoding')
end

def algorithm
Expand Down
6 changes: 6 additions & 0 deletions spec/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,12 @@
end
end

context 'a token with invalid Base64 segments' do
it 'raises JWT::DecodeError' do
expect { JWT.decode('hello.there.world') }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
end
end

context 'a token with two segments but does not require verifying' do
it 'raises something else than "Not enough or too many segments"' do
expect { JWT.decode('ThisIsNotAValidJWTToken.second', nil, false) }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
Expand Down