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

Verify JWT header format #622

Merged
merged 1 commit into from
Sep 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
**Fixes and enhancements:**

- Updated README to correctly document `OpenSSL::HMAC` documentation [#617](https://github.com/jwt/ruby-jwt/pull/617) ([@aedryan](https://github.com/aedryan))
- Verify JWT header format [#622](https://github.com/jwt/ruby-jwt/pull/622) ([@304](https://github.com/304))
- Your contribution here

## [v2.9.1](https://github.com/jwt/ruby-jwt/tree/v2.9.1) (2024-09-23)
Expand Down
1 change: 1 addition & 0 deletions lib/jwt/decode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def verify_signature

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
Expand Down
9 changes: 9 additions & 0 deletions spec/jwt/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
data = {
:empty_token => 'e30K.e30K.e30K',
:empty_token_2_segment => 'e30K.e30K.',
:invalid_header_token => 'W10.e30K.e30K',
:secret => 'My$ecretK3y',
:rsa_private => test_pkey('rsa-2048-private.pem'),
:rsa_public => test_pkey('rsa-2048-public.pem'),
Expand Down Expand Up @@ -520,6 +521,14 @@
end.to raise_error JWT::IncorrectAlgorithm
end

context 'invalid header format' do
it 'should raise JWT::DecodeError' do
expect do
JWT.decode data[:invalid_header_token]
end.to raise_error JWT::DecodeError
end
end

context '2-segment token' do
it 'should raise JWT::IncorrectAlgorithm' do
expect do
Expand Down
Loading