Repository navigation
feat(backend): Support ES256 and multiple signing keys - #11036
Conversation
Verify ES256-signed tokens against EC keys from the JWKS and P-256 PEM jwtKeys, replace the JWKS cache on refetch so removed keys stop being trusted, hand off to a handshake when a session cookie's kid is no longer in the JWKS, and load the key from the JWKS when a pinned jwtKey cannot verify a token's algorithm and secretKey is set.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 524d75e The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/electron
@clerk/electron-passkeys
@clerk/eslint-plugin
@clerk/expo
@clerk/expo-biometrics
@clerk/expo-google-signin
@clerk/expo-native-components
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/hono
@clerk/localizations
@clerk/mosaic
@clerk/nextjs
@clerk/nuxt
@clerk/react
@clerk/react-router
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/ui
@clerk/upgrade
@clerk/vue
commit: |
Keep RS256 on pinned RSA keys, parse P-256 jwtKeys with literal newlines, use null-prototype key caches, keep JwksKeyJSON n/e required, and document when jwtKey can make a network request.
Resolve verification keys through one loadVerificationKey helper for session, handshake and machine tokens, look algorithms up once, reuse pemToBuffer for P-256 keys, and parse the PEM body once.
Concurrent JWKS refetches for one instance share a single request, so a slower older response cannot overwrite a newer key set, and the jwtKey fallback only warns when the fetched key can verify the token.
| case TokenVerificationErrorReason.TokenIatInTheFuture: | ||
| return AuthErrorReason.SessionTokenIatInTheFuture; | ||
| case TokenVerificationErrorReason.JWKKidMismatch: | ||
| return AuthErrorReason.SessionTokenKidMismatch; |
There was a problem hiding this comment.
I think we'll need to add Go backend support for this, but I think it's just a metrics thing, it's non-breaking.
|
!snapshot signing-keys |
| @@ -272,6 +272,9 @@ export interface JwksKeyJSON { | |||
| alg: string; | |||
| n: string; | |||
| e: string; | |||
There was a problem hiding this comment.
n and e should also be optional now? Or we cannot change that type without a breaking change?
|
Hey @nikosdouvlis - the snapshot version command generated the following package versions:
Tip: Use the snippet copy button below to quickly install the required packages. npm i @clerk/astro@4.1.34-signing-keys.v20261130000825 --save-exact
npm i @clerk/backend@3.34.0-signing-keys.v20261130000825 --save-exact
npm i @clerk/chrome-extension@3.1.118-signing-keys.v20261130000825 --save-exact
npm i @clerk/clerk-js@6.48.2-signing-keys.v20261130000825 --save-exact
npm i @clerk/electron@0.1.23-signing-keys.v20261130000825 --save-exact
npm i @clerk/electron-passkeys@0.0.4-signing-keys.v20261130000825 --save-exact
npm i @clerk/eslint-plugin@0.2.1-signing-keys.v20261130000825 --save-exact
npm i @clerk/expo@4.17.9-signing-keys.v20261130000825 --save-exact
npm i @clerk/expo-biometrics@1.1.1-signing-keys.v20261130000825 --save-exact
npm i @clerk/expo-google-signin@1.1.1-signing-keys.v20261130000825 --save-exact
npm i @clerk/expo-native-components@0.4.11-signing-keys.v20261130000825 --save-exact
npm i @clerk/expo-passkeys@2.2.10-signing-keys.v20261130000825 --save-exact
npm i @clerk/express@2.1.100-signing-keys.v20261130000825 --save-exact
npm i @clerk/fastify@3.1.110-signing-keys.v20261130000825 --save-exact
npm i @clerk/hono@0.1.110-signing-keys.v20261130000825 --save-exact
npm i @clerk/localizations@4.28.1-signing-keys.v20261130000825 --save-exact
npm i @clerk/mosaic@0.13.1-signing-keys.v20261130000825 --save-exact
npm i @clerk/mosaic-docs@0.0.9-signing-keys.v20261130000825 --save-exact
npm i @clerk/msw@0.0.97-signing-keys.v20261130000825 --save-exact
npm i @clerk/nextjs@7.10.11-signing-keys.v20261130000825 --save-exact
npm i @clerk/nuxt@3.1.34-signing-keys.v20261130000825 --save-exact
npm i @clerk/react@6.19.8-signing-keys.v20261130000825 --save-exact
npm i @clerk/react-router@3.7.12-signing-keys.v20261130000825 --save-exact
npm i @clerk/shared@4.50.1-signing-keys.v20261130000825 --save-exact
npm i @clerk/swingset@0.0.84-signing-keys.v20261130000825 --save-exact
npm i @clerk/tanstack-react-start@1.7.23-signing-keys.v20261130000825 --save-exact
npm i @clerk/testing@2.3.10-signing-keys.v20261130000825 --save-exact
npm i @clerk/ui@1.46.1-signing-keys.v20261130000825 --save-exact
npm i @clerk/upgrade@2.0.12-signing-keys.v20261130000825 --save-exact
npm i @clerk/vue@2.5.30-signing-keys.v20261130000825 --save-exact |
dmoerner
left a comment
There was a problem hiding this comment.
Among the tradeoffs I checked but that I think are fine:
- The breakage for customers who set
CLERK_JWT_KEYis potentially real, but unavoidable and the changeset calls it out clearly. - Related to that: I think there are more paths where with an unknown kid we make more requests to origin to try to find it. However,
/v1/jwksis outside the normal BAPI rate limits so I don't see a DoS worry here (and it was already possible to trigger these kinds of hits).
Also say that a pinned jwtKey falls back to the JWKS when it cannot verify the token's alg, whether the key type or the algorithm differs.
|
API Changes Report
Summary
@clerk/backendCurrent version: 3.33.1 🟡 Non-breaking Changes (1)Modified:
|



Summary
Instances can publish several signing keys at once, for example an RSA key and an ES256 key under different
kids.@clerk/backendalready picks the verification key by the token'skid. This PR adds ES256 and makes adding and removing keys safe.verifyToken / verifyHandshakeToken / verifyMachineAuthToken decode header → { kid, alg } - jwtKey ? loadClerkJwkFromPem(jwtKey) # always RSA - : loadClerkJWKFromRemote(kid) + loadVerificationKey({ pem: jwtKey, kid, alg }) + pem → RSA or P-256 key; if it can't verify `alg` and secretKey is set → JWKS by kid + no pem → JWKS by kid verifyJwt - algs: RS256 | RS384 | RS512 + algs: RS256 | RS384 | RS512 | ES256 + key type must match alg (kty/crv, and the JWK `alg` when present), else invalid signature - subtle.verify(algorithm.name, …) + subtle.verify({ name: 'ECDSA', hash: 'SHA-256' } | RSASSA-PKCS1-v1_5, …)Evidence
token-invalid-algorithm.After: it verifies with an EC key from the JWKS and with a P-256 PEM
jwtKey.After:
keys.test.ts: a JWKS with RSA and EC kids resolves both; a new kid resolves after one refetch; a removed kid fails withjwk-kid-mismatchafter that refetch.After:
request.test.tsreturns a handshake.jwtKeywith an ES256 token fails.After:
verify.test.ts: withsecretKeyit loads the key from the JWKS in one fetch, in both directions (an RSA pin with an ES256 token, and a P-256 pin with an RS256 token); withoutsecretKeyit fails with zero fetches; a kid missing from the JWKS returnsjwk-kid-mismatch; and a compatible pin that fails the signature check never fetches.token-invalid-signature.Merge Danger
Door: two-way
A minor
@clerk/backendrelease. Apart from the error reasons below, nothing changes until an instance publishes a second key or signs with ES256. A revert restores the old verification behaviour.Blast Radius: verification
Every
@clerk/backendconsumer goes through these paths. Visible changes:jwtKeycan make a network call. When it can't verify a token'salg(a different key type or algorithm) andsecretKeyis set, the SDK fetches the JWKS, the same way an unknownkidalready does forsecretKey-only setups.session-token-kid-mismatchinstead ofjwk-kid-mismatch; Authorization-header requests are unchanged.algdoesn't match the token'salgnow fails astoken-invalid-signaturerather thantoken-verification-failed.JwksKeyJSONgains optionalcrv,xandy. This is additive.