OAuth redirect URI validation: prevent codes from reaching the wrong destination
Learn how OAuth redirect URIs should be registered and matched, why broad patterns and open redirects are risky, and how to troubleshoot mismatch errors safely.
OAuth redirect URI validation: prevent codes from reaching the wrong destination
An OAuth redirect URI (redirect_uri) is the client address where the authorization server returns the browser after the user authenticates and grants access. The server must check that the requested address exactly matches one preregistered for the client. Broad patterns or wildcards can let an attacker receive an authorization code or access token at an address they control.
How should it be validated?
Register the actual callback addresses needed for each environment. For example, register separate development and production callbacks; avoid patterns such as `https://*.example.com/*` that cover many destinations.
Send the registered address unchanged as `redirect_uri` in the authorization request.
The authorization server compares the requested and registered URI as exact strings. The OAuth security BCP requires exact string matching, with a narrow exception for the port of a native app's `localhost` redirect URI.
When exchanging an authorization code at the token endpoint, send the same `redirect_uri` used in the initial request if it was included there. This keeps the code bound to the redirect destination.
The client's callback handler must associate the response with the correct request and validate `state`. A safe redirect URI does not prevent errors in response handling.
Why does exact matching matter?
Suppose an authorization server permits this broad pattern:
https://*.example.com/callbackIf a subdomain is taken over, or matching code behaves more broadly than expected, an attacker-controlled destination may be accepted. The server could then send a user to that destination and expose an authorization code or, in an implicit flow, an access token. Even correctly implemented wildcard syntax can be risky if an allowed subdomain becomes attacker-controlled.
Another risk is an open redirect: the client accepts an arbitrary query value such as ?return_to= and sends the user there after authentication. Keep the OAuth callback at a fixed registered location and do not forward the authorization response to an arbitrary destination.
Localhost exception and URL normalization
A native app's loopback redirect can use a local web server that selects an available port, so the OAuth security BCP makes a limited exception for the localhost port. Do not extend that exception to ordinary domains or production callbacks.
Loosely normalizing URLs before validation can cause distinct addresses to be treated as the same by overlooking differences in case, path, port, or encoding. Register the representation required by the platform or authorization server and test the exact value each environment sends.
| Symptom | What to check |
|---|---|
The server returns a redirect_uri error | Compare the requested value character by character with the registered value. For example, https://client.example/callback and https://client.example/callback/ differ by a trailing slash. Use the intended value consistently; do not widen a wildcard to silence the error. Compare scheme, host, port, path, and encoding. |
| Development works but production fails | Register the production callback explicitly and inspect the value the app actually sends. Do not broaden a development wildcard. |
| The service goes to another URL after the callback | Inspect inputs such as return_to or next; restrict them to allowed internal paths. |
| Several subdomains are needed | Register each URI or use the provider's safe multi-URI configuration. Weigh operational needs against the risk of pattern matching. |
Verify the implementation
With a test client, try an unregistered URI, one with only its path changed, an attacker-controlled domain, and an external return_to value. Confirm authorization is rejected and the server does not automatically redirect to an invalid destination. If a test accepts an arbitrary URI, fix the client registration pattern and server matching policy, then retest. Do not place tokens or authorization codes in test logs.
Summary
A redirect_uri identifies where the OAuth response is delivered, so constrain it to an exact match with a registered address. Remove broad wildcards and redirects to arbitrary destinations, and limit the localhost port exception to native-app loopback redirects. RFC 9700 requires exact string matching for ordinary redirect URI comparisons.
References
[RFC 6749 §3.1.2 — Redirection Endpoint](https://www.rfc-editor.org/rfc/rfc6749.html#section-3.1.2)
[RFC 6749 §4.1.3 — Access Token Request](https://www.rfc-editor.org/rfc/rfc6749.html#section-4.1.3)
[RFC 9700 §2.1 — Protecting Redirect-Based Flows](https://www.rfc-editor.org/rfc/rfc9700.html#section-2.1)
[RFC 9700 §4.1 — Insufficient Redirection URI Validation](https://www.rfc-editor.org/rfc/rfc9700.html#section-4.1)