Open redirect
| Class | Access control |
|---|---|
| Also known as | Unvalidated redirect, open redirection, open redirector |
| CWE | CWE-601: URL Redirection to Untrusted Site |
| OWASP Top 10 | A01:2025 Broken Access Control |
| Typical severity | Medium |
| Affects | Sign-in pages with a return address in the link, click-tracking and "you are leaving this site" endpoints, and scripts that set location from the URL |
A visitor who opens a members-only page without a session is often sent to example.com/login?returnUrl=/account, and after signing in the site forwards them to /account. When the code forwards to whatever the parameter holds, the link example.com/login?returnUrl=https://example-login.net starts on the real site and ends on another one.1 A page that behaves this way is an open redirect: an address on a trusted domain that sends the browser on to any address named in the request.2
The redirect itself is a short response: a status such as 302 or 303 and a Location header holding the next address, which the browser loads without asking the user.32 The site's data stays untouched. The attacker borrows only its domain for the start of the link.
Where it comes from
Redirects of this kind are written on purpose. Sign-in flows send users back to the page they asked for, marketing mail routes clicks through the sender's domain to count them, and some sites show a notice before handing the visitor to an external page.24 Each needs a destination, and the shortest code takes it from the request:
// PHP
header("Location: " . $_GET["url"]);
// Rails
redirect_to params[:url]
// Express
res.redirect(req.query.next);Cookies and hidden form fields are a common source as well, because developers assume users cannot edit them.5 In single-page apps the redirect may happen in the browser instead: a script reads part of the URL and assigns it to location, location.href or window.open(), and the server never sees the value.6
Uses in attacks
Phishing
A link that begins with the real domain, served over HTTPS with a valid certificate, passes the checks users are taught to make, and many do not notice the redirect that follows.6 In August 2021 Microsoft reported a large Phishing campaign built on open redirects at legitimate organisations. Recipients who hovered over the button saw a domain they knew; the attacker's address sat in a parameter. The chain passed through a CAPTCHA page meant to stop automated scanners, then showed a fake sign-in page for a service such as Office 365 with the recipient's address filled in. After the first password the page claimed a timeout or a wrong password and asked again. Microsoft counted at least 350 phishing domains in the campaign and noted that the owners of the abused redirects were possibly unaware of it.4
On a sign-in page the trick can be quieter. The user signs in on the real site, lands on a look-alike domain that shows the login form again, and assumes the first attempt failed.1
Stealing OAuth tokens
In OAuth, the identity provider sends the user back to a registered redirect address with a code or a token attached. If the server that hosts that address also has an open redirect, the code or token can travel on to the attacker. Browsers make this easier for tokens placed after #: when a redirect's Location has no fragment, the browser copies the old one onto the new address, so #access_token=... arrives at the attacker's page. RFC 9700 forbids both clients and authorization servers from exposing open redirectors, and requires providers to compare redirect addresses by exact string match, with one exception for port numbers on localhost addresses of native apps.2
Script execution
When the redirect happens in a script and the attacker controls the start of the value, a javascript: address runs code in the page instead of leaving it, which turns the flaw into Cross-site scripting.6
Why string checks fail
A common fix tests the text: does it start with /, or contain example.com? Browsers parse the value with the URL Standard, which reads it differently. A value starting with // is a scheme-relative address, and //evil.example on an HTTPS page means https://evil.example. For http and https the parser treats a backslash as a slash, so /\evil.example leads to the same place.7 A check for the domain name anywhere in the string is beaten by https://example.com@evil.example, where the first part is user information and the host is evil.example, or by example.com.evil.example.3 One documented case, CVE-2020-11053 in a Go OAuth proxy, let HTML-encoded whitespace slip a foreign address past the validation.5
Safe redirects
The strongest form keeps the address out of the request. The link carries a short name, and the server maps it to a path written in the code:3
// Express
const NEXT = new Map([["account", "/account"], ["billing", "/settings/billing"]]);
res.redirect(NEXT.get(req.query.next) ?? "/account");Where any page on the site must be allowed as a return address, parse the value with a URL parser against the site's own origin, compare the parsed origin, and redirect to the parsed path rather than the raw text.3 The parser resolves //, backslashes and user information the way the browser will:7
// Node.js: keep only paths on this site
const ORIGIN = "https://example.com";
function safeNext(value) {
try {
const u = new URL(value ?? "/", ORIGIN);
return u.origin === ORIGIN ? u.pathname + u.search : "/";
} catch {
return "/";
}
}
res.redirect(safeNext(req.query.next));ASP.NET Core has LocalRedirect, which throws an exception for a non-local address, and Url.IsLocalUrl to test one first.1 In Supabase, the redirectTo value of a sign-in call should match the Redirect URLs list on the URL Configuration page. That list accepts wildcards for preview deployments; for production, Supabase advises the exact address.8 External destinations, where a site needs them, belong in an allowlist compared against the parsed scheme, host and port, never as a prefix or suffix match.3
Prompt for a coding agent
Find every redirect in this project whose target comes from the request or the URL: query parameters such as next, url, redirect, returnUrl or returnTo, form fields, cookies, headers, and client code that assigns to location, location.href or window.open. For each one, either map a short name to a fixed path, or parse the value with new URL(value, ownOrigin), reject it unless the parsed origin equals ownOrigin, and redirect to the parsed pathname and search. Never redirect to the raw string. Check that values starting with //, /\ or javascript: and values containing @ end on the default page. List each file and line you changed.
In SecFlash
SF-SEC-606 reports a live page that forwards to any address given in its link, and SF-SEC-742 reports code that sends the visitor to an address taken from the request. Both show on the Security page; Static application security testing describes the code scan.
See also
- Phishing
- OAuth 2.0
- Cross-site scripting
- Look-alike domains
- Input validation
- Server-side request forgery
- Category: Access control
References
- a b c "Prevent open redirect attacks in ASP.NET Core". Microsoft Learn.
- a b c d Lodderstedt, T. and others (January 2025). "RFC 9700: Best Current Practice for OAuth 2.0 Security". RFC Editor. IETF, BCP 240.
- a b c d e "Unvalidated Redirects and Forwards Cheat Sheet". OWASP Cheat Sheet Series.
- a b Microsoft 365 Defender Threat Intelligence Team (26 August 2021). "Widespread credential phishing campaign abuses open redirector links". Microsoft Security Blog.
- a b "CWE-601: URL Redirection to Untrusted Site". Common Weakness Enumeration. MITRE. Version 5.0.
- a b c "DOM-based open redirection". Web Security Academy. PortSwigger.
- a b "URL Standard". WHATWG. Living standard.
- back "Redirect URLs". Supabase Docs.
External links
- Unvalidated Redirects and Forwards Cheat Sheet, OWASP Cheat Sheet Series