Repository navigation
fix: Accept a JWK as the verification key in JWT.decode - #818
Conversation
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.
|
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 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
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 ( JWA objects are now turned into names, but Symbols pass through unchanged, and
3. JWKs for different algorithms in one key list still fail ( The check requires every JWK to support one of the algorithms, and 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 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 Nit: with Checked without finding problems:
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.
|
Addressed in b3e0aac:
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.
|
Re-review notes, addressed in d280f2a:
The full suite passes (902 examples) and RuboCop is clean. Posted by Claude on behalf of @anakinj. |
Description
Passing a JWK as the key to
JWT.decodefails, even when the JWK's algorithm is allowed:JWT.decodegives the allowed algorithms toJWT::JWA.validate_jwk_algorithms!as resolved JWA objects. The check compares them with the JWK's algorithm usingvalid_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:
JWT.decode, or returned by a key finder;EncodedToken#verify_signature!andToken#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.decodenarrows the algorithms to the token'salgheader. 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: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:
algheader isn't the JWK's algorithm is still rejected;These calls already failed, but now fail differently:
JWT::VerificationError: Signature verification failed, the parent class ofVerificationKeyError;octor RSA JWK withoutalganywhere in the key list always raisesJWKError, 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 whosealghas the same name, and is only used if it is registered under that name. The README now says so.A JWK without
algstill raisesCould 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.decodewith an RSA JWK;alg;algheader isn't the JWK's algorithm;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
algheader spec, which fails ifJWT.decodepasses 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:
fix: Reject tokens with too many segmentsPosted by Claude on behalf of @anakinj.