OWASP 2013 A10: Unvalidated Redirects and Forwards
A link that starts on your domain and ends on theirs is perfect bait.
Unvalidated Redirects and Forwards is a security vulnerability in which an application sends a user to whatever destination the request names, without checking it. It is number ten, the last item on the 2013 OWASP Top 10. It sits at the bottom because on its own it rarely hands an attacker your database. That placement makes people skip it, which is a mistake, because this is the bug that makes everything a phishing crew wants to do look legitimate. It lends your domain’s good name to their link.
The shape is simple. Your application takes a destination from the request and sends the user there, trusting that whatever arrived is safe. It is not. The attacker supplies their own destination, and now a link that genuinely begins at yourbank.example delivers the victim to a copy of your login page on a server the attacker controls.
What the bug is, in plain words
A redirect tells the browser to go somewhere else. Applications use them constantly and for good reasons: after login, send the user back to the page they wanted; after an action, move them on. To do that, the app often carries the intended destination in a parameter:
https://example.com/login?next=/account/settings
That is fine, because /account/settings is a path on your own site. The bug appears when the application will send the user to any destination the parameter names, including a full address pointing somewhere else:
https://example.com/login?next=https://examp1e-login.com/
The user reads the start of the link, sees your domain, and trusts it. After they log in, or sometimes immediately, your application forwards them to the attacker’s site. Their browser obeyed your server. The attacker never had to forge your domain; you lent it.
A forward is the same idea inside the application. Instead of telling the browser to go elsewhere, the server hands the request to another internal page or handler. If the target is taken from user input, an attacker can forward themselves to an internal function and, in some frameworks, land there having skipped the access checks that sit on the normal route in. The redirect threatens your users; the internal forward can threaten your server.
How the attack works, step by step
The attacker is building a phishing campaign and wants their link to survive a careful look.
They find a redirect on your site that honors a full address. Login and logout flows are the usual homes for it, because both legitimately bounce the user somewhere afterward.
They craft a link whose visible, trusted part is your domain and whose payload is their address, parked in the redirect parameter:
https://example.com/login?returnTo=https://examp1e-secure.com/login
They send it, and it passes the checks that stop cruder phishing. A mail filter that blocks known-bad domains sees example.com and lets it through. A wary user hovers over the link, sees it starts at the real site, and clicks. Your server reads returnTo and issues the redirect. The victim arrives at a pixel-perfect copy of your login and types their password into the attacker’s form.
The same primitive shows up in authentication protocols. OAuth and OpenID, the standards behind “log in with your account elsewhere,” send the user back to a redirect_uri after approval, carrying a code or token. If an authorization server accepts a loosely matched redirect target, or if the registered destination is itself an open redirector, that code or token can be bounced to the attacker. This class of abuse was publicized in May 2014 under the name Covert Redirect, affecting several large identity providers. The protocol’s own threat model (published January 2013) had already warned that redirect targets must be matched exactly, not loosely, for this reason.
Real cases
Covert Redirect, publicized May 2014. Researchers described how the redirect step in OAuth and OpenID flows at several major providers could be abused: an attacker who found an open redirect on a domain the identity provider trusted, or who exploited loose matching of the return address, could have the user’s authorization response delivered to a site they controlled. Even when a victim declined the permission prompt, they could still be bounced to an attacker-chosen page. The providers’ responses amounted to the same fix the standard already recommended: match the return address against a strict allow-list rather than accepting near-misses. The root cause was trusting a redirect target that was validated loosely or not at all.
Apache Struts, 2013 (CVE-2013-2248). The widely used Java web framework Struts 2 supported special parameter prefixes, redirect: and redirectAction:, that let a request change where the response went. In versions up to 2.3.15, an attacker could supply the redirect target in the request and send the user to an arbitrary external site, a clean open redirect delivered straight through the framework. The fix, in 2.3.15.1, removed those prefixes from the default request mapper. The root cause was a convenience feature that took a redirect destination from untrusted input and acted on it without restriction.
Both cases are the same mistake at different layers: one in a protocol’s handling of return addresses, one in a framework feature, each trusting a destination that came from the request.
Why “starts with my domain” fails
The instinct, once you understand the bug, is to check that the destination is one of yours. Developers reach for the quickest version of that check, and nearly every quick version is broken. The fundamental error is validating the destination as text instead of parsing it as a URL.
Vulnerable code (Node.js)
// Looks like it only allows our own site. It does not.
app.get('/login', (req, res) => {
const dest = req.query.next || '/';
if (dest.startsWith('https://example.com')) {
return res.redirect(dest);
}
res.redirect('/');
});
Every one of these slips past startsWith('https://example.com'):
https://example.com.attacker.com/ the real host is attacker.com
https://example.computer-repair.net/ your string is a prefix of a different domain
And these exploit how browsers parse an address rather than how your string check reads it:
//attacker.com/ a protocol-relative URL; the browser treats it as a full address to attacker.com
https:attacker.com some parsers accept this as a host, not a path
https://example.com@attacker.com/ everything before @ is a username; the host is attacker.com
https://attacker.com\@example.com/ a backslash; browsers often treat \ as /, so the host is attacker.com
https://attacker.com%2F@example.com encoded characters that decode after your check runs
/\attacker.com leading slash-backslash that some browsers read as //
A plain startsWith or a naive regular expression will clear several of these. The @ trick is especially cruel because https://example.com@attacker.com genuinely starts with your domain as text, and genuinely goes to the attacker. Text matching cannot win this. You have to parse the destination the way a browser will and then judge the parsed host.
The fix that ends it
Two defenses, in order of preference.
First choice: do not accept a destination at all. If you only ever need to send the user to pages on your own site, take a relative path and nothing else. Reject anything that could name a host.
Fixed code (Node.js), relative paths only:
app.get('/login', (req, res) => {
const next = req.query.next || '/';
// Must start with a single slash and not with // or /\ (which mean "another host").
if (typeof next === 'string' && /^\/[^/\\]/.test(next)) {
return res.redirect(next);
}
return res.redirect('/');
});
The test ^\/[^/\\] means: begins with one slash, and the next character is not a slash or a backslash, so //attacker.com and /\attacker.com are both refused while /account/settings passes.
Second choice, when you genuinely must redirect off-site: an allow-list. Parse the destination, then confirm its host is one you approve. Parsing, not string matching, is the point.
Fixed code (PHP)
<?php
$allowedHosts = ['example.com', 'www.example.com', 'help.example.com'];
$next = isset($_GET['next']) ? $_GET['next'] : '/';
$parts = parse_url($next);
// A relative path has no host: allow it if it starts with a single slash.
if ($parts !== false && !isset($parts['host'])) {
if (isset($next[0]) && $next[0] === '/' && (!isset($next[1]) || ($next[1] !== '/' && $next[1] !== '\\'))) {
header('Location: ' . $next);
exit;
}
}
// An absolute URL: the parsed host must be on the list, exactly.
if ($parts !== false && isset($parts['host']) && in_array(strtolower($parts['host']), $allowedHosts, true)) {
header('Location: ' . $next);
exit;
}
// Anything else: refuse and go home.
header('Location: /');
exit;
Fixed code (Java), the same parse-then-check:
import java.net.URI;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
static final Set<String> ALLOWED =
new HashSet<>(Arrays.asList("example.com", "www.example.com"));
String safeRedirect(String next) {
if (next == null || next.isEmpty()) return "/";
try {
URI u = new URI(next);
if (u.getHost() == null) {
// relative path: must start with a single slash
if (next.startsWith("/") && !next.startsWith("//") && !next.startsWith("/\\")) {
return next;
}
return "/";
}
if (ALLOWED.contains(u.getHost().toLowerCase())) {
return next;
}
} catch (Exception e) {
// fall through
}
return "/";
}
The comparison is on the parsed host and it is an exact match against a fixed list. Not “contains,” not “ends with,” not “starts with.” An attacker cannot register example.com out from under you, and they cannot smuggle it into the host position, because parse_url and URI resolve the @, the backslash and the protocol-relative forms to the real host before your check ever runs.
For OAuth and OpenID, the same rule in protocol terms: register each redirect_uri in full and match it exactly, and never treat your own domain’s open redirect as an acceptable return path.
For internal forwards, do not take the target from the request. Map a small set of fixed internal destinations to names in code, and forward only to those, so a forward can never be aimed at a handler the user should not reach.
Why the softer fixes fall short
An interstitial warning page. “You are leaving our site, continue?” trains users to click through and does nothing about a redirect that lands on a convincing clone before they read the warning. It is friction, not a control.
Blocking known-bad domains. A denylist of phishing hosts is always behind, because the attacker registers a fresh domain for each campaign. Allow-list the few destinations you trust; do not chase the infinite ones you do not.
Signing the redirect parameter. Attaching a signature so only your own links are honored can work, but it is heavier than the real answer and it breaks the moment a legitimate flow needs to build a redirect dynamically. If relative paths cover your needs, they are simpler and have no key to leak.
Test your own application
Find every place your application reads a destination from the request. Search the code for redirect calls whose argument is user input: redirect, sendRedirect, header('Location:, Response.Redirect, and the framework’s forward or render-to calls. Pay special attention to login and logout, password reset, and any “continue” or “return to” parameter.
For each one, send the bypass inputs and watch where the browser actually lands:
?next=https://attacker.example/
?next=//attacker.example/
?next=https://example.com@attacker.example/
?next=https://example.com.attacker.example/
?next=/\attacker.example
?next=https:attacker.example
If any of these sends you off your own domain, the redirect is open. For OAuth flows, try pointing redirect_uri at a host that merely resembles a registered one, and at a known open redirect on your own domain, and see whether the code or token follows.
A note from testing: an open redirect is often the quiet enabler that upgrades another finding. A redirect that leaks the OAuth code turns into account takeover; a redirect in a password reset mail turns into credential theft. When you find one, chase what flows through it, not just the redirect itself.
The point
Unvalidated Redirects and Forwards is last on the list and underrated for it. It rarely breaks your server, so it is easy to wave off, but it is the lever that makes phishing against your own users land, and in an authentication flow it can become full account takeover. The defense is not clever. Prefer relative paths, so there is no host to abuse. When you must go off-site, parse the destination and match its host against a short allow-list, exactly, never as a piece of text. Everything that goes wrong here goes wrong because someone compared strings instead of parsing a URL. Parse it, judge the host, and the attacker loses the one thing they wanted from you, which was your domain’s good name on their link.
Bypass inputs testers should keep on hand
//attacker.example/(protocol-relative, treated as another host)https://example.com@attacker.example/(everything before @ is a username)https://attacker.example\@example.com/(backslash read as a slash by browsers)https://example.com.attacker.example/(your domain is only a subdomain label)https://example.computer.example/(your domain is a text prefix of a different one)/\attacker.exampleand/%2Fattacker.example(slash tricks and late-decoding escapes)https:attacker.example(missing slashes, accepted as a host by some parsers)
Checklist
- Prefer relative paths for redirects; reject anything that can name a host.
- If you must redirect off-site, parse the URL and match the host against a fixed allow-list, exactly.
- Never validate a destination with starts-with, ends-with or contains on the raw string.
- Account for
@,//,\, and encoded characters; let a URL parser resolve them before you judge. - Register OAuth and OpenID redirect targets in full and match them exactly.
- For internal forwards, map fixed destinations to names in code; never forward to a target from the request.
- Test login, logout and reset flows with the bypass inputs, and follow what an open redirect could carry.