Skip to content

fix(backend): reject non-object JWT header or payload in decodeJwt - #11013

Merged
tmilewski merged 2 commits into
mainfrom
tom/session-token-whose-payload-is-json-null
Nov 25, 2026
Merged

tmilewski merged 2 commits into
mainfrom
tom/session-token-whose-payload-is-json-null

Conversation

@tmilewski

@tmilewski tmilewski commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Description

authenticateRequest() throws TypeError: Cannot read properties of null (reading 'iss') when the __session cookie holds a three-part token whose payload decodes to the JSON literal null. In Next.js, clerkMiddleware() calls authenticateRequest() before the route handler, so every route that the middleware covers returns a 500 until the user clears the cookie. That includes the sign-in page.

The cause is in decodeJwt(). It returned a decoded token whenever the header and the payload parsed as JSON, so values such as null, 42, or [] reached callers that expect objects. #9268 fixed segments that fail to parse, but not segments that parse to a value other than an object. A JWT header and a JWT claims set must both be JSON objects (RFC 7515 and RFC 7519).

decodeJwt() now returns a TokenVerificationError with reason token-invalid when the header or the payload is not a JSON object. Every caller already handles that error, so one check covers all of them:

  • authenticateRequest() returns a signed-out state with reason token-invalid when __client_uat is set, and a handshake when it is not.
  • Two other paths threw for the same reason and now return a signed-out state. One is a suffixed __session_<suffix> cookie with a null payload, which threw on payload.iat. The other is a machine token in the Authorization header with a null header, which threw on header.cat.
  • The public decodeJwt(), verifyJwt(), and verifyToken() exports throw a TokenVerificationError for these tokens. Before, decodeJwt() returned the token, and verifyJwt() and verifyToken() threw a TypeError for a null header.

Fixes #11010
Fixes CORE-4503

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

✎

@vercel

vercel Bot commented Nov 25, 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 25, 2026 7:07pm UTC
mosaic-docs Ready Ready Preview Nov 25, 2026 7:07pm UTC
swingset Ready Ready Preview Nov 25, 2026 7:07pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5f8f1fb

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

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

decodeJwt() now rejects JWT headers and payloads that decode to valid JSON values other than non-null, non-array objects. It returns a TokenInvalid error for these values. Tests cover non-object header and payload values, including a cookie token with a null payload, and a changeset documents the behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Severity of issue fixed: Low

Suggested reviewers: wobsoriano, dominic-clerk

Merge Risk: Minimal · up to 5f8f1

This change makes malformed session tokens whose payload is JSON null, or another non-object value, return a signed-out or handshake state instead of throwing a TypeError. No concrete merge-blocking risk was found. The change is small and covered by new tests.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: decodeJwt now rejects JWT headers or payloads that parse to non-object JSON values. The raw summary, the verifyJwt.ts change, and the request test all match this. The …
Linked Issues check Passed Issue #11010 requires that a three-part session token whose payload decodes to JSON null does not make authenticateRequest() throw a TypeError. The issue suggests rejecting non-object header or payloa…
Out of Scope Changes check Passed All changes support the linked objective. The changeset documents the patch for @clerk/backend. The verifyJwt.ts change is the core fix. The verifyJwt.test.ts and request.test.ts additions are regress…
Description check Passed The description states the bug (authenticateRequest() throws a TypeError when the __session cookie payload decodes to null), the cause (decodeJwt() accepts non-object header or payload values), and th…
Full details: Title check

Explanation

The title names the main change: decodeJwt now rejects JWT headers or payloads that parse to non-object JSON values. The raw summary, the verifyJwt.ts change, and the request test all match this. The title follows the conventional commit style and is specific enough for a teammate scanning history.

Full details: Linked Issues check

Explanation

Issue #11010 requires that a three-part session token whose payload decodes to JSON null does not make authenticateRequest() throw a TypeError. The issue suggests rejecting non-object header or payload in decodeJwt(). The diff to packages/backend/src/jwt/verifyJwt.ts adds an isJsonObject helper. decodeJwt() now returns a TokenVerificationError with reason TokenInvalid when the header or payload is null, a number, an array, or another non-object value. This matches the suggested fix and protects all callers. The PR adds regression tests in verifyJwt.test.ts for non-object header and payload segments, and in request.test.ts for a cookie session JWT with a null payload that expects a signed-out state with TokenInvalid. The fix covers the requirement. I did not trace the suffixed-cookie and machine-token paths named in the PR description. Those paths depend on existing caller handling of the same error, so they are not required by the issue.

Full details: Out of Scope Changes check

Explanation

All changes support the linked objective. The changeset documents the patch for @clerk/backend. The verifyJwt.ts change is the core fix. The verifyJwt.test.ts and request.test.ts additions are regression tests for that fix. No unrelated files or behavior changes were identified in the summary or the verifyJwt.ts diff. Public exports change only in error behavior for non-object tokens, which the issue requires.

Full details: Description check

Explanation

The description states the bug (authenticateRequest() throws a TypeError when the __session cookie payload decodes to null), the cause (decodeJwt() accepts non-object header or payload values), and the fix (decodeJwt() returns a TokenVerificationError with reason token-invalid for non-object header or payload). It also lists affected callers and the public export behavior changes, and links the related issues. This matches the changeset and the test additions.

  • 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 d69abb6 and 5f8f1fb.

Files selected for processing: 4

  • .changeset/decodejwt-non-object-segments.md
  • packages/backend/src/jwt/__tests__/verifyJwt.test.ts
  • packages/backend/src/jwt/verifyJwt.ts
  • packages/backend/src/tokens/__tests__/request.test.ts
Linked repositories identified

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

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

@blacksmith-sh

blacksmith-sh Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Matches Session cookie with null payload signs out instead of crashing

Before: TypeError reading 'iss'. After: signed-out, reason token-invalid.

Merge-base

authenticateRequest cases

This PR 5f8f1fb2

authenticateRequest cases

Also checked · 3 match

Matches Null payload without __client_uat triggers a handshake
Matches Suffixed cookie and machine token with null segments sign out
Matches Public decodeJwt, verifyJwt, verifyToken throw TokenVerificationError

Proof on 5f8f1fb2 · 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 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/electron

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

@clerk/electron-passkeys

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

@clerk/eslint-plugin

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

@clerk/expo

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

@clerk/expo-biometrics

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

@clerk/expo-google-signin

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

@clerk/expo-native-components

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/hono

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

@clerk/localizations

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

@clerk/mosaic

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/react

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

@clerk/react-router

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/ui

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

@clerk/upgrade

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

@clerk/vue

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

commit: 5f8f1fb

@github-actions

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-11-25T19:11:14.084Z

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 5f8f1fb.

@tmilewski
tmilewski merged commit 14c5168 into main Nov 25, 2026
63 checks passed
@tmilewski
tmilewski deleted the tom/session-token-whose-payload-is-json-null branch November 25, 2026 20:04

This branch was successfully deployed

2 active deployments
Preview – swingset — 5f8f1fb2 Deployed Nov 25, 2026 by vercel[bot]
Preview – clerk-js-sandbox — 5f8f1fb2 Deployed Nov 25, 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.

@clerk/backend: a session token whose payload is JSON null makes authenticateRequest() throw (TypeError on payload.iss)

2 participants