Fix/action forward loop guard #84525
Open
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.
What?
Fixes #84504
Add a minimal loop-detection guard to
createForwardedActionResponse
so a Server Action request that has already been forwarded (marked byx-action-forwarded=1
) is not forwarded again. Instead, we return early (no second forward).packages/next/src/server/app-render/action-handler.ts
x-action-forwarded=1
is present on the incoming request, return fromcreateForwardedActionResponse
without forwarding again.Why?
In apps that use Middleware rewrites, a forwarded Server Action request can be rewritten to a path that triggers forwarding again, causing an infinite loop (e.g.
/rewrite/other → /rewrite/... → /rewrite/rewrite/... → …
). This leads to repeated logs and a server action that never resolves.This guard breaks the cycle with very low risk by only affecting requests that were already forwarded once.
npm i && npm run dev
http://localhost:3000
How?
At the start of
createForwardedActionResponse
, inspect the incoming headers:x-action-forwarded=1
is present → return (do not forward again).x-action-forwarded=1
on the outgoing forwarded request.Works in both Node and Edge:
req.headers
is already aHeaders
(Edge), use it.HeadersAdapter
.Test Plan
Manual (using the public repro)
/rewrite/...
requests or log spam.Notes: This PR intentionally focuses on loop prevention. A broader follow-up could make forwarded requests mirror the pre-rewrite target to ensure resolution semantics remain identical under rewrites. That would be a separate change.
Risk
Low:
x-action-forwarded=1
.For Contributors Checklist
Fixes #84504
)pnpm prettier-fix
locally