Skip to content

fix(session): accept each JWT algorithm that the JWKS key can verify - #593

Open
gjtorikian wants to merge 1 commit into
mainfrom
fix/session-jwt-key-algorithms
Open

gjtorikian wants to merge 1 commit into
mainfrom
fix/session-jwt-key-algorithms

Conversation

@gjtorikian

Copy link
Copy Markdown
Contributor

Description

On main, the session token verifier accepts a token only if its algorithm is RS256 or ES256. Pull request #589 added ES256 to JWK_ALGORITHMS and added a private table of key types. No release has #589. This pull request removes the table, and the SDK does not read JWK_ALGORITHMS now.

The SDK now has no list of permitted algorithm names. The kid header selects the JWKS key. The type, the curve and the alg member of that key give the permitted algorithms. Thus a different algorithm needs no new list of names in an SDK release. The SDK accepts a token of that algorithm only if ruby-jwt and OpenSSL have the algorithm for the key.

How the SDK selects the key and the algorithm

decode_jwt gets the algorithm object for the alg header from JWT::JWA.find and gives JWT.decode that one object and a block that finds the key. The block gets the JWKS keys that have the kid of the token. If a key has an alg member, that member must be equal to the alg header.

JWT::JWK.import selects the key class from kty, and the SDK makes the key from the public members of that class (members, then verify_key). For an EC key, the token must use the one algorithm of the curve (jwa). For an RSA key, the token must use a JWT::JWA::Rsa or JWT::JWA::Ps algorithm. ruby-jwt then verifies the signature with OpenSSL and checks the claims.

An unsigned token and a token with a MAC are never valid. ruby-jwt gives an algorithm of the class JWT::JWA::None or JWT::JWA::Hmac for such a token, and the SDK then gives JWT.decode an empty algorithm list. JWT.decode then raises JWT::IncorrectAlgorithm before the block looks for a key. The SDK also uses no key that is a String, and the key of a symmetric JWK is a String.

Checks in SDK code

  • For a JWT::JWA::None or JWT::JWA::Hmac algorithm, the SDK gives ruby-jwt an empty algorithm list. Cause: ruby-jwt accepts an unsigned token if the list has its none algorithm, and it verifies a MAC with a String key. The check stops such a token before the SDK looks for a key, with the reason invalid_jwt_algorithm.
  • The alg header must be equal to the name of the algorithm, with the same letter case. Cause: ruby-jwt compares names without case, so it accepts a token with rs256 for RS256.
  • An alg member of the key must be equal to the alg header, and use must be absent or sig. Cause: ruby-jwt gets the OpenSSL key from the SDK, so it does not read these members.
  • The algorithm must agree with the type and the curve of the key (key_algorithm?). Cause: ruby-jwt gives no algorithm for an RSA key that has no alg member. Thus the SDK code has the JWT::JWA::Rsa and JWT::JWA::Ps classes. This check also selects the key when two keys have the same kid.
  • The length of an ECDSA signature must be two times the byte length of the curve. Cause: ruby-jwt accepts r || 0x00 || s, which has one more byte.
  • The SDK makes the key only from the public members that the key class gives. Cause: ruby-jwt makes no key if a private member is not correct, and an alg member replaces the algorithm of the curve.
  • The SDK uses no key that is a String. Cause: the verify_key of a symmetric key is the secret, which is a String.
  • If ruby-jwt, the key class or OpenSSL raises an error for a key, the SDK ignores that one key. Cause: ruby-jwt raises NoMethodError for a member that is not a String, and JWT::UnsupportedKeyType for a kty that it does not have.
  • For a token that is not a String, or an alg name that is not UTF-8, the SDK gives ruby-jwt an empty algorithm list. Cause: JWT::EncodedToken.new and JWT::JWA.find raise ArgumentError for them.
  • The SDK does not give ruby-jwt a key member with a name that is not UTF-8. Cause: JWT::JWK.import raises EncodingError for that name. The SDK then uses the key without that member.
  • verify_jwt raises JWT::DecodeError for an OpenSSL::OpenSSLError and for an ArgumentError. Cause: JWT.decode raises ArgumentError for a token that is a number or an Array, and it does not rescue each OpenSSL error.

Algorithms

  • In the tests of this pull request, the SDK verifies RS256 tokens for RSA keys of 2048, 3072 and 4096 bits. It also verifies RS384, RS512, PS256, PS384 and PS512 (RSA, 2048 bits), ES256 (P-256), ES384 (P-384), ES512 (P-521) and ES256K (secp256k1). The tests make each key with OpenSSL at test time.
  • With ruby-jwt 3.3.1, the full test suite passes on Ruby 3.3.13, 3.4.12 and 4.0.9 with OpenSSL 3.5.5. It also passes on Ruby 3.3.11 and 4.0.0 with OpenSSL 3.6.4, and on Ruby 3.4.11 with OpenSSL 4.1.0. With ruby-jwt 3.3.2, it passes on Ruby 3.4.11 with OpenSSL 4.1.0.
  • The SDK accepts a token with a different algorithm only if ruby-jwt and OpenSSL have that algorithm for the key. For a key type that is not RSA, the SDK accepts the one algorithm that the key class gives for the key. A token with an algorithm that ruby-jwt does not have gives the reason invalid_jwt_algorithm.

