Repository navigation
feat(backend): Support ES256 and multiple signing keys (Core 2) - #11037
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
1 Skipped Deployment
|
🦋 Changeset detectedLatest commit: 21aae58 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 |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
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.
|
@clerk/agent-toolkit
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/dev-cli
@clerk/elements
@clerk/clerk-expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/clerk-react
@clerk/react-router
@clerk/remix
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/themes
@clerk/types
@clerk/upgrade
@clerk/vue
commit: |
dmoerner
left a comment
There was a problem hiding this comment.
One small agent finding where this could cause a few extra handshakes
Core-2-only finding
A kid-mismatched cookie on a non-GET document request now redirects to the handshake. On main, isRequestEligibleForHandshake() returns false for non-GET requests; main's comment says FAPI answers a redirected POST with 405. Core-2 has no such guard (packages/backend/src/tokens/handshake.ts:112).
- Before this PR on core-2: a form POST with a cookie whose kid is not in the JWKS was signed out.
- After: it gets a 307 to the handshake, via the new branch at packages/backend/src/tokens/request.ts:692.
Core-2 already does this for expired tokens on POST, so the PR widens an existing gap rather than opening a new one. The window is small: a key removed while tokens signed by it are still live, or stale cookies after a key switch. I did not verify the 405 against FAPI myself.
Decision for the author: backport the method !== 'GET' guard to core-2, or accept it as consistent with core-2's existing expired-token behaviour. I'd accept it for this PR and backport the guard separately, since it also fixes the expired-token case.


Port of #11036 to Core 2.
This also brings the per-instance JWKS cache from #9394, as its own commit (cherry-picked cleanly). On
release/core-2the JWKS cache was process-wide and keyed bykidalone, and the PEM-derived key shared that map. #11036's cache replacement and shared refetch build on the per-instance cache.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.keys.test.ts(from fix(backend): Scope the JWKS cache per Clerk instance #9394): a cached key is not served to a differentsecretKey,apiUrlorapiVersion, each instance keeps its own cache TTL, and the JWK is always derived from thejwtKeypassed in.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.jwk-kid-mismatchmessage no longer lists the cached key IDs.