Skip to content

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

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

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

Conversation

@gjtorikian

Copy link
Copy Markdown
Contributor

Description

WorkOS.Session.authenticate/1 accepted only RS256 access tokens. It read the JWKS through the generated WorkOS.JwksResponse model, which holds RSA members only. Thus an EC key did not verify a token. An OKP or AKP key in the JWKS caused a FunctionClauseError for all tokens.

This change gives the verifier a fixed allow-list of RS256 and ES256. The verifier ignores the keys that it cannot use.

Changes

  • The allow-list is one private table in lib/workos/session.ex. The token, the JWKS and the caller cannot change it.
  • The SDK binds each algorithm to one key type before it calls jose. RS256 verifies only with an RSA key. ES256 verifies only with an EC key on the P-256 curve.
  • If the selected 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 comparison of the algorithm name is exact.
  • The verifier reads the raw JWKS document and selects the key by kid. It skips keys with an unknown type or curve, keys without a kid and malformed keys. Such a key is not an error.
  • The verifier reads only the public members of a key: n and e, or crv, x and y.
  • An RSA key and an EC key can share a kid. If two keys of one type share a kid, the verifier uses the first key.
  • An ES256 signature must be exactly 64 bytes (R || S). The SDK makes this check itself.
  • The verifier rejects a token that has a crit header member (RFC 7515, section 4.1.11).
  • A malformed key, a malformed signature or a library error gives the invalid_jwt result. It does not raise.
  • The doc comment of authenticate/1 now names the two algorithms.

The JWKS cache and the 30-second refetch interval for an unknown kid do not change. The expiry check, the token type check and the :jwt_issuer check also do not change.

Dependencies

The requirement for jose does not change (~> 1.11). There is no new dependency.

jose 1.11.13 rejects an ECDSA signature that does not have the canonical length. The SDK does not require that release, because jose 1.11.11 and later do not compile on Erlang/OTP 25. Elixir 1.18 supports Erlang/OTP 25. Thus the SDK makes the length check itself, and the check does not depend on the jose release. The test suite passes on Elixir 1.18 with Erlang/OTP 25 and jose 1.11.10.

Compatibility

No public function, parameter, return type or failure reason changes. RS256 tokens that verified before still verify.

Tests

The new tests are in test/workos/session_test.exs. They make all keys at test time.

  • RS256 and ES256 tokens authenticate with a mixed JWKS in the two orders.
  • The JWKS also has OKP, AKP and unknown key types, or entries that are not keys.
  • The keys do not have the alg, use, x5c and x5t#S256 members.
  • An RS256 header has the kid of the EC key. An ES256 header has the kid of the RSA key.
  • An HS256 token uses the public key as the secret. A token has alg none.
  • An ES256 token has the kid of a P-384 key. A token has an unknown kid.
  • An ES256 signature has a changed value, a length of 63, 65 or 66 bytes, or the DER form.
  • A token has a crit header member. A key is malformed. The JWKS response is not a JWKS.

Each rejected token gives the invalid_jwt result and does not raise.

script/ci passes: compile with warnings as errors, format, Credo, Dialyzer and 573 tests.

`WorkOS.Session.authenticate/1` accepted only RS256 access tokens. It
read the JWKS through the generated `WorkOS.JwksResponse` model, which
holds RSA members only. Thus an EC key did not verify a token. An OKP or
AKP key in the JWKS caused a `FunctionClauseError` for all tokens.

The verifier now has a fixed allow-list of RS256 and ES256:

- It reads the raw JWKS document and selects the key by `kid`.
- RS256 verifies only with an RSA key. ES256 verifies only with an EC
  key on the P-256 curve.
- An `alg` member of the key must be equal to the token algorithm. A
  `use` member of the key must be `sig`.
- It skips each key that it cannot use. Such a key is not an error.
- An ES256 signature must be exactly 64 bytes.
- It rejects a token that has a `crit` header member.
- A malformed key or signature gives the `invalid_jwt` result.

The requirement for jose does not change. The SDK makes the signature
length check itself. Thus the check does not depend on the jose release.

Public functions, types and failure reasons do not change.

The new tests make their keys at test time. They include RS256 and ES256
tokens, a mixed JWKS in the two orders and unknown key types. They also
include algorithm confusion, a P-384 key, an unknown `kid`, and ES256
signatures with a changed value or an incorrect length.
@gjtorikian
gjtorikian requested a review from a team as a code owner December 1, 2026 02:14
@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge; no actionable issues were established.RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge; no actionable issues were established.

Summary

Adds ES256 support to WorkOS.Session.authenticate/1 alongside RS256.

  • Session authentication verifies RS256 and ES256 tokens with matching keys.

No actionable issues were established. Tests were not run during this review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Open sealed cookie] --> B[Read protected header]
  B --> C{RS256 or ES256, with kid and no crit?}
  C -->|No| X[Return invalid_jwt]
  C -->|Yes| D[Read cached keys or fetch raw JWKS]
  D --> E[Select kid and matching key type]
  E --> F[Check signature length and verify signature]
  F --> G[Check token type and configured issuer]
  G --> H[Check expiry and return session result]
  E -->|No matching key| X
  F -->|Invalid| X
  G -->|Invalid| X
Loading

Reviews (1) · Last reviewed commit: "fix(session): accept ES256 access tokens..." · 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