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

Raise IncorrectAlgorithm if token has no alg header #411

Merged
merged 1 commit into from Apr 26, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/jwt/decode.rb
Expand Up @@ -34,6 +34,7 @@ def decode_segments

def verify_signature
raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') if allowed_algorithms.empty?
raise(JWT::IncorrectAlgorithm, 'Token is missing alg header') unless header['alg']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JWT::Decode#verify_signature calls 'header['alg']' 2 times

Read more about it here.

raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') unless options_includes_algo_in_header?

@key = find_key(&@keyfinder) if @keyfinder
Expand Down
9 changes: 9 additions & 0 deletions spec/jwt_spec.rb
Expand Up @@ -5,6 +5,7 @@

let :data do
data = {
:empty_token => 'e30K.e30K.e30K',
:secret => 'My$ecretK3y',
:rsa_private => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-private.pem'))),
:rsa_public => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-public.pem'))),
Expand Down Expand Up @@ -411,6 +412,14 @@
expect(jwt_payload).to eq payload
end
end

context 'token is missing algorithm' do
it 'should raise JWT::IncorrectAlgorithm' do
expect do
JWT.decode data[:empty_token]
end.to raise_error JWT::IncorrectAlgorithm
end
end
end

context 'issuer claim' do
Expand Down