Skip to content

feat(backend): Support ES256 and multiple signing keys (Core 2) - #11037

Merged
nikosdouvlis merged 4 commits into
release/core-2from
nikos/backend-multiple-signing-keys-core-2
Nov 30, 2026
Merged

nikosdouvlis merged 4 commits into
release/core-2from
nikos/backend-multiple-signing-keys-core-2

Conversation

@nikosdouvlis

@nikosdouvlis nikosdouvlis commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

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-2 the JWKS cache was process-wide and keyed by kid alone, 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/backend already picks the verification key by the token's kid. 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, …)
 loadClerkJWKFromRemote(kid)
   if cache expired or kid unknown → fetch JWKS
-    merge fetched keys into cache          # removed keys stay trusted
+    replace cache with fetched keys        # removed keys stop being trusted
+    concurrent misses share one fetch      # an older response can't overwrite a newer key set
 authenticateRequest, session cookie fails verification
-  expired / nbf / iat-in-future → handshake, else signed out
+  … or kid no longer in JWKS → handshake (cookie only), else signed out

Evidence

  • Before: an ES256 session token is rejected with token-invalid-algorithm.
    After: it verifies with an EC key from the JWKS and with a P-256 PEM jwtKey.
  • Before: keys removed from the JWKS could stay cached, and the cache was shared by every instance in the process.
    After: keys.test.ts: a JWKS with RSA and EC kids resolves both; a new kid resolves after one refetch; a removed kid fails with jwk-kid-mismatch after that refetch.
  • Before: an expired session cookie whose kid left the JWKS signs the user out.
    After: request.test.ts returns a handshake.
  • Before: an RSA jwtKey with an ES256 token fails.
    After: verify.test.ts: with secretKey it 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); without secretKey it fails with zero fetches; a kid missing from the JWKS returns jwk-kid-mismatch; and a compatible pin that fails the signature check never fetches.
  • Key/alg confusion (an ES256 header with an RSA key, an RS256 header with an EC key) is rejected as 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 different secretKey, apiUrl or apiVersion, each instance keeps its own cache TTL, and the JWK is always derived from the jwtKey passed in.
  • Backend tests pass on node, edge-runtime and miniflare.

Merge Danger

Door: two-way

A minor @clerk/backend release. 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/backend consumer goes through these paths. Visible changes:

  • Pinned jwtKey can make a network call. When it can't verify a token's alg (a different key type or algorithm) and secretKey is set, the SDK fetches the JWKS, the same way an unknown kid already does for secretKey-only setups.
  • New handshake case. Cookie requests whose kid is missing from the JWKS now go to a handshake. In production, mismatched publishable and secret keys therefore cost a few handshake redirects, bounded by the existing short-lived loop-counter cookie, before the user is signed out. In development the first failed handshake throws, as it does for other handshake failures. Cookie requests that can't handshake get the reason session-token-kid-mismatch instead of jwk-kid-mismatch; Authorization-header requests are unchanged.
  • Error reason for mismatched JWKs. A JWK whose type or alg doesn't match the token's alg now fails as token-invalid-signature rather than token-verification-failed.
  • Types. JwksKeyJSON gains optional crv, x and y. This is additive.
  • Per-instance cache (fix(backend): Scope the JWKS cache per Clerk instance #9394). A process that verifies tokens for several instances fetches and caches each instance's JWKS separately, and the jwk-kid-mismatch message no longer lists the cached key IDs.

@vercel

vercel Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
mosaic-docs Error Error Nov 30, 2026 1:52am UTC
swingset Error Error Nov 30, 2026 1:52am UTC
1 Skipped Deployment
Project Deployment Actions Updated
clerk-js-sandbox Skipped Skipped Nov 30, 2026 1:52am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 21aae58

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@clerk/backend Minor
@clerk/agent-toolkit Patch
@clerk/astro Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/remix Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch

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

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.
@blacksmith-sh

blacksmith-sh Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Matches ES256 session tokens verify

ES256 token verifies from the JWKS EC key and from a P-256 PEM jwtKey; base fails both with token-invalid-algorithm.

Merge-base

ES256 verification

This PR 38ebbdb5

ES256 verification

Also checked · 7 match

Matches ES256 machine and handshake tokens verify
Matches JWKS refetch replaces cached keys
Matches JWKS cache scoped per instance
Matches Concurrent cache misses share one fetch
Matches Pinned jwtKey falls back to JWKS on alg mismatch
Matches Key/alg confusion rejected as invalid signature
Matches Cookie with removed kid goes to handshake

Proof on 38ebbdb5 · mocked: Clerk Backend API /v1/jwks (Loopback HTTP fixture serves mutable JWKS (RSA and P-256 keys generated per run)...) · Did Proof get it right? Yes · No

View full Proof report · React 👎 or comment @codesmith dismiss this proof to dismiss

@pkg-pr-new

pkg-pr-new Bot commented Nov 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@11037

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@11037

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@11037

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@11037

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@11037

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@11037

@clerk/elements

npm i https://pkg.pr.new/@clerk/elements@11037

@clerk/clerk-expo

npm i https://pkg.pr.new/@clerk/clerk-expo@11037

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@11037

@clerk/express

npm i https://pkg.pr.new/@clerk/express@11037

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@11037

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@11037

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@11037

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@11037

@clerk/clerk-react

npm i https://pkg.pr.new/@clerk/clerk-react@11037

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@11037

@clerk/remix

npm i https://pkg.pr.new/@clerk/remix@11037

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@11037

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@11037

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@11037

@clerk/themes

npm i https://pkg.pr.new/@clerk/themes@11037

@clerk/types

npm i https://pkg.pr.new/@clerk/types@11037

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@11037

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@11037

commit: 21aae58

@dmoerner dmoerner 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.

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.

@nikosdouvlis
nikosdouvlis merged commit f7081df into release/core-2 Nov 30, 2026
75 of 79 checks passed
@nikosdouvlis
nikosdouvlis deleted the nikos/backend-multiple-signing-keys-core-2 branch November 30, 2026 02:05

This branch had an error being deployed

1 failed and 1 inactive deployments
Preview – swingset — 21aae580 Deployed Nov 30, 2026 by vercel[bot]
Preview – clerk-js-sandbox — 21aae580 Deployed Nov 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants