Every sign-in flow carries at least two redirect targets. One is redirect_uri, which tells the authorization server where to send the code. The other is your own returnTo, next or continue, which tells your app where to send the user once they are signed in. Both are taken from the URL, and both are open-redirect candidates.
On a marketing page an open redirect is a phishing helper. In an OAuth flow it can move an authorization code or an access token to an attacker's server. This post covers why, what the current OAuth security guidance requires, and how to validate a post-login redirect against the parser tricks that defeat naive checks.
Why a redirect bug is worse in a sign-in flow
An open redirect (CWE-601) is any endpoint that forwards the browser to a URL taken from the request. In OAuth the browser is carrying secrets while it bounces between sites: a code in the query string, or with the implicit grant, a token in the URL fragment.
RFC 9700, the OAuth 2.0 Security Best Current Practice published in January 2025, describes the chain. If the authorization server accepts a redirect URI on a loose pattern, an attacker registers or finds a matching URL they control. If the client app has an open redirector at a registered URI, the attacker points redirect_uri at it and adds their own destination; the browser reattaches a fragment token on the second hop and delivers it. The RFC's requirement is short: clients and authorization servers must not expose URLs that forward the browser to arbitrary URIs.
Rule 1: match redirect_uri exactly
RFC 9700 section 2.1 says authorization servers must use exact string matching for redirect URIs, with one exception: the port number for localhost redirect URIs used by native apps. No wildcards, no prefix matching, no "any subdomain of".
Pattern matching fails in practice in ways the RFC lists: a wildcard interpreted as "any character" instead of "any valid domain character", and a stale DNS record under a wildcard subdomain that an attacker takes over. Prefix matching on paths adds its own failure, since a prefix that accepts /callback may also accept a longer path that reaches another handler. If you configure an identity provider, register each callback URL in full. If you run your own authorization server, compare the requested redirect_uri with the registered list using plain string equality, after no normalisation at all.
On the client side, build redirect_uri from configuration, never from the request:
// Wrong: Host is a request header
const redirectUri = `https://${req.headers.host}/auth/callback`;
// Right: one fixed value per environment
const redirectUri = process.env.OAUTH_REDIRECT_URI; // https://app.example.com/auth/callbackRule 2: bind the flow with state and PKCE
The callback must prove it belongs to a flow this browser started. Generate a random state, store it server-side or in a signed cookie, and compare it on the callback; checking that state is present is not a check. Use PKCE with S256 for every client, which RFC 9700 recommends for confidential clients too. And prefer the authorization code flow over the implicit grant, since tokens in fragments are exactly what open redirectors leak.
A useful side effect: keep the post-login destination next to the state on the server, rather than passing returnTo through the identity provider. Then the value you redirect to at the end was validated once, when the flow started, and never travelled in a URL the attacker can edit.
Rule 3: validate returnTo with a parser, then compare
Most returnTo checks are string checks: "starts with /", "contains our domain", "does not start with http". Each one fails against inputs that browsers treat as another host. Here is how the WHATWG URL parser (the one in browsers and in Node's URL) resolves some inputs against https://app.example.com, next to what Python's urlsplit reports:
| Input | Browser resolves to | Python urlsplit netloc |
|---|---|---|
/settings |
app.example.com/settings |
empty |
//evil.com |
evil.com |
evil.com |
/\evil.com |
evil.com |
empty |
/<TAB>/evil.com |
evil.com (tab stripped) |
empty |
https://app.example.com.evil.com/ |
app.example.com.evil.com |
that host |
https://app.example.com@evil.com/ |
evil.com (the rest is userinfo) |
app.example.com@evil.com |
javascript:alert(1) |
a script URL | empty |
The /\evil.com row is the important one. A server-side check written in Python sees a relative path with no host and approves it. The browser treats the backslash as a slash, reads //evil.com, and leaves your site. Validation has to reject what the browser will interpret, not just what your server library parses.
A JavaScript version that rejects those cases:
const APP_ORIGIN = "https://app.example.com";
function safeReturnTo(raw, fallback = "/") {
if (typeof raw !== "string" || raw.length === 0 || raw.length > 2048) return fallback;
if (!raw.startsWith("/")) return fallback; // relative paths only
if (raw.startsWith("//") || raw.includes("\\")) return fallback;
if (/[\u0000-\u001f\u007f]/.test(raw)) return fallback; // tabs, newlines, control chars
let url;
try { url = new URL(raw, APP_ORIGIN); } catch { return fallback; }
if (url.origin !== APP_ORIGIN) return fallback; // parse, then compare origins
return url.pathname + url.search + url.hash;
}
// safeReturnTo("/settings?tab=2") -> "/settings?tab=2"
// safeReturnTo("/\\evil.com") -> "/"And a Python equivalent:
from urllib.parse import urlsplit
def safe_return_to(raw: str | None, fallback: str = "/") -> str:
if not raw or len(raw) > 2048 or not raw.startswith("/"):
return fallback
if raw.startswith("//") or "\\" in raw:
return fallback
if any(ord(c) < 0x20 or ord(c) == 0x7F for c in raw):
return fallback
parts = urlsplit(raw)
if parts.scheme or parts.netloc:
return fallback
return rawBoth accept only same-site paths and send everything else to a fixed page. That is the right default: almost no product needs returnTo to leave its own origin.
When you must redirect to another host
Some flows legitimately end on a sibling app, such as a docs site or a regional domain. Use an allowlist of exact hostnames, compared after parsing:
const ALLOWED_HOSTS = new Set(["app.example.com", "docs.example.com"]);
function safeExternal(raw, fallback = "https://app.example.com/") {
let url;
try { url = new URL(raw); } catch { return fallback; }
if (url.protocol !== "https:") return fallback;
if (!ALLOWED_HOSTS.has(url.hostname)) return fallback;
if (url.username || url.password) return fallback;
return url.href;
}Avoid endsWith("example.com"), which accepts evilexample.com, and includes("example.com"), which accepts example.com.evil.net. Exact hostnames in a set are short, reviewable and hard to get wrong.
Framework helpers worth switching on
Rails apps on load_defaults 7.0 or later raise on a redirect_to to an external host unless the call passes allow_other_host: true; the configuration guide documents the setting, now action_on_open_redirect. If your app predates that default, turn it on and audit every allow_other_host: true. In other stacks, look for a single redirect helper and route every user-supplied destination through it, so there is one function to review instead of thirty res.redirect calls.
A short test list
Add these to the unit tests for whatever function validates destinations: //evil.com, /\evil.com, \\evil.com, /%09/evil.com decoded to a tab, https://evil.com, https://app.example.com@evil.com, https://app.example.com.evil.com, javascript:alert(1), data:text/html,x, and an empty string. Each should produce your fallback. Then test that an ordinary path with a query string survives unchanged.
What Vulkro checks
Vulkro Core follows request values into redirect calls and reports them as open redirects: js-taint-redirect-001, py-taint-redirect-001, java-taint-redirect-001 and go-taint-redirect-001, plus RUBY-REDIRECT-001 for redirect_to and php-header-injection-redirect for PHP Location headers. Each finding carries the attack path from the route to the redirect call.
On the OAuth side, wildcard-redirect-uri reports a redirect_uri value that contains * or a regex wildcard, oauth-callback-state-unverified reports a callback that checks state for presence only, oauth-authorize-no-pkce reports an authorization request without PKCE, and oauth-implicit-flow reports token-in-fragment flows.
vulkro scan . # findings with attack paths
vulkro explain py-taint-redirect-001FAQ
Can redirect_uri use wildcards?
Not under current guidance. RFC 9700 requires authorization servers to use exact string matching for redirect URIs, allowing only a variable port for localhost redirects in native apps. Register every callback URL in full.
Is checking that returnTo starts with a slash enough?
No. //evil.com and /\evil.com both start with a slash and both send the browser to another host. Reject protocol-relative paths, backslashes and control characters, then parse the value and compare the resulting origin with your own.
Does PKCE stop open redirect attacks?
PKCE stops a stolen authorization code from being exchanged without the verifier, which removes much of the value of leaking a code. It does not protect tokens delivered in a fragment, and it does not make an open redirector safe for phishing. Use both PKCE and strict redirect validation.
See the API security use case and attack paths, or start a 14-day trial. Related: the sign-in that never checks the signature and open redirects in Visualforce and Experience Cloud.