Articles / Security

  • oauth2
  • redirect-uri
  • security
  • go
  • appsec
  • authentication

Why Your OAuth Redirect URI Check Must Compare Exact Strings

The redirect URI check is the one place in an OAuth server where "close enough" is a vulnerability. If you validate it with strings.HasPrefix, a wildcard, or a parsed-and-compared URL, someone will find a string that passes your check and lands the authorisation code on a server they control.

The fix is boring: register full URIs, and compare the incoming one to the registered one byte for byte. The interesting part is seeing why every cleverer approach fails.

What the check is protecting

In the authorisation code flow, the user approves access and your server redirects their browser to the redirect_uri with a code in the query string. Whoever receives that request holds the code. Whoever holds the code (and, for a public client, nothing else) can try to swap it for tokens.

So the redirect URI decides where the credential gets delivered. The attacker does not need to break any cryptography. They craft an authorisation link with their own redirect_uri, get the victim to click it, and the victim's real, logged-in approval does the rest.

What the standards say

RFC 6749 (section 3.1.2.3) lets a server compare against registered URIs and, for some cases, allows partial registration. The later security best current practice, RFC 9700, tightens this: authorisation servers must use exact string matching of redirect URIs. The one carve-out is in RFC 8252 for native apps, where a loopback redirect may vary the port because the app picks a free one at runtime.

That is the spec position. My own opinion: partial matching was a convenience for client developers that cost server developers years of CVEs.

Five ways a loose check gets beaten

Assume you registered https://app.example.com/callback. Each of these strings has fooled a real validator at some point:

  • Prefix match: https://app.example.com.evil.net/callback starts with a perfectly good-looking prefix if you registered only the host.
  • Userinfo trick: https://[email protected]/ has app.example.com as a username and evil.net as the host.
  • Path traversal: https://app.example.com/callback/../../open?next=https://evil.net starts with the registered path but resolves elsewhere.
  • Wildcard subdomains: *.example.com includes every forgotten staging host and every subdomain someone can take over.
  • Extra query parameters: a check that ignores the query lets an attacker add parameters that your callback page happily follows.

Quick detour: why the "safe" ones still hurt

Even a correct path-prefix check on a trusted host is dangerous if that host has an open redirector anywhere. The attacker registers nothing; they point redirect_uri at https://app.example.com/go?url=https://evil.net. Your server validates it, sends the code there, the open redirect bounces the browser onwards, and the code or token travels with it (in the Referer header, or in the fragment for the old implicit flow).

Exact matching kills this too, because /go?url=... was never registered. That is a big reason the rule is "whole string", not "same origin".

The check, in Go

Store the registered URIs per client and compare. No parsing, no normalisation, no url.Parse deciding what is equivalent:

package oauth

import "slices"

type Client struct {
	ID           string
	RedirectURIs []string // full URIs, registered in advance
}

// ValidRedirect reports whether got is exactly one of the registered URIs.
func (c *Client) ValidRedirect(got string) bool {
	return got != "" && slices.Contains(c.RedirectURIs, got)
}

That is the whole thing. If the request has no redirect_uri and the client registered exactly one, you may use it. If the client registered several, require the parameter.

Crucially, when the check fails you must not redirect to the supplied URI with an error. Show an error page instead. Redirecting an error=invalid_request to an unvalidated address turns your error handler into the open redirector.

Don't "help" by normalising

It is tempting to lowercase the host, strip a trailing slash or decode percent escapes before comparing. Resist it. Each normalisation step is a place where your idea of the URL and the browser's idea can differ, and that gap is exactly what attackers look for. https://app.example.com/callback and https://app.example.com/callback/ are different strings, and the client can register both if it really wants both.

The same applies at the token endpoint. RFC 6749 says that if redirect_uri was sent in the authorisation request, the token request must carry an identical value. Store the one you validated with the code and compare again. Otherwise an attacker can start the flow with one URI and finish it with another.

Handling the awkward cases

  • Many environments: register each full URI (dev, staging, production). A list of five exact strings is cheap.
  • Native apps on loopback: per RFC 8252, match scheme, host and path exactly, and ignore only the port for 127.0.0.1 and [::1]. Prefer the literal IP to localhost.
  • Custom schemes: register the full string, for example a reverse-domain scheme, and still compare exactly.
  • Per-request state: put it in the state parameter, not in the redirect URI's query.

A test worth having

Keep the evil strings from the list above in a table test next to the validator. If someone later "improves" the check to be friendlier, the test fails the same afternoon:

func TestValidRedirect(t *testing.T) {
	c := &Client{RedirectURIs: []string{"https://app.example.com/callback"}}
	bad := []string{
		"",
		"https://app.example.com/callback/",
		"https://app.example.com.evil.net/callback",
		"https://[email protected]/",
		"https://app.example.com/callback/../open",
		"https://app.example.com/callback?x=1",
		"HTTPS://APP.EXAMPLE.COM/callback",
	}
	for _, u := range bad {
		if c.ValidRedirect(u) {
			t.Errorf("accepted %q", u)
		}
	}
	if !c.ValidRedirect("https://app.example.com/callback") {
		t.Error("rejected the registered URI")
	}
}

The uncomfortable part is that this is less code than the vulnerable version. The security feature is a missing feature.