---
title: "Can your login link send users to a fake page? Fix open redirects"
description: "An open redirect is a page on your site that takes the destination from the request and redirects to it unchecked, so a link with your name on it lands on a phishing page. The fix: never take the URL from the user, or map it to a list on your server."
url: https://ihsanalapsi.dev/articles/open-redirect-return-url
lang: en
date: 2026-11-08
updated: 2026-11-08
tags: ["App security", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Can your login link send users to a fake page? Fix open redirects

Your customer gets a message with a link that starts with your exact domain: `https://yourshop.example/login?next=https://evil.example`. They open it, see the domain they know, and log in. Then your own site sends them to a page that looks like yours and asks for their password again. This article explains why that happens, and how to close it in the code that does the redirect.

## What is an open redirect?

An open redirect is a page on your site that takes the destination URL from the request itself, such as `?next=`, and sends the user there without checking it.

[OWASP's cheat sheet on unvalidated redirects](https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html) calls them Unvalidated Redirects and Forwards, and says they are possible when a web application accepts untrusted input that could cause it to redirect the request to a URL contained within that input. A login page that sends users back to the page they came from is one example, when that page is written in the link.

## Why does the link fool even careful users?

The link fools users because its first part is your real domain, so they see a name they know and click without reading the end of the URL.

The same cheat sheet puts it this way: by modifying untrusted URL input to a malicious site, an attacker may successfully launch a phishing scam and steal user credentials. Because the server name in the modified link is identical to the original site, the phishing attempt looks more trustworthy. So your site is the one lending the attacker's link its name, and the fix lives in your code.

## What does the vulnerable code look like?

The vulnerable code takes a value from the request and passes it straight to the redirect function. The OWASP Rails example is one line:

```ruby
redirect_to params[:url]
```

After its dangerous examples, the Rails one among them, the cheat sheet says the code above is vulnerable to an attack if no validation or extra method controls are applied. Its safe example, by contrast, declares the URL in the code itself, so an attacker cannot change it.

The same mistake shows up in any language: if a `next`, `returnUrl` or `redirect` value comes from the link and goes as-is into the `Location` header, you have an open redirect.

## How do you close an open redirect?

The safest fix is to never accept the destination URL from the user: they send a short name, and the server turns it into a URL of its own.

This is how the OWASP recommendations line up:

1. Avoid redirects and forwards altogether, if you can.
2. If you use them, do not allow the URL as user input for the destination.
3. Have the user send a short name, ID or token that the server maps to a full target URL. Make sure nobody can cycle through the IDs to find every target.
4. For local return URLs, use a framework helper that rejects non-local destinations, such as `LocalRedirect` or `IsLocalUrl` in ASP.NET Core.
5. If you need external destinations, parse the URL and compare the scheme, host and port against an explicit allowlist, reject userinfo, and do not use raw prefix or suffix matching.
6. You can also send every redirect through a page that tells users they are leaving your site, with the destination shown, and have them click the link to continue.

This is item 3 in Node.js: the link carries a name, and the URL comes from a list in the code.

```javascript
const RETURN_TO = new Map([
  ["dashboard", "/dashboard"],
  ["billing", "/settings/billing"],
]);

const target = RETURN_TO.get(req.query.next) ?? "/dashboard";
res.redirect(target);
```

Any value that is not in the list sends the user to the dashboard, and the attacker's link goes nowhere.

### Why is checking the start of the URL not enough?

Because `startsWith("https://yourshop.example")` also passes `https://yourshop.example.evil.example`, which is the attacker's domain. That is why item 5 says to parse the URL and compare its parts, not the whole string.

## Where do you start now?

Search your code for every place that redirects with a value from the request: `redirect_to params`, `res.redirect(req.query`, and any `Location` header built from the link. Try each one with `?next=https://example.com`: if you land outside your site, replace it with a list of names like the example above.

The login page is the first place to check, and while you are there, look at [your session cookie settings](/articles/session-cookie-flags-check) and [what your page URL leaks to other sites](/articles/referrer-policy-url-leaks). If you want someone to test every redirect and login path on your platform the way an attacker would, [Muhassan](/muhassan) does that.

Where does your login page send users after they sign in?

## FAQ

**Does an open redirect leak data from my site?**

Not by itself. Its danger is that it gives a phishing link your site's name, so users trust it and type their password on the attacker's page.

**Is it enough to check that the URL starts with my domain?**

No. OWASP says not to use raw prefix or suffix matching. Parse the URL and compare the scheme, host and port against an allowlist.

**What if I really need to redirect to other sites?**

Keep the approved destinations in an explicit allowlist and compare after parsing the URL, or send users through a page that tells them they are leaving your site and shows the destination.
