Validate OAuth Redirect URIs With Exact Matches and PKCE

OAuth redirect URI validation is security-critical because the authorization response can contain a code that is exchanged for tokens. If an authorization server accepts an attacker-controlled destination, a legitimate user may be sent to a site that captures the response. The risk is not limited to a visibly strange URL. Matching by prefix, wildcard, or substring can make an apparently trusted hostname accept a different authority or an open redirect path.

RFC 9700, the OAuth 2.0 Security Best Current Practice, recommends exact string matching between a requested redirect URI and a registered URI, with a limited localhost port exception for native applications. It also says clients and authorization servers must not expose open redirectors that forward users to arbitrary query-supplied destinations. This is a useful implementation rule because every normalization exception creates a parsing and policy question.

Register complete callback URLs

Store each approved callback as a complete URI for the client. Compare the requested value to the registered value using the rules required by the protocol and platform; do not accept https://app.example.com/callback.evil, https://app.example.com.attacker.invalid/callback, or a different scheme merely because the string begins with a trusted prefix. Keep development and production callbacks separate, and do not enable localhost destinations in a production client unless the native-app flow explicitly requires them.

Use HTTPS for web callbacks. RFC 9700 allows loopback HTTP redirection for native applications under the applicable rules, but that exception does not make arbitrary HTTP callbacks safe. Avoid wildcard subdomains unless there is a carefully justified model that proves ownership and exact routing. A hostname controlled by an abandoned tenant or user-generated subdomain can undermine a broad wildcard.

If a product needs to return a user to an internal page after login, store a short relative path in server-side state or validate it against a fixed set of safe application routes. Do not take an arbitrary returnTo or next URL and redirect to it after the OAuth callback. A callback allowlist does not automatically make a later post-login redirect safe.

Bind the authorization transaction

Use Authorization Code flow with PKCE for public clients and modern deployments. Generate a high-entropy verifier for each authorization request, derive its challenge using the supported method, and keep the verifier bound to that browser transaction. The token endpoint checks the verifier when the code is exchanged. PKCE reduces the value of an intercepted authorization code, but it does not fix incorrect redirect URI matching or an open redirector.

Generate and validate the state parameter to bind the callback to the initiating browser session and protect against cross-site request forgery. The value should be unpredictable, one-time, and compared against the transaction stored for that client session. OpenID Connect flows also use a nonce bound to the authentication request and verify that it appears correctly in the ID token. These values serve related but distinct protocol purposes; do not reuse one across users or requests.

For clients that interact with multiple authorization servers, validate issuer information to prevent mix-up attacks. Follow the current specification and library guidance for the particular OAuth or OpenID Connect profile. Avoid implementing protocol parsing and signature validation from scratch when a maintained client library exists.

Avoid URL parsing and wildcard traps

Do not “fix” callback variants by extracting only the origin and comparing it loosely. The allowed callback usually includes a path that selects a particular application handler. An open redirect on an approved host can turn that host into a stepping stone to an attacker destination. Review redirects, deep-link handlers, reverse-proxy rewrites, and client-side routes that may forward a callback onward.

Normalize registration and request values consistently, but preserve exact matching semantics. Be cautious with percent-encoding, trailing slashes, default ports, Unicode hostnames, and fragments. Reject malformed URIs rather than trying several parsers and accepting whichever interpretation looks safe. The authorization server should not silently repair a callback into an allowed one; it should reject the request and produce a useful, non-sensitive error.

Build a callback test matrix

In a test authorization server or local integration environment, register one callback such as https://app.example.test/oauth/callback. Verify that the exact value succeeds and the following fail:

Then verify the full transaction. Reusing a state value, swapping the browser session, using a code with the wrong PKCE verifier, replaying a code, or altering the OIDC nonce should fail. Check that tokens do not appear in browser history, application logs, referrer headers, or analytics. Use synthetic client credentials and accounts only.

Test denied requests at both the authorization endpoint and callback. A denied OAuth request should not redirect the user to the untrusted URI as a convenience error page. Return a safe error on the authorization server's own origin, and avoid reflecting a raw untrusted URL into HTML where it could create an injection issue.

Operational safeguards

Review registered callbacks when applications are renamed, domains are retired, or vendors change. Remove obsolete callbacks and clients, rotate credentials when exposure is suspected, and monitor unusual redirect validation failures. Separate client registrations by environment and grant the narrowest scopes needed. For a browser-based client, store tokens and protect refresh behavior according to the current browser-app guidance; avoid treating a client secret embedded in downloadable JavaScript as confidential.

Use an established identity provider's application settings for callbacks, but still review the configuration and test it. Provider defaults differ, and a setting named “wildcard” may have its own semantics. Record who owns each registered URI and require an approval path for new callbacks.

Review checklist

  1. Register full callback URIs and use exact matching for web clients.
  2. Keep development and production redirect registrations separate.
  3. Remove open redirects from callback and post-login flows.
  4. Use PKCE and transaction-bound state; validate OIDC nonce and issuer where applicable.
  5. Test lookalike hosts, altered paths, schemes, ports, and encodings.
  6. Remove obsolete callbacks and monitor denied redirect attempts.

RFC 9700 documents redirect validation and OAuth security practices. RFC 7636 specifies PKCE. OpenID Connect Core defines the ID token and nonce behavior for OpenID Connect. Implement these controls with a maintained protocol library and verify the deployment's actual client registration.

References


Editorial status: Owner authorized publication without an individual reviewer. Test only against an authorization server and clients you control.