Skip to content

Commit

Permalink
Raise IncorrectAlgorithm if token has no alg header
Browse files Browse the repository at this point in the history
  • Loading branch information
bouk authored and anakinj committed Apr 26, 2021
1 parent 018fcc8 commit 3434f58
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
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']
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

0 comments on commit 3434f58

Please sign in to comment.