Skip to content

fix: Accept a JWK as the verification key in JWT.decode - #818

Merged
anakinj merged 3 commits into
jwt:mainfrom
anakinj:fix/jwk-as-decode-key
Nov 29, 2026
Merged

anakinj merged 3 commits into
jwt:mainfrom
anakinj:fix/jwk-as-decode-key

Conversation

@anakinj

@anakinj anakinj commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Description

Passing a JWK as the key to JWT.decode fails, even when the JWK's algorithm is allowed:

jwk = JWT::JWK.new(rsa_private, alg: 'RS256')
token = JWT.encode(payload, jwk.signing_key, 'RS256')

JWT.decode(token, jwk, true, algorithm: 'RS256')
# JWT::VerificationKeyError: Provided JWKs do not support one of the specified algorithms: #<JWT::JWA::Rsa:0x...>

JWT.decode gives the allowed algorithms to JWT::JWA.validate_jwk_algorithms! as resolved JWA objects. The check compares them with the JWK's algorithm using valid_alg?, which expects a string, so nothing ever matched. The error message also printed the objects instead of their names.

The check now compares algorithm names as strings, taken from JWA objects, Strings or Symbols. This fixes:

  • a JWK given as the key to JWT.decode, or returned by a key finder;
  • EncodedToken#verify_signature! and Token#sign! when the algorithm is given as a JWA object or a Symbol.

JWKs for other algorithms

The check required every JWK to support one of the algorithms, and JWT.decode narrows the algorithms to the token's alg header. So a key list or key finder with JWKs for two algorithms failed for every token. This has been the case since the check was added in #695:

JWT.decode(rs256_token, [rs256_jwk, es256_jwk], true, algorithms: %w[RS256 ES256])
# raised JWT::VerificationKeyError; now decodes

When verifying, JWKs that support none of the algorithms are now skipped, like keys of the wrong type since #810. The error is raised only when no key is left. Behaviour that stays the same:

  • a single JWK for another algorithm is still rejected, and the error now names the algorithms;
  • a token whose alg header isn't the JWK's algorithm is still rejected;
  • signing with a JWK still requires the JWK to support the algorithm.

These calls already failed, but now fail differently:

  • a mixed key list where no key verifies raises JWT::VerificationError: Signature verification failed, the parent class of VerificationKeyError;
  • an oct or RSA JWK without alg anywhere in the key list always raises JWKError, where before the result depended on the order of the keys.

A JWK always signs and verifies with the algorithm registered under its alg. A custom algorithm object given with a JWK therefore only selects the JWKs whose alg has the same name, and is only used if it is registered under that name. The README now says so.

A JWK without alg still raises Could not resolve the JWA, the "alg" parameter is missing, except EC, which takes the algorithm from its curve. This PR doesn't change that.

Specs

  • jwt_spec.rb:
    • JWT.decode with an RSA JWK;
    • with several allowed algorithms;
    • from a key finder;
    • with an EC JWK without alg;
    • with a JWK for another algorithm;
    • with a token whose alg header isn't the JWK's algorithm;
    • with key lists and key finders that mix JWKs for two algorithms, or a JWK and a plain key;
    • with a token signed with a JWK whose algorithm isn't allowed, given in a mixed key list.
  • encoded_token_spec.rb: verify_signature! with algorithms given as JWA objects and Symbols, an unsupported algorithm in the list, and a JWK for another algorithm given first.
  • token_spec.rb: sign! with the algorithm given as a JWA object and as a Symbol.

All the new specs fail on main. Two of them guard behaviour that stays the same: the alg header spec, which fails if JWT.decode passes all allowed algorithms instead of the ones matching the header, and the skipped JWK spec, which fails if the skipped JWKs are used anyway. The full suite passes and RuboCop is clean.

Checklist

Before the PR can be merged be sure the following are checked:

  • The pull request title follows Conventional Commits, for example fix: Reject tokens with too many segments
  • There are tests for the fix or feature added/changed

Posted by Claude on behalf of @anakinj.

JWT.decode passes the allowed algorithms to the JWK check as resolved
JWA objects, but the check compared them with the JWK's algorithm as
strings. A JWK given as the key, or returned by a key finder, was
rejected even when its algorithm was allowed, and the error printed
the JWA objects instead of their names.

The check now uses the names of JWA objects. The same applies to
EncodedToken#verify_signature! and Token#sign! when the algorithm is
given as a JWA object.
@anakinj

anakinj commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Notes from an independent review before merging.

The fix holds up. The review found no way to verify a token with a JWK whose algorithm wasn't allowed. Through JWT.decode, it also found no way to verify under an algorithm other than the token's alg header. None of about 60 scenarios that work on main fail with this PR.

Two small changes before merging, and one gap that could go here or in a follow-up.

1. Pin the header binding with a spec

JWT.decode is only safe because lib/jwt/decode.rb:56 passes allowed_and_valid_algorithms, which is the allowed list narrowed to the header's alg. If that line passes allowed_algorithms instead, the whole suite still passes, and a token with an RS256 header and an RS512 signature verifies with an RS512 JWK. A spec in the new context would catch that:

rs512_jwk = JWT::JWK.new(test_pkey('rsa-2048-private.pem'), alg: 'RS512')
forged = JWT::Token.new(payload: payload, header: { 'alg' => 'RS256' })
                   .tap { |t| t.sign!(algorithm: 'RS512', key: rs512_jwk.signing_key) }.jwt

expect { JWT.decode(forged, rs512_jwk, true, { algorithms: %w[RS256 RS512] }) }.to raise_error(JWT::VerificationKeyError)

2. Symbols still fail (lib/jwt/jwa.rb:61)

JWA objects are now turned into names, but Symbols pass through unchanged, and 'RS256'.casecmp(:RS256) is nil. Symbols are a supported input: JWA.resolve takes them, and encoded_token_spec.rb:338 accepts algorithm names given as symbols.

verify_signature!(algorithm: :RS256, key: jwk) -> JWT::VerificationKeyError: Provided JWKs do not support one of the specified algorithms: RS256
Token#sign!(algorithm: :RS256, key: jwk)        -> JWT::EncodeError: Provided JWKs do not support one of the specified algorithms: RS256

JWT.decode isn't affected, because it resolves the names first. alg.is_a?(SigningAlgorithm) ? alg.alg : alg.to_s, plus a spec, would fix it.

3. JWKs for different algorithms in one key list still fail (lib/jwt/jwa.rb:50, :65)

The check requires every JWK to support one of the algorithms, and JWT.decode narrows the algorithms to the header's alg. So a key list with JWKs for two algorithms fails for every token:

JWT.decode(rs256_token, [rs256_jwk, es256_jwk], true, algorithms: %w[RS256 ES256])
# JWT::VerificationKeyError: Provided JWKs do not support one of the specified algorithms: RS256

The same happens with a key finder that returns both. It has been like this since the check was added in #695. It works against #810, which made mixed key lists work for plain keys.

The fix: in create_verifiers, drop the JWKs that support none of the algorithms, and raise only when no key is left. That fixes finding 2 too, and still rejects a single mismatched JWK and the forged token from finding 1.

The description says a JWK returned by a key finder now works. That holds for one JWK, or for several JWKs that all match the header.

4. Custom algorithm objects with a JWK

Take algorithms: [CustomRS256.new], an unregistered SigningAlgorithm whose alg is RS256, together with an RS256 JWK. Decode now passes, but the custom verify is never called, because a JWK verifies with its own algorithm from the registry. Before this PR, that combination raised. Matching by name is consistent with how the header is matched, so this probably only needs documenting.

Nit: with JWA::Unsupported, or a custom object without an alg, the message shows an empty name, like ...algorithms: , RS512. Only EncodedToken and Token can get there.

