fix: replace original request body after middleware execution (#77662)#7
Open
MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
Open
fix: replace original request body after middleware execution (#77662)#7MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
Conversation
…#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>
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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
finallyblock.Updates the Server Actions e2e fixtures to run under
experimental.nodeMiddlewareby default and adds parallel*-node-middlewaretest entrypoints plus amiddleware-node.js(withruntime: '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.