Security9 min read

How to Prevent Open Redirect Vulnerabilities in a Next.js SaaS (CVE-2026-64645)

Open redirect vulnerabilities in Next.js let attackers hijack login flows. Learn how CVE-2026-64645 works and how to prevent these attacks in your SaaS.

An open redirect vulnerability is one of the most underestimated flaws in SaaS security. It does not grant an attacker direct access to your database, but it weaponizes your trusted domain — turning a link to your application into a launching pad for phishing, credential theft, and OAuth token interception. In July 2026, the Next.js team patched CVE-2026-64645, an open redirect and SSRF flaw in how rewrites() and redirects() rules build destination hostnames from request-controlled input, affecting versions 12.0.0 through 15.5.20 and 16.0.0 through 16.2.10.

Beyond the CVE, the deeper problem is structural: Next.js does not validate redirect targets for you. redirect(), NextResponse.redirect(), and your next.config.js rules send users wherever the destination string resolves, including off your domain. This guide covers the CVE, the attack patterns that persist even after patching, and the full set of defenses you need in a production SaaS. For the security headers that complement redirect protection, see our Next.js security headers guide and the companion post on CSRF prevention in Next.js SaaS.

What Is an Open Redirect Vulnerability?

An open redirect occurs when your application uses attacker-controlled input — a URL parameter, a header value, or a request path — to build a redirect target, and that target is not validated against an allowlist of trusted destinations. The user's browser follows the redirect because it originated from your domain, which means the attacker gets the credibility of your application while routing the victim wherever they choose.

  • →Phishing via trusted URL: an attacker sends a link to https://your-app.com/login?next=https://evil.com. The user sees your domain in the browser bar, authenticates on your site, and is redirected to the attacker's credential-harvesting page. The legitimate first hop defeats spam filters and corporate proxies that would block a direct link to a malicious site.
  • →OAuth token theft: in OAuth 2.0 flows, if the redirect_uri parameter passes through your app without strict validation, an attacker substitutes their own URI and captures the authorization code or access token — a complete account takeover, not just a phishing redirect.
  • →SSRF via rewrites: when the same flaw appears in a rewrites() rule rather than a client-side redirect, the attacker proxies the request through your server to internal infrastructure — cloud metadata endpoints, internal APIs, or services that trust the server's network identity.
  • →Phishing credibility amplification: a link to a known, indexed domain passes email security gateways and reputation checks that a direct link to a malicious domain would not. The first hop being your domain is the entire value to the attacker.

CVE-2026-64645: The Next.js Security Flaw Explained

CVE-2026-64645, disclosed in Next.js's July 2026 security release, is a vulnerability in how Next.js processes rewrites() and redirects() configuration rules when the destination includes interpolated values derived from request input. When an attacker can control the host portion of the destination hostname — through a path segment, a query parameter, or a header that the rule interpolates — the redirect or rewrite sends the browser or the server to an attacker-chosen host.

  • →Affected versions: Next.js 12.0.0 through 15.5.20 and 16.0.0 through 16.2.10. The July 2026 release also patches two related flaws — CVE-2026-64649 and CVE-2026-64642 — in the same advisory covering Server Actions and Middleware.
  • →Fixed versions: upgrade to 15.5.21 or 16.2.11. If you are on an end-of-life version, upgrading is the only authoritative fix; there is no configuration workaround that fully closes the flaw in affected versions.
  • →Impact for rewrites(): the Next.js server proxies the request to the attacker-chosen host, potentially reaching internal services, cloud metadata endpoints at 169.254.169.254, or other back-end infrastructure not meant to be reachable from the internet.
  • →Impact for redirects(): the browser is sent to the attacker-chosen domain. No server-side request is made, but the victim trusts the redirect because it originated from your application's domain.
  • →CVSS score: rated 6.1 (Medium) by some sources and 8.3 (High) by others. The divergence reflects whether SSRF or open redirect impact is considered primary. Treat it as High for rewrites() exposure and Medium for redirects()-only exposure.

The Login Callback: The Most Common Open Redirect Attack Vector

The login callback redirect is the attack pattern that causes real user harm in SaaS applications, independent of your Next.js version. When users navigate to a protected route and are sent to login, most apps preserve the original destination in a query parameter — callbackUrl, returnTo, next, or redirect — so the user lands where they intended after authenticating. If that parameter is reflected into the redirect without validation, any link to your login page can redirect a freshly authenticated user anywhere.

  • →Protocol-relative URL bypass: the payload //evil.com starts with a slash, so a startsWith('/') check passes it. The browser interprets //evil.com as https://evil.com and follows it. Percent-encoded variants like /%2F%2Fevil.com decode to the same destination before the browser resolves the redirect.
  • →Backslash normalization: /\evil.com is normalized to //evil.com by some browsers. Any redirect validation that only checks for a leading slash fails on this input, as does any regex that anchors only on the first character.
  • →OAuth account takeover path: in OAuth 2.0 flows, if the redirect_uri is derived from a callbackUrl or returnTo parameter that an attacker controls, the authorization code is delivered to the attacker's server on the first request. The attacker exchanges the code for an access token without the victim ever realizing what happened.
  • →Session cookie leakage: some flows set a session cookie before the final redirect. If the destination is attacker-controlled, the session token appears in the Referer header on the subsequent request to the attacker's server, depending on the Referrer-Policy configuration of the destination page.

How to Validate Redirect Targets Correctly in Next.js

The correct validation pattern constructs the target as a URL object against your own origin, then compares the resulting hostname to your domain. This handles percent-encoding, protocol-relative URLs, backslash normalization, and unicode normalization in one step. String-based prefix checks do not handle these cases reliably and have known bypass payloads in every security scanner's test suite.

  • →Use new URL(target, origin) where origin is your app's base URL such as https://your-app.com. If the constructor throws, the target is not a valid URL — reject it. If the resulting .host does not match your known domain, reject it and fall back to a safe default like /dashboard.
  • →Never rely on target.startsWith('/') as your only check. The string //evil.com starts with a slash but resolves to https://evil.com. The string /\evil.com is normalized by some browsers to the same thing. Both are open redirect bypasses that this check misses.
  • →Build a getSafeRedirect(target, fallback) helper and call it everywhere you read a redirect parameter: login callbacks, post-payment returns, email verification flows, and invitation acceptance links. One function, tested once, is easier to audit than ad hoc checks in every handler.
  • →Allowlist external hosts explicitly: if your app legitimately redirects to a small set of external partners, maintain an explicit array of allowed hostnames and check the constructed URL's .host against it. Do not use a regex — attackers reliably find edge cases in regex-based URL validation.
  • →Validate at the point of use, not at the point of storage. A redirect parameter stored in a cookie from an earlier step in your own flow must be re-validated when you read it and act on it. Trust no input that passed through the browser.

Securing rewrites() and redirects() in next.config.js

CVE-2026-64645 specifically targets next.config.js rules where the destination hostname is built from request-controlled values. Review every rule in your configuration for this pattern. Upgrading Next.js fixes the library-level issue; it does not fix a custom rule you wrote that interpolates attacker-controlled input into the destination hostname.

  • →Hard-code destination hostnames: if your rewrite rule proxies to an upstream API, write the full hostname literally. Never use a captured :param segment or a header value as part of the host in the destination string.
  • →Use has() conditions to constrain which requests match a rule: a rule with a has condition on a specific header value or query string limits the input space an attacker can reach, reducing the risk of an unexpected match.
  • →If you need dynamic upstream routing — routing to different backends based on subdomain or region — do it in Middleware, where you can fully validate and allowlist the destination before constructing the NextResponse.rewrite() URL.
  • →After upgrading, audit your existing rules against the bypass payloads. A rule that constructed a valid internal hostname but allowed the hostname segment to be replaced by an attacker-controlled value was your vulnerability, not the library's. Upgrading does not remove that rule.

Middleware Redirect Validation in Next.js App Router

Next.js Middleware runs before the page renders and is the right place to enforce redirect validation for all authenticated flows. Because Middleware executes before any route handler, you can validate, rewrite, and redirect in one place — keeping the logic out of individual route handlers where it is easy to miss during a security review.

  • →Read the callback or redirect parameter from request.nextUrl.searchParams and apply getSafeRedirect() before constructing NextResponse.redirect(). If the target fails validation, redirect to your default post-login route — do not return a 400, which gives attackers useful signal about your validation logic.
  • →Log invalid redirect attempts to your security event pipeline. A spike in requests where the callbackUrl or next parameter is an external URL is an active phishing campaign using your domain. Alert on it at the same severity as a failed brute-force login attempt.
  • →Write explicit unit tests for your getSafeRedirect() helper covering the standard bypass payloads: //evil.com, /\evil.com, /%2F%2Fevil.com, https://evil.com, an empty string, and a null value. These are in every security scanner's open redirect test suite and represent the minimum coverage bar.
  • →Review every location where NextResponse.redirect() appears in your codebase — not just the login callback. Post-payment confirmation routes, email link handlers, invitation acceptance flows, and OAuth callback handlers are all common locations where redirect input is accepted and insufficiently validated.

Frequently Asked Questions

What is CVE-2026-64645 in Next.js?

CVE-2026-64645 is an open redirect and SSRF vulnerability disclosed in Next.js's July 2026 security release. It affects how rewrites() and redirects() rules in next.config.js build destination hostnames when attacker-controlled request input is interpolated into the hostname position. For rewrites, the server proxies to the attacker's host (SSRF). For redirects, the browser is sent to the attacker's domain. Fixed in Next.js 15.5.21 and 16.2.11.

How do I check if my Next.js app is vulnerable to open redirect attacks?

Check two things. First, your Next.js version — if you are on 12.0.0 through 15.5.20 or 16.0.0 through 16.2.10, upgrade to 15.5.21 or 16.2.11 to close CVE-2026-64645. Second, test every route that reads a URL parameter such as callbackUrl, next, returnTo, or redirect by appending the payload ?next=//evil.com to your login URL and observing where the app sends you after authentication. If you land on evil.com, the redirect is unvalidated.

Why doesn't startsWith('/') prevent open redirect attacks in Next.js?

The string //evil.com starts with a slash but is a protocol-relative URL that browsers resolve as https://evil.com. A startsWith('/') check passes it. The payloads /\evil.com and /%5Cevil.com also bypass this check after browser normalization. The correct check is to pass the target to new URL(target, origin) and verify the resulting .host matches your domain — this handles all normalization paths before the comparison is made.

Can an open redirect lead to account takeover in a SaaS app?

Yes, specifically in OAuth 2.0 authentication flows. If your post-login redirect is derived from a parameter an attacker controls, and that redirect is used as or influences the OAuth redirect_uri, the authorization code is delivered to the attacker's server. The attacker exchanges the code for an access token and completes a full account takeover. Outside OAuth, an open redirect can also expose session tokens to an attacker through Referer headers on the redirect, depending on your Referrer-Policy configuration.

How Belsoft Helps Secure Your Next.js SaaS

A Belsoft security audit covers the full surface of your Next.js application: manual testing of all authentication flows including login callbacks, OAuth, and post-payment redirects; API route authorization; Supabase or Firebase data access rules; middleware validation; and AI feature red-teaming. We specifically test for open redirect patterns in every entry point that accepts a redirect parameter — the class of vulnerability CVE-2026-64645 represents — and return a detailed report with prioritized fixes and optional fix support. Our security audit service is designed for SaaS teams preparing for enterprise customers, handling a security questionnaire, or who shipped with an AI coding tool and haven't yet hardened the application.

Before every deploy, run DeployReady — Belsoft's open-source CLI — to catch configuration-level flaws in your next.config.js rewrites and redirects rules, missing security headers, and other OWASP-related misconfigurations. DeployReady establishes the automated baseline in seconds; a Belsoft audit goes further and tests the dynamic behavior of your login and callback flows by hand. Book a call to scope an engagement.

“An open redirect turns your trusted domain into an attacker's delivery mechanism. The fix is not just patching — it is validating every redirect target at the point of use.”

Written by

Marco Silva

Security Engineer, Belsoft Solutions

Ready to partner?

Let's talk about your company.

30 minutes. No pitch. We talk through how you run today and where AI and automation would help.

Book a free audit
logo

Your AI & automation transformation partner: we audit, build, and train your team, then stay embedded as you grow.

Member ofNVIDIA Inception Program

Copyright Ⓒ 2026 BelSoft. All Rights Reserved.

BELSOFT, LDA · NIPC 517893258 · Lisbon, Portugal