Skip to content

fix: replace original request body after middleware execution (#77662)#7

Open
MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
pr_037_after
Open

fix: replace original request body after middleware execution (#77662)#7
MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
pr_037_after

Conversation

@MitchLewis930
Copy link

@MitchLewis930 MitchLewis930 commented Jan 30, 2026

PR_037


Note

Medium Risk
Touches request body streaming/cleanup around middleware execution, which can impact POST/Server Actions behavior and is sensitive to edge cases in body consumption; mitigated by added e2e coverage.

Overview
Fixes Node.js runtime middleware invocation to avoid consuming/altering the original request body: when running Node middleware via the adapter, the server now passes a cloned body stream and always finalizes the cloneable body in a finally block.

Updates the Server Actions e2e fixtures to run under experimental.nodeMiddleware by default and adds parallel *-node-middleware test entrypoints plus a middleware-node.js (with runtime: 'nodejs') to validate actions + rewrites continue working with Node middleware.

Written by Cursor Bugbot for commit bda5dc3. This will update automatically on new commits. Configure here.

…#77662)

In vercel#77553 we fixed reading the
request body in middleware using the `nodejs` runtime. However, this
caused issues with subsequent reads like in server actions. In sandbox,
[we
run](https://github.com/vercel/next.js/blob/1e62ce2c61048ddc0297f1a4f268894541975521/packages/next/src/server/web/sandbox/sandbox.ts#L146-L148)
`.finalize()` after middleware is executed so we should do the same
here.

Fixes vercel#77646

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

})
} finally {
if (hasRequestBody) {
requestData.body.finalize()
Copy link

Choose a reason for hiding this comment

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

Missing await on async finalize() call

High Severity

The finalize() method is an async function that awaits an internal promise (endPromise) before replacing the request body, but it's called without await in the finally block. This means the function returns immediately without waiting for the body to be properly restored, creating a race condition where subsequent handlers may try to read the body before finalize() completes.

Fix in Cursor Fix in Web

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.

2 participants