How-To Guide

How to Prevent Open Redirects in OAuth Flows

Secure the OAuth redirect_uri and post-login return: register exact URIs, validate the return target, and stop token theft via open redirects.

On this page

  1. Introduction
  2. Register and match exact redirect URIs
  3. Validate the post-login return safely
  4. Keep credentials out of attacker redirects
  5. Test redirect abuse variants
  6. Common pitfalls
  7. Conclusion

Introduction

OAuth flows move users between an authorization server and a client, and every hop is a redirect. That makes the redirect target a security-critical value: if an attacker can influence where the authorization code or token is sent, they can capture it and take over the account. The redirect_uri and the post-login return are the two places where this matters, and both are frequently implemented with a loose check that an attacker can satisfy.

This tutorial covers registering redirect URIs exactly, validating the post-login return safely, keeping tokens out of attacker-controlled redirects, and testing the variants that pass a loose check. The examples reference an OAuth 2.0 flow, but the validation reasoning applies to any flow that redirects the user with a credential.

Register and match exact redirect URIs

The client registers its redirect URIs with the authorization server, and the server must match the requested redirect_uri against the registered set exactly. That means the same scheme, host, port, path, and query, not a prefix and not a wildcard. A prefix match is the classic bug: an attacker registers or controls a host whose name begins with the trusted host, or appends a path, and the loose check admits it while the browser resolves to the attacker’s server.

Reject any request whose redirect_uri does not exactly equal a registered value, and reject one that is absent unless a single registered value makes a default unambiguous. Do not allow a fragment in the redirect_uri, because fragments are not sent to the server and their handling varies, giving an attacker room to manipulate where the browser ultimately lands. Log the mismatch so a misconfiguration is visible.

function redirectAllowed(requested, registered) {
  // exact match on the full URI; no prefix, no wildcard, no fragment
  let url;
  try {
    url = new URL(requested);
  } catch {
    return false;
  }
  if (url.hash) return false;
  return registered.some((r) => r === requested);
}

Validate the post-login return safely

After the flow completes, the application often redirects the user to where they were headed. If that target is taken from a query parameter, an attacker can send a victim to a look-alike site, or leak a token if the return is on the token-bearing URL. Accept only a relative path that stays on your origin, or an opaque key that the server maps to a fixed destination.

Parse the value and allow it only if it is a relative path with no host and no scheme. Reject absolute URLs, protocol-relative URLs that start with two slashes, and any value whose parsed origin differs from yours. Prefer mapping an identifier to a server-side destination over accepting a URL at all, since a key cannot express an external host.

function safeReturn(next: string | undefined): string {
  if (!next) return '/';
  if (next.startsWith('//') || next.includes('://')) return '/';   // no absolute or protocol-relative
  const url = new URL(next, 'https://app.example.com');
  if (url.origin !== 'https://app.example.com') return '/';
  return url.pathname + url.search;   // relative path only
}

Keep credentials out of attacker redirects

The authorization code is a credential: whoever presents it to the token endpoint with the client secret gets an access token. It must only ever travel to a registered redirect URI. Validate the redirect_uri at both the authorization request and the token exchange, because a malicious redirect that passed a loose request check could still be used to deliver the code elsewhere. The token endpoint must confirm the redirect_uri matches the one used in the authorization request.

Where the flow puts a token in a URL, prefer not to; use the authorization code flow with a PKCE exchange and keep tokens in the response body to the registered client, never in a redirect the user’s browser follows. When a token must appear in a URL fragment, ensure the destination is one you control, and never forward the fragment to an external site. The less a credential lives in a URL, the fewer redirects can leak it.

Test redirect abuse variants

Test the redirect_uri with variants an attacker would try: a registered host with a different port, the host as a subdomain of the attacker’s domain, the registered URI with a trailing path, a URI with an appended query, and one with a fragment. Assert each is rejected because it does not exactly equal a registered value. Test the return parameter with an absolute external URL, a protocol-relative URL, and a value with a scheme, and assert each falls back to a safe default.

for RU in \
  "https://app.example.com.evil.test/cb" \
  "https://app.example.com:8443/cb" \
  "https://app.example.com/cb/extra" \
  "https://app.example.com/cb#x" ; do
  curl -s -o /dev/null -w "%{http_code} $RU\n" "http://localhost:3000/authorize?redirect_uri=$RU"
done
# every variant must be rejected: not an exact registered URI

Common pitfalls

Prefix or wildcard matching of redirect_uri, accepting a URI with a fragment, returning a token in a redirect to an unvalidated target, validating the return parameter with a substring check instead of a full parse, and not repeating the redirect_uri validation at the token exchange are the recurring mistakes. Exact matching, fragment rejection, a relative-path-only return, and validation at both steps address them.

Conclusion

OAuth security depends on where credentials are sent. Register redirect URIs exactly and match them without prefix or wildcard, reject fragments, accept only a relative path or a mapped key for the post-login return, keep tokens out of redirects to unvalidated targets, and validate the redirect_uri again at the token exchange. Self-check by trying prefix, port, and fragment variants and asserting each is rejected. Then a crafted redirect cannot capture a code or token.

Sources: RFC 6749: OAuth 2.0 — redirect URI registration, OAuth 2.0 Security Best Current Practice, OWASP: Unvalidated redirects cheat sheet.

FAQ

Why is prefix matching on redirect_uri dangerous?
An attacker registers https://app.example.com.evil.com or appends to a trusted prefix so the URI passes a startsWith check but resolves to a server the attacker controls. Match registered URIs exactly, including scheme, host, path, and no trailing wildcards.
How do I safely return a user after login?
Accept only a relative path, or map an opaque key to a server-side destination. Reject absolute URLs and anything with a scheme, so a crafted return value cannot send the user, or a token, to another origin.

Related Examples