Skip to content

#77599 šŸ› bug: fix infinite loop caused by redirect function with empt…#92750

Open
sergioramirez-dev wants to merge 10 commits intovercel:canaryfrom
sergioramirez-dev:issue_77599-redirect-empty-url
Open

#77599 šŸ› bug: fix infinite loop caused by redirect function with empt…#92750
sergioramirez-dev wants to merge 10 commits intovercel:canaryfrom
sergioramirez-dev:issue_77599-redirect-empty-url

Conversation

@sergioramirez-dev
Copy link
Copy Markdown

Fix: Prevent infinite redirect loop for redirect("")

Problem

Calling redirect("") results in an infinite redirect loop in the browser due to an empty Location header, which resolves to the current URL.

Cause

The redirect response sets Location: "", and the client router does not guard against self-redirects.

Solution

Validate the redirect target and reject empty strings.

if (!url || url.trim() === "") {
  throw new Error("Invariant: attempted to redirect to an empty URL. This causes infinite redirect loops.");
}

Changes

  • Added validation for empty redirect targets
  • Ensured Location header is never empty.

Tests

Throws on redirect("") and redirect(" ")
Valid redirects unchanged.

Notes

This prevents undefined behavior and improves error clarity. Treating empty strings as invalid is preferred over silent normalization.

Closes #77599

@nextjs-bot
Copy link
Copy Markdown
Collaborator

Allow CI Workflow Run

  • approve CI run for commit: 0d5e8e4

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@nextjs-bot
Copy link
Copy Markdown
Collaborator

nextjs-bot commented Apr 13, 2026

Allow CI Workflow Run

  • approve CI run for commit: cfed4e6

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Copy link
Copy Markdown
Contributor

@vercel vercel bot left a comment

Choose a reason for hiding this comment

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

Additional Suggestion:

The permanentRedirect function is missing the empty URL guard that was added to redirect, allowing permanentRedirect("") to cause an infinite redirect loop with a 308 status code.

Fix on Vercel

Comment thread test/unit/redirect-empty-url.test.ts Outdated
Copy link
Copy Markdown
Author

@sergioramirez-dev sergioramirez-dev left a comment

Choose a reason for hiding this comment

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

Have added URL guard to permanentRedirect and added just one test condition.

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.

redirect with empty string causes infinite loop

2 participants