Skip to content

fix(backend): Accept RSA jwtKey of any size - #11107

Merged
nikosdouvlis merged 4 commits into
mainfrom
nikos/backend-jwt-key-any-size
Dec 3, 2026
Merged

nikosdouvlis merged 4 commits into
mainfrom
nikos/backend-jwt-key-any-size

Conversation

@nikosdouvlis

@nikosdouvlis nikosdouvlis commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Summary

jwtKey / CLERK_JWT_KEY accepts an RSA public key of any size, such as 3072-bit. ES256 support and the JWKS fallback from #11036 are unchanged.

 loadClerkJwkFromPem
   P-256 PEM → EC JWK
-  RSA PEM   → strip a fixed 2048-bit SPKI prefix/suffix to get n, build an RS256 JWK
+  RSA PEM   → PEM string, imported by WebCrypto as SPKI

The prefix strip was a 2021 workaround for Next.js dev mode not parsing PEM keys. importKey has imported PEM strings directly since signJwt landed, so jwtKey now goes through that path too.

Evidence

  • Before: a 3072-bit jwtKey derives the wrong modulus, so every token fails with token-invalid-signature.
    After: verify.test.ts "verifies a token signed by a 3072-bit key" passes.
  • Two PEMs under the same kid each verify only their own token: "verifies only the token signed by its own key when two keys share a kid" passes.
  • A one-line PEM with literal \n or \r\n escapes, a PEM wrapped in double or single quotes, a bare base64 body and a base64url body all verify.
  • A garbage jwtKey still fails with token-invalid-signature, so the dev-mode handshake hint is unchanged.
  • The existing ES256 and JWKS fallback tests pass unedited, with the same error reasons.

Merge Danger

Door: two-way

Blast Radius: verification

  • An RSA jwtKey also verifies RS384 and RS512 tokens signed by the same key, without fetching the JWKS.
  • verifyJwt from @clerk/backend/jwt reports a PEM key that cannot be imported as token-invalid-signature, which is what a jwtKey already gave. Before, a bad PEM string passed to verifyJwt gave token-verification-failed. hasValidSignature now returns false for it instead of throwing.

@vercel

vercel Bot commented Dec 3, 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 Dec 3, 2026 2:49pm UTC
mosaic-docs Ready Ready Preview Dec 3, 2026 2:49pm UTC
swingset Ready Ready Preview Dec 3, 2026 2:49pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Pinned RSA keys can now be loaded as normalized SPKI strings, while P-256 keys continue to load as JWKs. Verification tests cover different RSA key sizes and encodings, mismatched keys with the same kid, invalid key strings, and RS384 tokens. When importing a string key fails, hasValidSignature returns false.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • clerk/javascript#11105: Both PRs change pinned jwtKey handling in @clerk/backend and cover verification with 3072-bit RSA keys.

Suggested reviewers: dominic-clerk, wobsoriano

Merge Risk: Minimal · up to 6e6af

This change lets pinned JWT keys of any RSA size verify tokens, and an unimportable key now reports an invalid signature instead of throwing. No concrete merge-blocking risk was identified in the supplied changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(backend): Accept RSA jwtKey of any size" names the main change. The changeset removes the fixed 2048-bit SPKI prefix/suffix handling in favor of importing RSA PEM keys through WebCrypto…
Description check Passed The description states that jwtKey / CLERK_JWT_KEY accepts RSA public keys of any size. It explains the old fixed 2048-bit SPKI stripping and the new WebCrypto SPKI import path. It lists test evidence…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix(backend): Accept RSA jwtKey of any size" names the main change. The changeset removes the fixed 2048-bit SPKI prefix/suffix handling in favor of importing RSA PEM keys through WebCrypto, so keys of any size are accepted. The title is short, specific, and follows a conventional commit style. It does not cover the extra accepted encodings or the error-handling change, which is acceptable.

Full details: Description check

Explanation

The description states that jwtKey / CLERK_JWT_KEY accepts RSA public keys of any size. It explains the old fixed 2048-bit SPKI stripping and the new WebCrypto SPKI import path. It lists test evidence, accepted key encodings, the RS384/RS512 behavior, and the change in error reporting for verifyJwt and hasValidSignature. It is directly related to the changeset.

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

Warning

Linked repositories: This review skipped 6 repositories. Cross-repository analysis may be incomplete. Check linked repository access and settings.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between d9882a6 and 6e6af5b.

Files selected for processing: 6

  • .changeset/backend-jwt-key-any-size.md
  • packages/backend/src/fixtures/index.ts
  • packages/backend/src/jwt/verifyJwt.ts
  • packages/backend/src/tokens/__tests__/keys.test.ts
  • packages/backend/src/tokens/__tests__/verify.test.ts
  • packages/backend/src/tokens/keys.ts
Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

Files with no reviewable changes: 1

  • packages/backend/src/fixtures/index.ts

Included review availability: This review used your included allowance. 7 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 10 reviews per hour.

@changeset-bot

changeset-bot Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6e6af5b

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

@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/electron

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

@clerk/electron-passkeys

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

@clerk/eslint-plugin

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

@clerk/expo

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

@clerk/expo-biometrics

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

@clerk/expo-google-signin

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

@clerk/expo-native-components

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/hono

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

@clerk/localizations

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

@clerk/mosaic

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/react

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

@clerk/react-router

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/ui

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

@clerk/upgrade

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

@clerk/vue

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

commit: 6e6af5b

@blacksmith-sh

blacksmith-sh Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Matches 3072-bit and 4096-bit RSA jwtKey verify tokens

Base rejects 3072- and 4096-bit jwtKeys with token-invalid-signature; head verifies 2048, 3072 and 4096-bit keys.

Merge-base

RSA key sizes

This PR 6e6af5bf

RSA key sizes

Also checked · 2 match

Matches Escaped, quoted and bare-base64 jwtKey formats verify
Matches Wrong or garbage jwtKey still rejected as invalid signature

Proof on 6e6af5bf · Did Proof get it right? Yes · No

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

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-12-03T14:53:29.806Z

Summary

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

No API Changes Detected

All packages have stable APIs with no detected changes.


Report generated by Break Check

Last ran on 6e6af5b.

@nikosdouvlis
nikosdouvlis merged commit a070563 into main Dec 3, 2026
71 of 106 checks passed
@nikosdouvlis
nikosdouvlis deleted the nikos/backend-jwt-key-any-size branch December 3, 2026 15:22

This branch was successfully deployed

2 active deployments
Preview – swingset — 6e6af5bf Deployed Dec 3, 2026 by vercel[bot]
Preview – clerk-js-sandbox — 6e6af5bf Deployed Dec 3, 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.

1 participant