Skip to content

fix(astro): Do not consume the request body in middleware#9220

Merged
wobsoriano merged 6 commits into
mainfrom
rob/fix-js-issue-9210
Jul 22, 2026
Merged

fix(astro): Do not consume the request body in middleware#9220
wobsoriano merged 6 commits into
mainfrom
rob/fix-js-issue-9210

Conversation

@wobsoriano

@wobsoriano wobsoriano commented Jul 22, 2026

Copy link
Copy Markdown
Member

Description

This PR fixes #9210 by ensuring Astro’s clerkMiddleware() does not consume the original request body during authentication.

It also moves the existing patchRequest() implementations from React Router and TanStack React Start into @clerk/shared. Astro now uses the same shared helper, removing the duplicated implementations across the three SDKs.

In the future, we could consider applying this behavior inside authenticateRequest() itself.

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:

@changeset-bot

changeset-bot Bot commented Jul 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 18f0ebf

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

This PR includes changesets to release 23 packages
Name Type
@clerk/astro Patch
@clerk/react-router Patch
@clerk/shared Patch
@clerk/tanstack-react-start Patch
@clerk/backend Patch
@clerk/chrome-extension Patch
@clerk/clerk-js Patch
@clerk/electron Patch
@clerk/expo-passkeys Patch
@clerk/expo Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/headless Patch
@clerk/hono Patch
@clerk/localizations Patch
@clerk/msw Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react Patch
@clerk/testing Patch
@clerk/ui Patch
@clerk/vue 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

@vercel

vercel Bot commented Jul 22, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview, Comment Jul 22, 2026 8:43pm
swingset Ready Ready Preview, Comment Jul 22, 2026 8:43pm

Request Review

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 99f58824-fd6d-4764-aa77-8e8d4c441124

📥 Commits

Reviewing files that changed from the base of the PR and between 154b654 and 18f0ebf.

📒 Files selected for processing (1)
  • .changeset/cyan-needles-listen.md
🔗 Linked repositories identified

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

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)
💤 Files with no reviewable changes (1)
  • .changeset/cyan-needles-listen.md

📝 Walkthrough

Walkthrough

The change adds a shared patchRequest utility for request reconstruction, migrates React Router and TanStack React Start to use it, and applies it in Astro middleware before Clerk request creation. Tests verify POST cloning behavior and preservation of the original request body state. Changesets record patch releases for the affected packages.

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

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the Astro middleware body-consumption fix.
Description check ✅ Passed The description matches the PR by describing the Astro fix and shared patchRequest refactor.
Linked Issues check ✅ Passed The changes prevent Astro middleware from consuming the body by using a shared patched Request, matching #9210.
Out of Scope Changes check ✅ Passed The React Router and TanStack updates are part of the shared helper refactor and stay aligned with the stated fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jul 22, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/electron

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

@clerk/electron-passkeys

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

@clerk/eslint-plugin

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

@clerk/expo

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

@clerk/expo-google-signin

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/hono

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

@clerk/localizations

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/react

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

@clerk/react-router

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/ui

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

@clerk/upgrade

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

@clerk/vue

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

commit: 18f0ebf

@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-07-22T20:43:39.305Z

Summary

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

@clerk/shared

Current version: 4.25.6
Recommended bump: MINOR → 4.26.0

Subpath ./patchRequest

🟢 Additions (1)

Added: ./patchRequest

New subpath export ./patchRequest


Report generated by Break Check

Last ran on 18f0ebf.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
packages/astro/src/server/utils.ts (1)

30-30: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add an explicit return type to patchRequest.

Use : Request on this exported utility to make its API contract explicit.

