Deutsch
Fix libraryMediumAuth · Redirects

Open redirect after login: your sign-in page sends users anywhere

A login page that follows ?next= or ?redirect= to any URL lets phishers borrow your domain. Accept only paths on your own site, with a tested helper.

By Tim GeithnerPublished 3 min read

ShipRely's Security audit checks for this today.

Short answer

Many apps send users back to where they were after login with a parameter such as ?next=/settings. If the app follows that value without checking it, a link to your real login page can end on a look-alike site after the user signs in. Parse the value as a URL, accept it only if it stays on your own origin, and fall back to a fixed page for everything else.

Ask an AI about this page:ChatGPTClaude

On this page

An open redirect after login happens when your sign-in page reads a target from the URL, such as /login?next=…, and sends the user there without checking it. Someone can then share a link to your real login page that ends, right after a successful sign-in, on a site they control, for example a copy of your app asking the user to “confirm” their password. It is rated medium: no data leaks directly, but your trusted domain becomes the opening act of a phishing attack.

Why it matters

OWASP’s cheat sheet on unvalidated redirects describes the core problem: because the link starts on your real domain, a phishing attempt “may have a more trustworthy appearance”. Users are taught to check the address bar before they type a password, and in this case the address bar is correct. The redirect happens after the check.

AI-built apps get this wrong because the convenient code is also the unsafe code:

Vulnerablesrc/pages/Login.tsx
const [params] = useSearchParams();

async function onSignedIn() {
  // Whatever the link says, we go there.
  window.location.href = params.get("next") ?? "/";
}

A first attempt at a fix often checks next.startsWith("/"). That isn’t enough: //other.example starts with a slash too, and the browser treats it as an address on another host.

The same issue shows up in auth callbacks. Supabase’s own password reset example for server-side apps reads a next parameter in its /auth/confirm route, and it carefully keeps the value only if it points to the same host. Copies of that route that drop the check are a common source of this bug.

How to find it in your app

Search for places where a value from the URL becomes a navigation target:

Search your repository
grep -rnE "searchParams\.get\(\"(next|redirect|redirectTo|returnTo|return_to|callbackUrl)\"\)" src app 2>/dev/null
grep -rnE "location\.(href|assign|replace)|navigate\(|redirect\(" src app 2>/dev/null | grep -iE "next|redirect|return"

For every hit, follow the value from the URL to the navigation call. If nothing in between checks where it points, it’s an open redirect. Then test it on your own app: open https://your-app.example/login?next=https://example.com, sign in with a test account, and see where you land. If you end up on example.com, the redirect is open.

How to fix it

Don’t compare strings. Parse the value with the URL API relative to your own origin, then accept it only if the origin didn’t change. That handles absolute URLs, protocol-relative ones (//host), backslashes and javascript: in one rule, the “parsed URL components” approach the OWASP cheat sheet recommends:

Safesrc/lib/safe-next-path.ts
/** Returns a path on this site to continue to after login, or "/" for anything else. */
export function safeNextPath(next: string | null, origin = window.location.origin): string {
  if (!next) return "/";
  let url: URL;
  try {
    url = new URL(next, origin);
  } catch {
    return "/";
  }
  // Same scheme, host and port only, and no path that a browser reads as another host.
  if (url.origin !== origin || url.pathname.startsWith("//")) return "/";
  return `${url.pathname}${url.search}${url.hash}`;
}

Use it everywhere a target comes from the URL:

Safesrc/pages/Login.tsx
const [params] = useSearchParams();

async function onSignedIn() {
  navigate(safeNextPath(params.get("next")));
}

We ran this helper against the usual tricks: full URLs to other hosts, //other.example, /\other.example, /.//other.example, https://user@other.example, javascript: URLs and a different scheme on your own host all fall back to /, while /projects/42?tab=team#x and a full URL on your own origin pass through as paths.

If your redirect targets are a short known list, a mapping is even simpler: accept ?next=billing and look up the path in an object, as OWASP suggests. For OAuth and magic links, keep redirectTo pointing to your own callback route and apply safeNextPath there as well. The password reset version of this problem is covered in password reset link that redirects to any URL.

Validate the redirect after login

Find every place in the app where a value from the URL (next, redirect, redirectTo, returnTo, callbackUrl) is used to navigate after sign-in, sign-up, OAuth callbacks or email confirmation. Add a helper safeNextPath(next) that parses the value with new URL(next, window.location.origin), returns “/“ if parsing fails, if the origin differs from the app’s origin or if the pathname starts with “//“, and otherwise returns pathname plus search plus hash. Use it at every one of those navigation calls, including server-side callback routes (compare against the request’s origin there).

Do not use string checks like startsWith(“/“) as the only check. Keep the existing behaviour for valid paths on our own site. Afterwards, list every file you changed.

Check that the fix works

Sign in with a test account on your own app using each of these links. Every one must end on your own site:

  • https://your-app.example/login?next=/settings lands on /settings (the normal case still works).
  • https://your-app.example/login?next=https://example.com lands on /.
  • https://your-app.example/login?next=//example.com lands on /.
  • https://your-app.example/login?next=javascript:alert(1) lands on / and nothing runs.

Repeat the second link for your OAuth or magic link callback route if you have one. For more on getting auth redirects right, see the Supabase Auth pitfalls guide.

Frequently asked questions

Doesn’t Supabase’s redirect allow list protect me?

Only for the redirectTo value you pass to Supabase Auth, which must match your Redirect URLs list. A ?next= parameter that your own code reads after login is outside that list; your app has to check it.

Is checking that the value starts with / enough?

No. A value like //other.example also starts with a slash, and browsers read it as a link to another host. Parse the value with the URL API and compare the origin instead of checking strings.

Why is this only rated medium?

It doesn’t expose data by itself. It makes phishing more convincing, because the link the victim checks really points to your domain. Combined with a flow that puts tokens into the redirect URL, it can become much more serious.

Sources

  1. 1.Unvalidated Redirects and Forwards Cheat Sheet — OWASP Cheat Sheet Series
  2. 2.Password-based Auth — Supabase Docs
  3. 3.Redirect URLs — Supabase Docs
  4. 4.CWE-601: URL Redirection to Untrusted Site ('Open Redirect') — MITRE

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt