Skip to content

fix(session): accept ES256 access tokens and ignore unknown JWKS keys - #788

Merged
gjtorikian merged 3 commits into
mainfrom
fix/session-jwt-allowlist
Dec 3, 2026
Merged

gjtorikian merged 3 commits into
mainfrom
fix/session-jwt-allowlist

Conversation

@gjtorikian

@gjtorikian gjtorikian commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

The session verifier accepted only RS256 access tokens. With this change, the verifier accepts RS256 and ES256. RS256 tokens verify as before.

Changes

  • The allow-list is one private constant: RS256 and ES256. The token header, the JWKS and the caller cannot add an algorithm to it.
  • The SDK makes sure that the key agrees with the token algorithm before PyJWT verifies the signature. RS256 uses an RSA key only. ES256 uses an EC key on the P-256 curve only.
  • If the JWKS key has an alg member, it must be equal to the token algorithm. If the key has a use member, it must be sig.
  • The SDK does not accept a token that has a crit header (RFC 7515, section 4.1.11).
  • The SDK reads the JWKS one key at a time. A key that PyJWT cannot read, for example a key with "alg": "none", has no effect on the other keys.
  • The SDK selects the JWKS key by the kid of the token and the algorithm of the token. When an RSA key and an EC key have the same kid, each token gets the key for its algorithm.
  • authenticate() returns INVALID_JWT when the JWKS has no key that has the kid of the token and is a key for its algorithm. It returns the same result when PyJWT can read no key of the JWKS. Before this change, authenticate() raised an error for these conditions.
  • refresh() returns INVALID_JWT for the same conditions. Before this change, refresh() returned the text of the error for some of them.
  • The JWKS request sends the User-Agent of the SDK.

Limit

If PyJWT can read no key of the JWKS, the SDK returns INVALID_JWT for each token and does not keep the JWKS. It does not raise an error.

Dependency

The minimum PyJWT version changes from 2.12 to 2.15 (pyjwt~=2.15). The JWKS client of the SDK is a PyJWKClient. It uses the cooldown_duration of PyJWT 2.15 to limit the JWKS requests for an unknown kid to one in each 30 seconds. PyJWT 2.15 supports Python 3.9 and later, thus the supported Python versions do not change. The SDK reads the JWKS and makes the algorithm and key checks itself. It does not rely on the PyJWT version for them.

No change

  • Public names, parameters, return types and failure reasons.
  • The checks for expiry, token type, issuer and leeway.
  • The result for a JWKS request failure. authenticate() raises PyJWKClientConnectionError and refresh() returns INVALID_JWT. If the connection stops in the body of the response, authenticate() raises OSError. The docstring of authenticate() now shows these errors.

Code that replaces session.jwks

The SDK now gives the PyJWK object to jwt.decode. A replacement for session.jwks must return a jwt.PyJWK object from get_signing_key_from_jwt(). A mock object that has only a key member now gives INVALID_JWT. A replacement can select the key by its kid only, as PyJWKClient does. The SDK then makes sure that the key is a key for the algorithm of the token.

Tests

The new tests run for Session and AsyncSession, with authenticate() and refresh(). The tests make each JWK and each signature without PyJWT. They make all keys at test time.

  • RS256 and ES256 tokens verify with an RSA key, with an EC key, and with the two keys in each order.
  • OKP, AKP, P-384 and unknown key types in the JWKS have no effect.
  • JWKS members that are not keys, and keys with a key parameter that is incorrect or absent, have no effect.
  • Keys that PyJWT cannot read have no effect: a key with "alg": "none", a key whose alg is an array or an object, and an oct key without k.
  • An RSA key and an EC key with the same kid verify RS256 and ES256 tokens, in each order of the keys. The SDK also goes past a key with that kid that is not a key for the algorithm.
  • Keys without alg, use, x5c and x5t#S256 verify tokens.
  • These tokens give INVALID_JWT:
    • an RS256 header with the kid of the EC key, and an ES256 header with the kid of the RSA key
    • HS256 with the public key as the secret, none, and each other algorithm
    • an ES256 token for a P-384 key
    • an unknown kid
    • a crit header
    • a changed ES256 signature, and ES256 signatures of 63 and 65 bytes
  • A JWKS with no readable key gives INVALID_JWT. The tests use a JWKS with only keys that PyJWT cannot read, a body that is not JSON and a body with too many levels.
  • The JWKS client keeps the keys for the lifespan. For an unknown kid, it gets the JWKS again one time at most in each cooldown period. The tests move a replaced clock.
  • A replacement PyJWKClient verifies tokens. The SDK refuses the key that it gives when the key is not a key for the algorithm of the token.
  • The HTTP path of the JWKS client was checked by hand against a local JWKS server. The server gave keys that PyJWT cannot read, and an RSA key and an EC key with the same kid. The client read the keys, selected the key for each algorithm, made one request and sent the SDK User-Agent.
  • A JWKS request failure gives the same result as before this change.
  • The SDK refuses a token with a header defect or a key defect before it calls jwt.decode.
  • Three test fixtures now return a PyJWK object, as PyJWKClient does.

./scripts/ci is successful: format, lint, type check and 4493 tests.

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 verifier accepted only RS256 access tokens. It now accepts
RS256 and ES256. The allow-list is one private constant. The token
header, the JWKS and the caller cannot add an algorithm to it.

The SDK now makes these checks before PyJWT verifies the signature:
- The algorithm of the token is RS256 or ES256.
- The token has no "crit" header (RFC 7515, section 4.1.11).
- RS256 uses an RSA key. ES256 uses an EC key on the P-256 curve.
- If the key has an "alg" member, it is equal to the token algorithm.
- If the key has a "use" member, it is "sig".

authenticate() now returns INVALID_JWT when the JWKS has no usable key
for the "kid" of the token. It returns the same result when the key
does not agree with the token algorithm, and when PyJWT cannot read the
JWKS. Before this change, authenticate() raised an error for these
conditions. For some of them, refresh() returned the text of the error.
refresh() now returns INVALID_JWT.

PyJWT does not ignore three types of JWKS key: a key with
"alg": "none", a key whose "alg" is an array or an object, and an "oct"
key without "k". For a JWKS with such a key, the SDK returns INVALID_JWT
for each token.

The result for a JWKS request failure does not change. authenticate()
raises the error of the request. The docstring of authenticate() now
shows this error.

The JWKS request now sends the User-Agent of the SDK. The minimum PyJWT
version is now 2.15. That version ignores most JWKS keys that it cannot
load and limits the JWKS requests for an unknown "kid".

The tests run for Session and AsyncSession, with authenticate() and
refresh(). They make sure of these conditions:
- RS256 and ES256 tokens verify with RSA and EC keys in each order.
- OKP, AKP and unknown key types in the JWKS have no effect.
- Keys without the optional members verify tokens.
- Algorithm confusion, a P-384 key, an unknown "kid", a "crit" header
  and an incorrect ES256 signature give INVALID_JWT.
- A JWKS that PyJWT cannot read gives INVALID_JWT.
- A JWKS request failure gives the same result as before.
@gjtorikian
gjtorikian requested a review from a team as a code owner December 1, 2026 02:13
@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; no actionable new issue was found.

Summary

Adds ES256 access-token support while keeping RS256 support.

  • Session tokens can use RS256 or ES256 with a matching signing key.
  • Unreadable JWKS entries no longer block usable session keys.

No actionable new issue was found. Tests were not run during this review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Read token header] --> B{RS256 or ES256 and no crit?}
  B -- No --> C[Return INVALID_JWT]
  B -- Yes --> D[Read cached or fetched JWKS]
  D --> E[Skip unreadable keys]
  E --> F[Match kid and algorithm]
  F --> G{Key type, curve and use agree?}
  G -- No --> C
  G -- Yes --> H[Verify signature and claims]
Loading

Reviews (3) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

@nicknisi nicknisi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed by Riker for Nick, at this head. Checked: the accepted algorithms are exactly RS256 and ES256 (HS* and none stay rejected), the key is matched to the token's algorithm and key type, unknown or unusable JWKS entries don't break verification of valid tokens, RS256 behavior is unchanged, and the new tests cover those cases.

Non-blocking follow-up: read the JWKS per entry and select by kid and key type. Today a few odd entry shapes fail every token, and when an RSA and an EC key share a kid only the first-listed one works. It fails closed, so not a security problem.

…id and algorithm

PyJWKClient reads the JWKS as one document. A key that PyJWT cannot read,
for example a key with "alg": "none", makes the whole document unusable,
and no token verifies. PyJWKClient also selects a key by its kid only.
When an RSA key and an EC key have the same kid, only the first key in
the JWKS verifies tokens.

The SDK now uses a PyJWKClient subclass that reads each key on its own
and ignores the keys that PyJWT cannot read. It selects the key that has
the kid of the token and is a key for the algorithm of the token. The
client keeps the keys for the lifespan, and gets the JWKS again for an
unknown kid one time at most in each cooldown period, as before.
@gjtorikian
gjtorikian enabled auto-merge (squash) December 2, 2026 23:49
@gjtorikian
gjtorikian merged commit 9452810 into main Dec 3, 2026
13 checks passed
@gjtorikian
gjtorikian deleted the fix/session-jwt-allowlist branch December 3, 2026 00:07
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.

2 participants