Checked without finding problems:

  • Algorithms: strings, lowercase names, registered and custom JWA objects, Unsupported, mixed arrays, [] and nil.
  • JWKs: alone, in an array, from a key finder, in a one-key JWK::Set, and next to a plain key. Also EC JWKs without alg, oct JWKs and OKP JWKs from jwt-eddsa 2.0.0.
  • Other: the jwks: option, and none both in a JWK and in the header.
  • Specs and CI: the 8 new specs fail without the fix, the suite passes with 893 examples, RuboCop is clean and CI is green.

Posted by Claude on behalf of @anakinj.

The JWK check left Symbols as they were, and 'RS256'.casecmp(:RS256)
is nil, so a JWK given with algorithm: :RS256 was rejected. Algorithm
names are now strings.

The check also required every JWK to support one of the algorithms,
and JWT.decode narrows the algorithms to the alg header, so a key list
or key finder with JWKs for two algorithms failed for every token.
JWKs for other algorithms are now skipped, and the error is raised
only when no key is left. A token whose alg header is not the
algorithm of the JWK is still rejected, and a spec now covers that.

The error message no longer shows an empty name for an algorithm
without one, and the README says that a JWK signs and verifies with
its own algorithm, not with a custom algorithm of the same name.
@anakinj

anakinj commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Addressed in b3e0aac:

  1. Header binding: added a spec that decodes a token with an RS256 header and an RS512 signature using an RS512 JWK, with algorithms: %w[RS256 RS512], and expects VerificationKeyError. It fails when decode.rb:56 passes allowed_algorithms instead of allowed_and_valid_algorithms.

  2. Symbols: algorithm names are now strings, so verify_signature! and sign! accept :RS256 with a JWK. There are specs for both.

  3. JWKs for other algorithms: create_verifiers now skips the JWKs that support none of the algorithms, and raises only when no JWK and no plain key is left. Specs cover:

    • a key list with RS256 and ES256 JWKs, for tokens of both algorithms;
    • a key finder returning both;
    • an ES256 JWK next to a plain RSA key;
    • an ES256 JWK given first to verify_signature!.

    A single mismatched JWK and the forged token from point 1 are still rejected.

  4. Custom algorithms: the README now says that a JWK signs and verifies with its own algorithm, and that a custom algorithm only selects JWKs with the same alg name. To use the custom implementation, pass jwk.signing_key or jwk.verify_key instead.

Nit: the error message leaves out algorithms without a name, and there's a spec for that.

The full suite passes (901 examples) and RuboCop is clean. I've updated the description to match.

Posted by Claude on behalf of @anakinj.

…are not used

A spec now checks that a JWK for an algorithm that isn't allowed is
skipped rather than used to verify. Without it, using every JWK again
left the suite green.

The error message names an algorithm without an alg, such as
JWT::JWA::Unsupported, instead of leaving it out.

The README says that a custom algorithm registered under a JWK's alg
is used for that JWK, and the note now follows the example.
@anakinj

anakinj commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Re-review notes, addressed in d280f2a:

  1. Skipped JWKs: added a spec that decodes a token signed with an ES256 JWK under an RS256 header, using [es256_jwk, rs256_jwk] with only RS256 allowed. It expects VerificationError: Signature verification failed. If create_verifiers used every JWK instead of the usable ones, the suite used to pass. Now this spec fails.
  2. README: the note now says that a JWK uses the algorithm registered under its alg, or the one for its curve for EC and OKP keys without alg. A custom algorithm given with a JWK is used only if it is registered under that name. I checked this with a registered XS256 algorithm and an oct JWK, and its sign and verify were each called once. The note now follows the example.
  3. Error message: an algorithm without a name is shown as itself, for example JWT::JWA::Unsupported, RS512, so the list is never empty.
  4. Description: it now lists the error class changes for calls that already failed, and the sentence about which specs pass without the fix is corrected.

The full suite passes (902 examples) and RuboCop is clean.

Posted by Claude on behalf of @anakinj.

@anakinj
anakinj merged commit 2651e5f into jwt:main Nov 29, 2026
17 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.

1 participant