Skip to content

Check the token algorithm before calling the key finder - #784

Merged
anakinj merged 3 commits into
jwt:mainfrom
anakinj:encoded-token-key-finder-algorithm
Nov 3, 2026
Merged

anakinj merged 3 commits into
jwt:mainfrom
anakinj:encoded-token-key-finder-algorithm

Conversation

@anakinj

@anakinj anakinj commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Description

JWT::EncodedToken called key_finder before looking at the allowed algorithms, so alg: none or HS256 tokens that could never verify against an RS256-only check still triggered a key lookup, which is often a remote JWKS request. JWT.decode already rejects these first.

#valid_signature? now returns false without calling key_finder when the token's alg is not allowed, so #verify_signature! raises JWT::VerificationError. That is the error these tokens already raised, so it differs from the JWT::IncorrectAlgorithm suggested 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_finder token whose alg header does not match the allowed algorithm used to verify and is now rejected. The key: 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 leave payload readable. Custom algorithms must match the token header through valid_alg?, as they already must in JWT.decode, so one with a symbol or nil alg no longer verifies through a key_finder. An unknown algorithm name still raises JWT::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:

  • There are tests for the fix or feature added/changed
  • A description of the changes and a reference to the PR has been added to CHANGELOG.md. More details in the CONTRIBUTING.md

Posted by Claude on behalf of @anakinj.

@anakinj
anakinj force-pushed the encoded-token-key-finder-algorithm branch from 805caa5 to 84e2efc Compare November 2, 2026 20:41
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
anakinj force-pushed the encoded-token-key-finder-algorithm branch from 84e2efc to ff5e3fa Compare November 2, 2026 20:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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.
@anakinj
anakinj merged commit 5d34f0e into jwt:main Nov 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JWT::EncodedToken calls key_finder before checking the token's algorithm

2 participants