Repository navigation
fix(session): accept each JWT algorithm that the JWKS key can verify - #790
Open
gjtorikian wants to merge 1 commit into
Open
gjtorikian wants to merge 1 commit into
gjtorikian wants to merge 1 commit into
Conversation
A list from #788 permits only RS256 and ES256 access tokens. This change removes that list. The JWKS key that the kid of the token names decides the algorithm. PyJWT makes each decision from the alg, type and curve of that key. A test adds an algorithm to the PyJWT registry, and its token verifies. The SDK uses public keys only. Unsigned and MAC tokens never verify. A token that PyJWT cannot verify gives the failure reason invalid_jwt.
Contributor
Confidence Score: 5/5
[High impact] The PR appears safe to merge; no actionable issues were found. SummarySession verification now accepts the public-key algorithms supported by the selected JWKS key instead of a fixed
No actionable issues were found. Tests were inspected but not run.
|
This branch has not been deployed
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
On
main, the session verifier accepts an access token only if itsalgheader isRS256orES256. #788 added the list of these two names, and #788 is not in a release. This pull request removes the list. The SDK code now has no list of algorithm names. The JWKS key that thekidof the token names decides the algorithm. PyJWT makes each decision from thealgmember, the type and the curve of that key.How the key decides
The JWKS client of the SDK gives the JWKS entries to
PyJWKone at a time and skips an entry that PyJWT cannot read.PyJWKgives one algorithm to a key. That algorithm is thealgmember of the key, or the PyJWT default for the key type and curve.The SDK selects the key by
kid. A key with analgmember verifies that algorithm only. For a key withoutalg, PyJWT must read the key for the tokenalg. PyJWT rejects a key whose type or curve is not correct for that algorithm.jwt.decodegets thePyJWKand noalgorithmsargument, thus PyJWT accepts the algorithm of that key only.An unsigned token and a MAC token never verify. PyJWT reads no RSA, EC or OKP entry for such an algorithm, and the SDK rejects each key that is not a public key. The SDK also rejects these tokens before the JWKS request. For that check, it uses the PyJWT classes
NoneAlgorithmandHMACAlgorithm.Checks in SDK code
PyJWKSetraises an error for the full set if, for example, thealgmember of one entry is a list.alg. IfPyJWK(jwk)cannot read such an entry, the SDK gives it to the algorithms of the PyJWT registry (get_default_algorithms()), one at a time. The first algorithm that reads the entry becomes the algorithm of the key.PyJWKhas no default algorithm for an OKP key that is not Ed25519.algmember. The SDK gives the tokenalgtoPyJWKonly for a key that has noalgmember.PyJWK(jwk, algorithm)does not compare itsalgorithmargument with that member.kidand algorithm. The SDK selects the first key with thekidthat is a key for the tokenalg. It makes the same check on a key from a replacement forSession.jwks.PyJWKClientselects a key bykidonly.Algorithm.prepare_keybeforejwt.decodeand rejects the key for each error.jwt.decoderaisesInvalidKeyErrorfor a key on another curve, and that error is not anInvalidTokenError.use. The key must be an object of thecryptographyunionPublicKeyTypes, andusemust be absent orsig. PyJWT reads anoctentry as a MAC secret and an entry with private members as a private key.jwt.decodedoes not readuse.critheader. The SDK rejects a token that has acritheader, as onmain. PyJWT accepts a token with"crit": ["b64"].algmust be a name of the PyJWT registry whose class is notHMACAlgorithmorNoneAlgorithm. This check closes no gap of PyJWT. It keeps the count of JWKS requests at 0 for a token that it rejects.jwt.decodethat is not anInvalidTokenErrorto anInvalidTokenError. PyJWT does not changeUnicodeEncodeError(a lone surrogate in the token) orValueError(an RSA key too small for the hash).Algorithms
RS256,RS384,RS512,PS256,PS384,PS512. RSA keys of 3072 and 4096 bits:RS256. EC keys:ES256(P-256),ES384(P-384),ES512(P-521),ES256K(secp256k1). OKP keys:EdDSA(Ed25519 and Ed448).cryptography50.0.0 on Python 3.10, 3.11, 3.12, 3.13 and 3.14, and PyJWT 2.15.0 on Python 3.10. Another algorithm verifies only if PyJWT andcryptographysupport it for the key.No change
workos.sessionchanges. A comparison of the module onmainand with this change shows 10 public names with equal signatures and members.invalid_jwt. The results for a failed JWKS request do not change.authenticate()raisesPyJWKClientConnectionErrororOSError.kid(one time at most in each 30 seconds) does not change.pyproject.toml,uv.lockand the workflows do not change.Behavior changes
main, the result isinvalid_jwtfor each algorithm other thanRS256andES256. A key whosealgisRS256verifiesRS256only, as onmain. With a replacementPyJWKClientinSession.jwks, a key verifies only the algorithm that PyJWT gave to it. ThusPS256with an RSA key withoutalggivesinvalid_jwtthere, and the JWKS client of the SDK accepts it.main, only anRS256orES256token can cause a JWKS request. Now each token whosealgis a public key algorithm of PyJWT can cause one. Example: aPS256token for a key whosealgisRS256givesinvalid_jwtwith 1 request (main: 0).none, a MAC name and a name that PyJWT does not have cause no request, as onmain. For a JWKS whose only key is an Ed448 key withoutalg, 3 calls with anRS256token cause 1 request (main: 3).invalid_jwt. Onmain,authenticate()raisesUnicodeEncodeErrorandrefresh()gives the error text as the reason.jwt.decodethat is not anInvalidTokenErrornow givesinvalid_jwt. Example: withjwt_leeway="60"(astr),authenticate()raisesTypeErroronmain.Session._JWK_ALGORITHMS,AsyncSession._JWK_ALGORITHMS,workos.session._ACCESS_TOKEN_ALGORITHMSandworkos.session._key_agrees_with_algorithm. No SDK code reads_JWK_ALGORITHMS, thus a value that code sets there has no effect.Tests
authenticate()andrefresh()ofSessionandAsyncSession. One test adds an algorithm class to the PyJWT registry, and the SDK verifies its token with no change of SDK code.invalid_jwt. The cases: analgfor another key type, an ECDSAalgfor another curve, analgthat differs from thealgmember.none,HS256,HS384andHS512tokens giveinvalid_jwt. The cases use the public key bytes as the secret, and anoctentry with thekidof the token. A key with private members verifies no token.invalid_jwt. A changed bit and an RSA signature of another padding or hash give the same result.ktyorcrvand malformed entries in the JWKS, the other keys verify. A token for a skipped key givesinvalid_jwt. Thejwk,jku,x5uandx5cheaders of a token have no effect.tests/test_session.pyhas 1243 tests (main: 507). The test file ofmainruns against this change with 492 passes and 15 failures. Those 15 tests use theRS256andES256list, or assert a result of that list. The full suite passes on Python 3.10 to 3.14 (5231 tests).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.