Repository navigation
fix(session): accept ES256 access tokens and ignore unknown JWKS keys - #590
Closed
gjtorikian wants to merge 2 commits into
Closed
gjtorikian wants to merge 2 commits into
gjtorikian wants to merge 2 commits into
Conversation
The session verifier accepted only RS256 access tokens. It read the JWKS through the generated JwksResponse model, which holds only the members of an RSA key. Thus one JWKS key of a different type made each session check fail. The verifier now accepts RS256 and ES256. The allow-list is a fixed constant. The SDK reads the JWKS as raw JSON and uses the `kid` header of the token to select the key. It ignores all other keys, also keys of a type that it does not know. The SDK does these checks before ruby-jwt verifies the signature, because not all permitted ruby-jwt versions do them: - RS256 uses only an RSA key. ES256 uses only an EC P-256 key. - If the key has an `alg` member, it must be the algorithm of the token. - If the key has a `use` member, it must be `sig`. - The comparison of the algorithm name is exact. - An ES256 signature must be 64 bytes long. - A token that has a `crit` header is not valid. A key or a signature that OpenSSL cannot use gives the result for a token that is not valid. A JWKS response that is not JSON, or that has no `keys` array, gives the same result. The SDK does not put such a response in the cache. An unknown `kid` causes a new JWKS request, but not more than one in 30 seconds. If that request fails, the SDK continues to use the cached keys. This commit is the 9.5.x version of the change on the main branch. The dependency ranges do not change on this line. The new tests make their keys at test time. They cover ES256 tokens, a JWKS with RSA and EC keys in the two sequences, keys of unknown types, algorithm confusion, ES256 signatures of an incorrect length, unknown key IDs, JWKS requests that fail, and JWKS responses that are not JSON. The algorithm confusion tests also use keys that have no `alg` member.
The Lint job reads the Ruby version from `.ruby-version`. The pinned release of ruby/setup-ruby cannot install that version on the current runner image. Thus the job stops before the lint step. This commit pins the release that the `main` branch uses.
Contributor
[Critical impact] The PR appears safe to merge; no actionable issues were found. SummaryAdds ES256 session verification while keeping RS256 support.
No actionable issues were found. Acknowledged choices from
|
Contributor
Author
|
This change goes only to |
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
This pull request is for the 9.5.x release line (base branch
v9.5.x). It is the 9.5.x version of the change with the same title onmain.The change lets the session verifier accept ES256 access tokens. It also makes the verifier ignore JWKS keys that it cannot use.
Problem
JwksResponsemodel. That model holds only the members of an RSA key. Thus one JWKS key of a different type made each session check fail, also for RS256 tokens.Change
All changes are in hand-written files.
WorkOS::SessionManager::JWK_ALGORITHMSis now["RS256", "ES256"]. The list is a fixed constant. The SDK does not take it from the token, from the JWKS or from the caller.kidheader of the token selects the key. The SDK ignores all other keys. This includes keys of an unknown type (for exampleOKPorAKP) and keys that are not correct.jwtgem verifies the signature:algmember, it must be the algorithm of the token.usemember, it must besig.r || s).critheader is not valid (RFC 7515, section 4.1.11). The SDK implements no header extension.Session#authenticate. An error of the JWKS request (for example a TLS error) escapes as before.keysarray, gives the same result for the token. The SDK does not put such a response in the cache, so the subsequent session check sends a new JWKS request.kidcauses a new JWKS request, but not more than one in 30 seconds. If that request fails, the SDK continues to use the cached keys. The cache time of 5 minutes does not change.Differences from the change on
mainjwtrange stays~> 3.1, andGemfile.lockdoes not change. The checks in the SDK do not rely on the version of thejwtgem.rbi/workos/session_manager.rbidoes not change, because it has the header of a generated file on this line. The type ofJWK_ALGORITHMSstaysT::Array[String]. The value in that file stays["RS256"].mainis not applicable to this line, so this patch does not include it.ruby/setup-rubyrelease in the lint workflow to the release thatmainuses. The older release cannot install the Ruby version of this line on the current runner image.Compatibility
JWK_ALGORITHMSis the only public value that changes.JSON::ParserErrorbefore. It now gives theinvalid_jwtresult.SessionManager#decode_jwtraisesJWT::DecodeErrorfor it.Tests
test/workos/test_session.rbhas 44 new tests. The tests make their keys at test time. They cover these conditions:algmember. These tests fail if the SDK does not bind the algorithm to the key type and to the curve.script/ciis successful with Ruby 3.3, 3.4 and 4.0. The session tests are also successful with jwt 3.1.0, 3.1.2, 3.2.0, 3.3.0, 3.3.1 and 3.3.2.Documentation
Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.
If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.