As per coding guidelines, exported TypeScript functions must declare explicit return types. Based on learnings, exported functions in this repository should enforce the same annotation standard.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/astro/src/server/utils.ts` at line 30, Update the exported
patchRequest function signature to explicitly declare a Request return type,
preserving its existing implementation and behavior.

Sources: Coding guidelines, Learnings

packages/astro/src/server/clerk-middleware.ts (1)

86-87: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add a middleware regression test for body preservation.

Exercise a body-bearing request through clerkMiddleware(), then assert that the downstream Astro handler can still read context.request.json(). This should also verify that Clerk receives the cloned body rather than an empty request.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/astro/src/server/clerk-middleware.ts` around lines 86 - 87, Add a
regression test around clerkMiddleware() that sends a request with a JSON body,
verifies Clerk receives the cloned body, and confirms the downstream Astro
handler can still read the same payload through context.request.json().
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/astro/src/server/clerk-middleware.ts`:
- Around line 86-87: Add a regression test around clerkMiddleware() that sends a
request with a JSON body, verifies Clerk receives the cloned body, and confirms
the downstream Astro handler can still read the same payload through
context.request.json().

In `@packages/astro/src/server/utils.ts`:
- Line 30: Update the exported patchRequest function signature to explicitly
declare a Request return type, preserving its existing implementation and
behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: e2dd58c6-906c-4253-9883-4f8b41f19016

📥 Commits

Reviewing files that changed from the base of the PR and between 68ae308 and fb46521.

📒 Files selected for processing (3)
  • .changeset/cyan-needles-listen.md
  • packages/astro/src/server/clerk-middleware.ts
  • packages/astro/src/server/utils.ts
🔗 Linked repositories identified

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

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/shared/src/__tests__/patchRequest.spec.ts (1)

45-54: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert that the original body remains readable.

bodyUsed === false confirms the stream was not disturbed, but the test does not exercise the downstream operation this PR protects. Read the original body after patchRequest and assert its contents.

As per coding guidelines, unit tests for new functionality must verify behavior and edge cases.

Suggested test addition
-  it('clones POST requests without forwarding the body', () => {
+  it('clones POST requests without forwarding the body', async () => {
...
     expect(cloned.body).toBeNull();
     expect(original.bodyUsed).toBe(false);
+    expect(await original.text()).toBe('payload');
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/shared/src/__tests__/patchRequest.spec.ts` around lines 45 - 54,
Update the POST request test around patchRequest to read the original request
body after patchRequest(original) and assert that it remains readable with the
expected “payload” contents, while preserving the existing method, cloned-body,
and bodyUsed assertions.

Source: Coding guidelines

🧹 Nitpick comments (1)
packages/shared/src/patchRequest.ts (1)

6-6: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add an explicit return type to patchRequest.
Use : Request here so the exported helper’s contract is obvious.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/shared/src/patchRequest.ts` at line 6, Update the exported
patchRequest function signature to explicitly declare the Request return type,
preserving its existing implementation and behavior.

Sources: Coding guidelines, Learnings

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@packages/shared/src/__tests__/patchRequest.spec.ts`:
- Around line 45-54: Update the POST request test around patchRequest to read
the original request body after patchRequest(original) and assert that it
remains readable with the expected “payload” contents, while preserving the
existing method, cloned-body, and bodyUsed assertions.

---

Nitpick comments:
In `@packages/shared/src/patchRequest.ts`:
- Line 6: Update the exported patchRequest function signature to explicitly
declare the Request return type, preserving its existing implementation and
behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 8994fc8a-ec3b-4a71-b58d-3e09ae8c29ff

📥 Commits

Reviewing files that changed from the base of the PR and between fb46521 and 451bdfe.

📒 Files selected for processing (9)
  • .changeset/cyan-needles-listen.md
  • packages/astro/src/server/clerk-middleware.ts
  • packages/react-router/src/server/clerkMiddleware.ts
  • packages/react-router/src/server/loadOptions.ts
  • packages/react-router/src/server/utils.ts
  • packages/shared/src/__tests__/patchRequest.spec.ts
  • packages/shared/src/patchRequest.ts
  • packages/tanstack-react-start/src/server/clerkMiddleware.ts
  • packages/tanstack-react-start/src/server/utils/index.ts
🔗 Linked repositories identified

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

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)
💤 Files with no reviewable changes (2)
  • packages/react-router/src/server/utils.ts
  • packages/tanstack-react-start/src/server/utils/index.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • .changeset/cyan-needles-listen.md
  • packages/astro/src/server/clerk-middleware.ts

@wobsoriano
wobsoriano merged commit 097432d into main Jul 22, 2026
51 checks passed
@wobsoriano
wobsoriano deleted the rob/fix-js-issue-9210 branch July 22, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[@clerk/astro] clerkMiddleware() consumes request body, breaking Cloudflare Durable Objects / downstream body reads

2 participants