Open redirect vulnerability: why returnUrl checks fail
Open redirect checks fail on strings browsers rewrite. Of 21,015 fuzz strings that passed a slash check, 326 left the site; IsLocalUrl let none out.
The string /\evil.example/ starts with a slash, and its second character is not a slash. A
redirect check built on those two rules accepts it, and the browser then leaves your site. We put
this language switcher on a local server, sent that string as returnUrl, and watched headless
Chrome land on a second server playing the attacker:
app.MapGet("/language", (HttpContext http, string lang, string returnUrl) =>
{
http.Response.Cookies.Append("lang", lang);
if (!returnUrl.StartsWith('/') || returnUrl.StartsWith("//"))
returnUrl = "/";
return Results.Redirect(returnUrl);
});That is an open redirect vulnerability, which CWE-601 files under URL redirection to an untrusted site. A link that begins with your real domain passes the glance test and delivers the visitor to a page someone else controls. Microsoft's open redirect guidance walks through the classic version, where a user signs in on the real site and lands on a look-alike that asks for the password again. OAuth raises the cost: RFC 9700 forbids open redirectors on clients and authorization servers because they can leak authorization codes and access tokens. The same endpoint can also walk a server-side fetcher off its allowlist if it follows redirects, one more reason the SSRF guide treats every hop as hostile.
A Location header is text until the browser resolves it against the current page. The
WHATWG URL standard says what happens next. The parser strips
leading and trailing control characters and spaces, deletes every tab and newline wherever they
appear, and treats a backslash as a forward slash in http and https URLs. Your validator saw
none of that.
The check reads a string. The browser parses a URL.
/\evil.example/
Naive check: pass
One leading slash, second character is not a slash
Browser parser
Backslash becomes a slash
Destination
evil.example
/<tab>/evil.example/
Naive check: pass
One leading slash, second character is a tab
Browser parser
Tab is deleted before parsing
Destination
evil.example
https://[email protected]/
Naive check: pass
Starts with the trusted site
Browser parser
shop.example is only the userinfo
Destination
evil.example
Strings every parser agrees on
One leading slash, a second character that is not a slash, no backslash anywhere, and no control character anywhere. Everything else goes to the home page.
Here is what we measured. The four check columns ran on .NET 10, with identical verdicts on Windows
and Linux. The last column comes from real navigations in headless Chrome through Kestrel, with
localhost:5102 in place of evil.example.
returnUrl |
Slash, not // |
!IsAbsoluteUri |
System.Uri host compare |
IsLocalUrl |
Chrome goes to |
|---|---|---|---|---|---|
/orders/42 |
accept | accept | accept | accept | same site |
//evil.example/ |
reject | accept | reject | reject | off-site if accepted |
/\evil.example/ |
accept | accept | accept | reject | off-site if accepted |
/<tab>/evil.example/ |
accept | accept | accept | reject | off-site if accepted |
<tab>//evil.example/ |
reject | accept | reject | reject | off-site if accepted |
/%09/evil.example/ |
accept | accept | accept | accept | same site |
Kestrel adds a wrinkle. It refused to send a Location containing a line feed or any non-ASCII
character and returned a 500, but a tab went straight through. A percent-encoded tab is harmless here,
because %09 stays in the path and no parser treats it as a separator. Prefix checks fail in a
different way: StartsWith("https://shop.example") accepts https://[email protected]/, where
everything before the @ is a username, and https://shop.example.evil.example/, which is a subdomain
of the attacker's domain.
Resolving the input and comparing hosts fails on the same strings:
var resolved = new Uri(new Uri("https://shop.example/"), returnUrl);
var sameHost = resolved.Host == "shop.example";For /\evil.example/ and the tab variant, System.Uri reports shop.example, and Chrome navigates
somewhere else. Asking Uri.TryCreate with UriKind.RelativeOrAbsolute fares no better, since it
calls //evil.example/ relative. Any parser other than the browser's is a second opinion, so the
check has to accept only strings where every parser must agree.
To see how far the disagreement goes, we generated every concatenation of up to four tokens from a
list of 28 (slashes, backslashes, control characters, é, @, %09, host names and schemes). That
made 637,420 strings. Node's WHATWG URL parser stood in for the browser, and an escape means the
resolved URL had a different host or scheme.
| Check | Accepted | Escaped |
|---|---|---|
!IsAbsoluteUri |
564,858 | 20,010 |
System.Uri host compare |
509,654 | 5,347 |
Starts with /, not // |
21,015 | 326 |
RedirectHttpResult.IsLocalUrl |
10,072 | 0 |
IsSafeLocalPath (below) |
8,798 | 0 |
Of 21,015 strings that passed the slash check, 326 resolved off-site. Of 10,072 that passed
IsLocalUrl, none did.
Zero escapes across one token list is evidence, not a proof, and Node is not Chrome. It does show where the naive checks break, and they break often.
In ASP.NET Core, use the framework's rule and the redirect result that enforces it, then encode what Kestrel will not send:
app.MapGet("/language", (HttpContext http, string lang, string? returnUrl) =>
{
http.Response.Cookies.Append("lang", lang);
return RedirectHttpResult.IsLocalUrl(returnUrl)
? Results.LocalRedirect(EncodeNonAscii(returnUrl!))
: Results.LocalRedirect("/");
});
static string EncodeNonAscii(string url) =>
Regex.Replace(url, @"[^\x00-\x7F]+", match => Uri.EscapeDataString(match.Value));Every hostile row in the first table went home in Chrome with this version, and /orders/42 still
worked. Each guard closes a way to trigger a 500 on demand. Results.LocalRedirect throws
InvalidOperationException on a non-local URL, so IsLocalUrl has to run first. And IsLocalUrl
accepts /café, whose non-ASCII Location Kestrel refuses, so an honest link fails unless you encode
it. We sent all 9,991 distinct strings IsLocalUrl accepted from the token list through real Kestrel.
Without the encoding, 2,548 returned 500. With it, all 9,991 returned 302, and /café arrived in Chrome
as /caf%C3%A9.
Outside that framework the rule fits in a few lines, and it translates to any language. Apply the same encoding to whatever it accepts:
static bool IsSafeLocalPath(string url)
{
if (url.Length == 0 || url[0] != '/') return false;
if (url.Length > 1 && url[1] == '/') return false;
foreach (var c in url)
if (char.IsControl(c) || c == '\\') return false;
return true;
}It trims nothing and repairs nothing, because every repair is another guess about a parser. A raw
backslash in an honest query string sends that visitor to the home page, a price worth paying. Run
it on the value your framework has already decoded, then leave that value alone apart from the
encoding: decoding a second time after the check turns %09 back into a real tab.
OWASP's redirect cheat sheet
prefers a server-side map, where the request carries a short ID and your code looks up the URL. No
attacker string reaches the header at all. When the URL must come from the request, compare parsed
components against an allowlist, here a case-insensitive HashSet called PartnerHosts, and send the
canonical form the parser approved:
static string? PartnerRedirect(string input) =>
Uri.TryCreate(input, UriKind.Absolute, out var uri)
&& uri.Scheme == Uri.UriSchemeHttps
&& uri.UserInfo.Length == 0
&& uri.IsDefaultPort
&& PartnerHosts.Contains(uri.Host)
? uri.AbsoluteUri
: null;Returning AbsoluteUri instead of the raw input matters, since it closes the gap between what you
checked and what you send. In the token run, this method accepted 235 strings and Node resolved all
of them to the allowed host. Compare Host rather than IdnHost: on a host containing U+2028,
TryCreate succeeded and IdnHost then threw UriFormatException.
A client-side location.href = next has a worse failure than a phishing page. Chrome executed all
three of javascript:, JaVaScRiPt: and java<tab>script: when we assigned them that way, so a
substring blocklist for javascript: catches only the first. All three fail IsLocalUrl and
IsSafeLocalPath because they do not begin with a slash. For user-supplied links rendered into
href, allow http, https and site-relative paths, then treat everything else as inert.
These inputs are cheap to pin, and they catch the regression when someone "simplifies" the check during a refactor:
[Theory]
[InlineData("//evil.example/")]
[InlineData("/\\evil.example/")]
[InlineData("/\t/evil.example/")]
[InlineData("\t//evil.example/")]
[InlineData(" //evil.example/")]
[InlineData("https://[email protected]/")]
[InlineData("javascript:alert(1)")]
public void Rejects_off_site_targets(string returnUrl) =>
Assert.False(RedirectHttpResult.IsLocalUrl(returnUrl));
[Fact]
public void Encodes_non_ascii_paths_for_the_location_header() =>
Assert.Equal("/caf%C3%A9", EncodeNonAscii("/café"));The same input-picks-a-destination pattern drives CORS misconfiguration
and path traversal. The two Katabench katas attached to this post begin from
the mistakes above: a check that stops at the first slash, then a link filter that bans javascript:
by exact spelling. If you want a free place to start on that family of bugs, the
C# secure coding exercises cover several, and the
Secure Coding track guide says what the grader checks.
Practice Security & Reliability
- Open in the editor: Comment Renderer (HTML Injection)
Secure coding Easy Free, no account needed
Comment Renderer (HTML Injection)
Fix a stored-XSS hole in a comment renderer without breaking its output.
- Open the Pro kata: Open Redirect Validator
Secure coding Medium Katabench Pro
Open Redirect Validator
Allow only same-site redirect targets, blocking the off-site tricks.
More like this: C# secure coding exercises →