Repository navigation
Check the token algorithm before calling the key finder - #784
Merged
Merged
Conversation
anakinj
force-pushed
the
encoded-token-key-finder-algorithm
branch
from
November 2, 2026 20:41
805caa5 to
84e2efc
Compare
EncodedToken called key_finder before looking at the allowed algorithms, so tokens that could never verify still triggered a key lookup, which is often a remote JWKS request. JWT.decode already rejects them first.
anakinj
force-pushed
the
encoded-token-key-finder-algorithm
branch
from
November 2, 2026 20:49
84e2efc to
ff5e3fa
Compare
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the change to reject disallowed token algorithms before key lookup.
No open issues 🎉
What changed in this PR
This PR updates JWT::EncodedToken to reject disallowed token algorithms before invoking a key finder, avoiding unnecessary key lookups and remote JWKS requests.
Changes:
- Checks allowed algorithms before key lookup while preserving JWK-derived algorithm selection and direct-key behavior.
- Clears previous signature-verification state before rechecking and adds regression coverage.
- Documents the behavior change and sets RuboCop’s class-length limit to 120.
| File | Description |
|---|---|
spec/jwt/encoded_token_spec.rb |
Tests early rejection, algorithm compatibility, verification-state reset, and JWK fallback. |
lib/jwt/encoded_token.rb |
Adds the algorithm precheck and resets signature-verification state. |
CHANGELOG.md |
Documents the key-finder behavior change. |
.rubocop.yml |
Sets the maximum class length to 120. |
Review effort: Balanced
Resolve the conflict with the segment count check from jwt#782 by keeping both checks: reset the verification state, reject a malformed token, then skip the key finder for a disallowed algorithm.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
JWT::EncodedTokencalledkey_finderbefore looking at the allowed algorithms, soalg: noneor HS256 tokens that could never verify against an RS256-only check still triggered a key lookup, which is often a remote JWKS request.JWT.decodealready rejects these first.#valid_signature?now returnsfalsewithout callingkey_finderwhen the token'salgis not allowed, so#verify_signature!raisesJWT::VerificationError. That is the error these tokens already raised, so it differs from theJWT::IncorrectAlgorithmsuggested in the issue, to keep existing rescues working and the predicate from raising.An empty algorithm list skips the check, so a key finder can still supply a JWK that determines the algorithm. A
key_findertoken whosealgheader does not match the allowed algorithm used to verify and is now rejected. Thekey:path is unchanged, because an existing spec expects it to tolerate that mismatch.valid_signature?also clears any earlier verification state up front, so a rejected recheck can no longer leavepayloadreadable. Custom algorithms must match the token header throughvalid_alg?, as they already must inJWT.decode, so one with a symbol or nilalgno longer verifies through akey_finder. An unknown algorithm name still raisesJWT::VerificationKeyError, so a configuration typo is not reported as a bad signature.Fixes #781
Checklist
Before the PR can be merged be sure the following are checked:
Posted by Claude on behalf of @anakinj.