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

verifies algorithm before evaluating keyfinder #346

Merged
merged 2 commits into from Jul 7, 2020
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
6 changes: 3 additions & 3 deletions lib/jwt/decode.rb
Expand Up @@ -33,12 +33,12 @@ def decode_segments
private

def verify_signature
@key = find_key(&@keyfinder) if @keyfinder
@key = ::JWT::JWK::KeyFinder.new(jwks: @options[:jwks]).key_for(header['kid']) if @options[:jwks]

raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') if allowed_algorithms.empty?
raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') unless options_includes_algo_in_header?

@key = find_key(&@keyfinder) if @keyfinder
@key = ::JWT::JWK::KeyFinder.new(jwks: @options[:jwks]).key_for(header['kid']) if @options[:jwks]

Signature.verify(header['alg'], @key, signing_input, @signature)
end

Expand Down
10 changes: 10 additions & 0 deletions spec/jwt_spec.rb
Expand Up @@ -299,6 +299,16 @@
end.not_to raise_error
end

it 'should raise JWT::IncorrectAlgorithm on mismatch prior to kid public key network call' do
token = JWT.encode payload, data[:rsa_private], 'RS256'

expect do
JWT.decode(token, nil, true, { algorithms: ['RS384'] }) do |_,_|
# unsuccessful keyfinder public key network call here
end
end.to raise_error JWT::IncorrectAlgorithm
end

it 'should raise JWT::IncorrectAlgorithm when algorithms array does not contain algorithm' do
token = JWT.encode payload, data[:secret], 'HS512'

Expand Down