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

Allow a list of algorithms for decode #241

Merged
merged 3 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions lib/jwt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ def decode(jwt, key = nil, verify = true, custom_options = {}, &keyfinder)

def decode_verify_signature(key, header, payload, signature, signing_input, options, &keyfinder)
algo, key = signature_algorithm_and_key(header, payload, key, &keyfinder)
allowed_algorithms = options[:algorithms] || []
allowed_algorithms += [options[:algorithm]] if options.key?(:algorithm)

raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') unless options[:algorithm]
raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') unless algo == options[:algorithm]
raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') if allowed_algorithms.empty?
raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') unless allowed_algorithms.include?(algo)

Signature.verify(algo, key, signing_input, signature)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jwt/default_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module DefaultOptions
verify_aud: false,
verify_sub: false,
leeway: 0,
algorithm: 'HS256'
algorithms: ['HS256']
}.freeze
end
end
16 changes: 14 additions & 2 deletions spec/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,30 @@

context 'Verify' do
context 'algorithm' do
it 'should raise JWT::IncorrectAlgorithm on missmatch' do
it 'should raise JWT::IncorrectAlgorithm on mismatch' do
token = JWT.encode payload, data[:secret], 'HS512'

expect do
JWT.decode token, data[:secret], true, algorithm: 'HS384'
JWT.decode token, data[:secret], true, algorithms: 'HS384'
Copy link
Member

Choose a reason for hiding this comment

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

I'd duplicate the tests in that case, since :algorithm is still supported. I prefer to be a little more paranoid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a typo, should be left as algorithm there.

Copy link
Member

Choose a reason for hiding this comment

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

Simply push a fix for that and I'll merge it.

end.to raise_error JWT::IncorrectAlgorithm

expect do
JWT.decode token, data[:secret], true, algorithm: 'HS512'
end.not_to raise_error
end

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

expect do
JWT.decode token, data[:secret], true, algorithms: ['HS384']
end.to raise_error JWT::IncorrectAlgorithm

expect do
JWT.decode token, data[:secret], true, algorithms: ['HS512', 'HS384']
end.not_to raise_error
end

context 'no algorithm provided' do
it 'should use the default decode algorithm' do
token = JWT.encode payload, data[:rsa_public].to_s
Expand Down