Skip to content

feat(backend): Support ES256 and multiple signing keys - #11036

Merged
nikosdouvlis merged 5 commits into
mainfrom
nikos/backend-multiple-signing-keys
Nov 30, 2026
Merged

nikosdouvlis merged 5 commits into
mainfrom
nikos/backend-multiple-signing-keys

Conversation

@nikosdouvlis

@nikosdouvlis nikosdouvlis commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

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.
    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.
  • Backend tests pass on node and edge-runtime.

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.

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

vercel Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
clerk-js-sandbox Ready Ready Preview Nov 30, 2026 1:11am UTC
mosaic-docs Ready Ready Preview Nov 30, 2026 1:11am UTC
swingset Ready Ready Preview Nov 30, 2026 1:11am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 524d75e

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/astro Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/hono Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch
@clerk/swingset 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 29, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/electron

npm i https://pkg.pr.new/@clerk/electron@11036

@clerk/electron-passkeys

npm i https://pkg.pr.new/@clerk/electron-passkeys@11036

@clerk/eslint-plugin

npm i https://pkg.pr.new/@clerk/eslint-plugin@11036

@clerk/expo

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

@clerk/expo-biometrics

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

@clerk/expo-google-signin

npm i https://pkg.pr.new/@clerk/expo-google-signin@11036

@clerk/expo-native-components

npm i https://pkg.pr.new/@clerk/expo-native-components@11036

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@11036

@clerk/localizations

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

@clerk/mosaic

npm i https://pkg.pr.new/@clerk/mosaic@11036

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/react

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

@clerk/react-router

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@11036

@clerk/upgrade

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

@clerk/vue

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

commit: 524d75e

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;

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.

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.

@nikosdouvlis

Copy link
Copy Markdown
Member Author

!snapshot signing-keys

@@ -272,6 +272,9 @@ export interface JwksKeyJSON {
alg: string;
n: string;
e: string;

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.

n and e should also be optional now? Or we cannot change that type without a breaking change?

@github-actions

Copy link
Copy Markdown
Contributor

Hey @nikosdouvlis - the snapshot version command generated the following package versions:

Package Version
@clerk/astro 4.1.34-signing-keys.v20261130000825
@clerk/backend 3.34.0-signing-keys.v20261130000825
@clerk/chrome-extension 3.1.118-signing-keys.v20261130000825
@clerk/clerk-js 6.48.2-signing-keys.v20261130000825
@clerk/electron 0.1.23-signing-keys.v20261130000825
@clerk/electron-passkeys 0.0.4-signing-keys.v20261130000825
@clerk/eslint-plugin 0.2.1-signing-keys.v20261130000825
@clerk/expo 4.17.9-signing-keys.v20261130000825
@clerk/expo-biometrics 1.1.1-signing-keys.v20261130000825
@clerk/expo-google-signin 1.1.1-signing-keys.v20261130000825
@clerk/expo-native-components 0.4.11-signing-keys.v20261130000825
@clerk/expo-passkeys 2.2.10-signing-keys.v20261130000825
@clerk/express 2.1.100-signing-keys.v20261130000825
@clerk/fastify 3.1.110-signing-keys.v20261130000825
@clerk/hono 0.1.110-signing-keys.v20261130000825
@clerk/localizations 4.28.1-signing-keys.v20261130000825
@clerk/mosaic 0.13.1-signing-keys.v20261130000825
@clerk/mosaic-docs 0.0.9-signing-keys.v20261130000825
@clerk/msw 0.0.97-signing-keys.v20261130000825
@clerk/nextjs 7.10.11-signing-keys.v20261130000825
@clerk/nuxt 3.1.34-signing-keys.v20261130000825
@clerk/react 6.19.8-signing-keys.v20261130000825
@clerk/react-router 3.7.12-signing-keys.v20261130000825
@clerk/shared 4.50.1-signing-keys.v20261130000825
@clerk/swingset 0.0.84-signing-keys.v20261130000825
@clerk/tanstack-react-start 1.7.23-signing-keys.v20261130000825
@clerk/testing 2.3.10-signing-keys.v20261130000825
@clerk/ui 1.46.1-signing-keys.v20261130000825
@clerk/upgrade 2.0.12-signing-keys.v20261130000825
@clerk/vue 2.5.30-signing-keys.v20261130000825

Tip: Use the snippet copy button below to quickly install the required packages.
@clerk/astro

npm i @clerk/astro@4.1.34-signing-keys.v20261130000825 --save-exact

@clerk/backend

npm i @clerk/backend@3.34.0-signing-keys.v20261130000825 --save-exact

@clerk/chrome-extension

npm i @clerk/chrome-extension@3.1.118-signing-keys.v20261130000825 --save-exact

@clerk/clerk-js

npm i @clerk/clerk-js@6.48.2-signing-keys.v20261130000825 --save-exact

@clerk/electron

npm i @clerk/electron@0.1.23-signing-keys.v20261130000825 --save-exact

@clerk/electron-passkeys

npm i @clerk/electron-passkeys@0.0.4-signing-keys.v20261130000825 --save-exact

@clerk/eslint-plugin

npm i @clerk/eslint-plugin@0.2.1-signing-keys.v20261130000825 --save-exact

@clerk/expo

npm i @clerk/expo@4.17.9-signing-keys.v20261130000825 --save-exact

@clerk/expo-biometrics

npm i @clerk/expo-biometrics@1.1.1-signing-keys.v20261130000825 --save-exact

@clerk/expo-google-signin

npm i @clerk/expo-google-signin@1.1.1-signing-keys.v20261130000825 --save-exact

@clerk/expo-native-components

npm i @clerk/expo-native-components@0.4.11-signing-keys.v20261130000825 --save-exact

@clerk/expo-passkeys

npm i @clerk/expo-passkeys@2.2.10-signing-keys.v20261130000825 --save-exact

@clerk/express

npm i @clerk/express@2.1.100-signing-keys.v20261130000825 --save-exact

@clerk/fastify

npm i @clerk/fastify@3.1.110-signing-keys.v20261130000825 --save-exact

@clerk/hono

npm i @clerk/hono@0.1.110-signing-keys.v20261130000825 --save-exact

@clerk/localizations

npm i @clerk/localizations@4.28.1-signing-keys.v20261130000825 --save-exact

@clerk/mosaic

npm i @clerk/mosaic@0.13.1-signing-keys.v20261130000825 --save-exact

@clerk/mosaic-docs

npm i @clerk/mosaic-docs@0.0.9-signing-keys.v20261130000825 --save-exact

@clerk/msw

npm i @clerk/msw@0.0.97-signing-keys.v20261130000825 --save-exact

@clerk/nextjs

npm i @clerk/nextjs@7.10.11-signing-keys.v20261130000825 --save-exact

@clerk/nuxt

npm i @clerk/nuxt@3.1.34-signing-keys.v20261130000825 --save-exact

@clerk/react

npm i @clerk/react@6.19.8-signing-keys.v20261130000825 --save-exact

@clerk/react-router

npm i @clerk/react-router@3.7.12-signing-keys.v20261130000825 --save-exact

@clerk/shared

npm i @clerk/shared@4.50.1-signing-keys.v20261130000825 --save-exact

@clerk/swingset

npm i @clerk/swingset@0.0.84-signing-keys.v20261130000825 --save-exact

@clerk/tanstack-react-start

npm i @clerk/tanstack-react-start@1.7.23-signing-keys.v20261130000825 --save-exact

@clerk/testing

npm i @clerk/testing@2.3.10-signing-keys.v20261130000825 --save-exact

@clerk/ui

npm i @clerk/ui@1.46.1-signing-keys.v20261130000825 --save-exact

@clerk/upgrade

npm i @clerk/upgrade@2.0.12-signing-keys.v20261130000825 --save-exact

@clerk/vue

npm i @clerk/vue@2.5.30-signing-keys.v20261130000825 --save-exact

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

Among the tradeoffs I checked but that I think are fine:

  1. The breakage for customers who set CLERK_JWT_KEY is potentially real, but unavoidable and the changeset calls it out clearly.
  2. 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/jwks is 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.
@blacksmith-sh

blacksmith-sh Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Matches ES256 session tokens verify

ES256 token verifies via the JWKS EC key and via a P-256 PEM jwtKey on head; base rejects both with token-invalid-algorithm. RS256 still verifies, and an edited ES256 payload fails as token-invalid-signature.

Merge-base

ES256 verifyToken

This PR 524d75e5

ES256 verifyToken

Also checked · 1 match · 5 not run

Not run Concurrent JWKS misses share one fetch · Not run
Not run Cookie with removed kid gets a handshake · Not run
Not run Pinned jwtKey falls back to JWKS on alg mismatch · Not run
Not run Key/alg confusion rejected as invalid signature · Not run
Not run ES256 handshake and M2M tokens verify · Not run
Matches JWKS key rotation: add and remove kids

Proof on 524d75e5 · interrupted

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

@nikosdouvlis
nikosdouvlis merged commit 8e97b2d into main Nov 30, 2026
102 of 104 checks passed
@nikosdouvlis
nikosdouvlis deleted the nikos/backend-multiple-signing-keys branch November 30, 2026 01:49
@github-actions

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-11-30T01:49:30.814Z

Summary

Metric Count
Packages analyzed 20
Packages with changes 1
🔴 Breaking changes 0
🟡 Non-breaking changes 1
🟢 Additions 0

🤖 This report was reviewed by claude-sonnet-4-6.


@clerk/backend

Current version: 3.33.1
Recommended bump: MINOR → 3.34.0

🟡 Non-breaking Changes (1)

Modified: verifyToken

// ... 6 unchanged lines elided ...
      secretKey?: string | undefined;
      apiUrl?: string | undefined;
      apiVersion?: string | undefined;
-     skipJwksCache?: boolean | undefined;
      jwksCacheTtlInMs?: number | undefined;
+     skipJwksCache?: boolean | undefined;
      jwtKey?: string | undefined;
  }) => Promise<NonNullable<import("@clerk/shared/types").JwtPayload | undefined>>

Static analyzer: Breaking change in function verifyToken: Parameter options type changed: {audience?:string|string[]|undefined;authorizedParties?:string[]|undefined;clockSkewInMs?:number|undefined;issuer?:string|string[]|undefined;headerType?:string|string[]|undefined;secretKey?:string|undefined;apiUrl?:string|undefined;apiVersion?:string|undefined;skipJwksCache?:boolean|undefined;jwksCacheTtlInMs?:number|undefined;jwtKey?:string|undefined;} → {audience?:string|string[]|undefined;authorizedParties?:string[]|undefined;clockSkewInMs?:number|undefined;issuer?:string|string[]|undefined;headerType?:string|string[]|undefined;secretKey?:string|undefined;apiUrl?:string|undefined;apiVersion?:string|undefined;jwksCacheTtlInMs?:number|undefined;skipJwksCache?:boolean|undefined;jwtKey?:string|undefined;}

🤖 AI review (reclassified as non-breaking) (99%): The only difference between the before and after signatures is the order of skipJwksCache and jwksCacheTtlInMs in the options object type; all properties are identical and optional, so no consumer code is affected.


Report generated by Break Check

Last ran on 524d75e.

This branch was successfully deployed

2 active deployments
Preview – swingset — 524d75e5 Deployed Nov 30, 2026 by vercel[bot]
Preview – clerk-js-sandbox — 524d75e5 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants