JWT::EncodedToken calls key_finder before checking the token's algorithm
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- Half a day
- Newbie friendliness
- 42/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- ruby, security
- Domain
- authentication, security
Research direction
The bug sits in lib/jwt/encoded_token.rb: in valid_signature?, key_finder.call(self) runs before the verifiers are built from the allowed algorithms. Reproduce with the snippet in the issue, which should show the finder called once before JWT::VerificationError. Done means a disallowed alg raises JWT::IncorrectAlgorithm without calling the finder, while JWK-supplied algorithm calls still pass the existing specs.
Written by the indexing model from the issue text.
Description
JWT.decode rejects a token with a disallowed alg before looking up a key. JWT::EncodedToken#verify_signature! calls key_finder first, so tokens that could never pass still trigger the lookup.
Signature verification still fails correctly, so this isn't a bypass. The concern is that key finders often fetch a remote JWKS, which lets an unauthenticated alg: none or HS256 token cause outbound requests when the application expects only RS256.
rsa = OpenSSL::PKey::RSA.generate(2048)
token = JWT.encode({ 'sub' => 'x' }, 'secret', 'HS256')
calls = 0
finder = lambda { |_token|
calls += 1
rsa.public_key
}
JWT::EncodedToken.new(token).verify_signature!(algorithm: 'RS256', key_finder: finder)
# => JWT::VerificationError: Signature verification failed (calls == 1)
JWT.decode(token, nil, true, algorithm: 'RS256') { |_header| rsa.public_key }
# => JWT::IncorrectAlgorithm: Expected a different algorithm (block not called)
Reproduced on 3.3.1, and an alg: none token behaves the same way. lib/jwt/encoded_token.rb is unchanged on main.
Cause: EncodedToken#valid_signature? evaluates key_finder.call(self) before creating the verifiers from the allowed algorithms.
Suggestion: When algorithm: is given, check the token's alg against it before calling key_finder, and raise JWT::IncorrectAlgorithm if it isn't allowed. Calls that rely on a JWK to supply the algorithm would need to keep working.
Posted by Claude on behalf of @anakinj.
- Dominant language
- Ruby
- Stars
- 3.7k
- Forks
- 381
- Avg merge
- 19h 13m
- Merged PRs (30d)
- 8
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from jwt/ruby-jwt
-
JWT::EncodedToken raises TypeError for a non-object headerPossibly taken @anakinj claimed this today. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
jwt/ruby-jwt#780 · 1 reaction ·
Maintainers usually reply within 1 day
-
JWT::EncodedToken accepts tokens with more than three segmentsPossibly taken @anakinj claimed this today. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 30/100
jwt/ruby-jwt#779 · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
jwt/ruby-jwt#743 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
jwt/ruby-jwt#723 · 2 comments ·
Maintainers usually reply within 1 day
-
feature
Difficulty 5/5 Over a week Newbie friendliness 35/100
jwt/ruby-jwt#428 · 7 comments · 1 reaction ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 Under an hour Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
zold-io/zold-ruby-sdk#85 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Maintainers usually reply within 1 day