No change

  • The public names, parameters and return types of SessionManager and Session, and the failure reason constants. JWK_ALGORITHMS stays a frozen Array with the same value as on main. The SDK does not read it now, and its comments say so.
  • The claims checks, the crit rejection, the check of kid, and the dependencies (workos.gemspec, Gemfile.lock).
  • The JWKS cache (300 seconds) and the new JWKS request for an unknown kid (not more than one in 30 seconds).
  • The results for RS256, ES256, unsigned and MAC tokens, with the exceptions in the subsequent section.

Behavior changes

  • A token of RS384, RS512, PS256, PS384, PS512, ES384, ES512 or ES256K with a correct signature. The key has no alg member, or it has that alg member. On main: invalid_jwt_algorithm. Now: valid.
  • A token of these algorithms that is not valid. On main: invalid_jwt_algorithm. Now: invalid_jwt_signature for an incorrect signature, expired_jwt for an expired token, and invalid_jwt for other causes. Examples of other causes: a different alg member of the key, an unknown kid, a crit header. Also a key of a different type or curve.
  • A token of these algorithms with an unknown kid, when the last JWKS request is older than 30 seconds. On main: no new JWKS request. Now: one new JWKS request, as for RS256 on main.
  • Two keys have the same kid, and the first key is not correct. Examples: an EC point that is not on the curve, a member that is not Base64. On main: invalid_jwt. Now: the SDK ignores that key and uses the subsequent key.
  • A token that is not a String (a number, true, an Array, a Hash). Also an n or x member of a JWKS key that is not UTF-8. On main: authenticate and decode_jwt raise an ArgumentError. Now: the reason is invalid_jwt, and decode_jwt raises JWT::DecodeError.
  • decode_jwt when the key of the token is not correct. On main: JWT::JWKError or JWT::Base64DecodeError. Now: JWT::DecodeError. The two classes are subclasses of JWT::DecodeError.
  • A key class and an algorithm that the application adds to ruby-jwt. On main: invalid_jwt_algorithm. Now: valid for a correct signature, if the key class has the members, jwa and verify_key methods. The tests of this pull request show this with a test key class and a test algorithm. A key class with no members method, or with a String key, gives invalid_jwt.

Tests

  • The session tests increase from 89 to 122, and the full suite increases from 1684 to 1717 tests. All tests pass.
  • Accept: each algorithm of the Algorithms section, for a key with no alg member and for a key with that alg member. session_manager.rb has only the algorithm names RS256 and ES256, in JWK_ALGORITHMS. With that constant empty, only the two tests that compare its value fail.
  • Reject, key confusion: an algorithm that does not agree with the key type, and an ECDSA algorithm with a key of a different curve. Also an alg header that is not the alg member of the key.
  • Reject: none, and HS256, HS384 and HS512 with the public key as the secret. Also HS256, HS384 and HS512 with the secret of an oct key that has the kid of the token.
  • Reject: an ECDSA signature with an incorrect length or in the DER form, for each of the four curves. The SDK uses no key that the token gives in a jwk, jku, x5c or x5u header member.
  • Tolerance: a key that the SDK cannot use does not stop the other keys. Examples: an unknown kty or crv, an oct key, a member that is not correct.
  • One test of main has a new name and new assertions, because it tests the list. It now asserts that the SDK accepts ES384. For a key with alg RS256, it asserts invalid_jwt for RS384, RS512 and PS256. A second test of main gets assertions for x5c and x5u. The pull request removes no test.
  • A local run removed each SDK check from a copy, one check at a time (24 cases). A test failed in each case.

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[ ] Yes

If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.

The session token verifier has no list of permitted algorithms now.
After pull request #589, that list had only RS256 and ES256.
Thus a different algorithm needed a new SDK release.
The `kid` header selects the JWKS key. The algorithm must agree with it.
ruby-jwt has the algorithms for the key type and verifies the signature.
An unsigned token and a token with a MAC are never valid.
`JWK_ALGORITHMS` stays as a public constant. The SDK does not read it.
No public name, parameter, return type or failure reason constant changes.
@gjtorikian
gjtorikian requested a review from a team as a code owner December 4, 2026 07:43
@greptile-apps

greptile-apps Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

SessionManager now accepts algorithms supported by the selected JWKS key instead of enforcing JWK_ALGORITHMS.

  • Session tokens can use algorithms supported by their JWKS keys.
  • Session verification skips unusable keys and normalizes input errors.

No actionable issues were found. Tests were reviewed but not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Read token algorithm] --> B{Unsigned or HMAC?}
  B -->|Yes| R[Reject token]
  B -->|No| C[Select JWKS keys by kid]
  C --> D[Check key alg and use]
  D --> E[Import public members]
  E --> F[Check key type and curve]
  F --> G[Check ECDSA signature length]
  G --> H[Verify signature and claims]
Loading

Reviews (1) · Last reviewed commit: "fix(session): accept each JWT algorithm ..." · Reviewed by Greptile

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant