-
Notifications
You must be signed in to change notification settings - Fork 376
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
Verify JWT header format #622
Conversation
d96643b
to
abd8816
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking at this. Do you think it would make sense to handle this case separately?
lib/jwt/decode.rb
Outdated
@@ -135,7 +135,7 @@ def decode_signature | |||
end | |||
|
|||
def alg_in_header | |||
header['alg'] | |||
header.is_a?(Hash) && header['alg'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would maybe like to have the type concern separated from checking the alg value.
For example a separate guard in #verify_algo
def verify_algo
raise JWT::IncorrectAlgorithm, 'An algorithm must be specified' if allowed_algorithms.empty?
raise JWT::DecodeError, 'Token header not a JSON object' unless header.is_a?(Hash)
raise JWT::IncorrectAlgorithm, 'Token is missing alg header' unless alg_in_header
raise JWT::IncorrectAlgorithm, 'Expected a different algorithm' if allowed_and_valid_algorithms.empty?
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea @anakinj! 👍 Thank you, I've applied your suggestion.
This change is also a fix to #470 |
**Problem** JWT header is expected to be a hash. However, it's possible to generate a token that defines header as an Array `[]`. This case is not handled by the application and leads to `TypeError: no implicit conversion of String into Integer`. **Solution** Add a verification for an header type before accessing hash elements.
abd8816
to
5767e79
Compare
Great work. Thanks @304 for the effort. |
Description
JWT header is expected to be a hash. However, it's possible to generate a token that defines header as an Array
[]
. This case is not handled by the application and leads toTypeError: no implicit conversion of String into Integer
.Solution
Add a verification for an header type before accessing hash elements.
Checklist
Before the PR can be merged be sure the following are checked: