OAuth 2.0 security: the common mistakes and what a WAF can see

OAuth 2.0 security is mostly a matter of how an application is wired to its authorization server: which redirect addresses are accepted, how a login response is tied to the browser that began it, and where tokens travel. RFC 6749 defines the protocol, and RFC 9700, the Security Best Current Practice, collects what years of deployments got wrong. OAuth is one way to do API authentication, and the tokens it issues are often JWT tokens. This guide belongs to API security.

Section API security Updated Reference RFC 9700

Quick facts

Defined by
RFC 6749, the OAuth 2.0 framework; bearer tokens in RFC 6750
Security guidance
RFC 9700, OAuth 2.0 Security Best Current Practice
Redirect URIs
The authorization server must compare them by exact string matching (RFC 9700, section 2.1); only the port of a native app’s localhost address may vary
Implicit grant
Clients should not use response_type=token (RFC 9700, section 2.1.2)
PKCE
Public clients must use it, confidential clients are told to (RFC 9700, section 2.1.1); defined in RFC 7636
OWASP guidance
OAuth 2.0 Protocol Cheat Sheet; failures here fall under the authentication failures category of the OWASP Top 10
Can a WAF fix a loose OAuth setup?
No. It sees requests and responses, not your client registration or the logic that issues tokens

Where the browser carries the secrets

In the authorization code flow, the application (the client) sends the user’s browser to the authorization server. The user signs in there. The server then redirects the browser back to the client with a short-lived authorization code in the URL, and the client swaps that code for tokens in a direct request to the token endpoint. The code is meant to be used once; RFC 6749, section 4.1.2 says a code must not be used more than once and recommends a lifetime of at most 10 minutes.

The point to take from this: the first half of the exchange runs through the user’s browser, in URLs that anyone with the right position can copy. The authorization request below uses made-up values and shows which parameters travel there.

An authorization request, shown with example values
GET /authorize?response_type=code
  &client_id=app-1234
  &redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
  &scope=profile
  &state=k3Jx9...random
  &code_challenge=E9Melh...
  &code_challenge_method=S256
Host: auth.example.com

redirect_uri says where the response goes, state ties the response to the session that started it, and code_challenge binds the later code exchange to the client that began the flow. Each of the mistakes below is a way to leave one of those three open.

Common OAuth 2.0 mistakes

  • The redirect URI is not matched exactly. If the authorization server accepts a prefix, a pattern or any path on a registered host, the response may be delivered somewhere the client owner never intended. RFC 9700 requires exact string matching, because loose matching leaks authorization codes and access tokens (section 2.1; the attack is described in section 4.1). A related hole is an open redirect on the client: RFC 9700 says clients and servers must not expose URLs that forward the browser to arbitrary addresses taken from a query parameter, since those can be used to exfiltrate codes and tokens.
  • There is no state, or it is not checked. Without it, the client cannot tell a response it asked for from one pushed at it. That is a CSRF against the callback: the attacker’s own code is delivered to the victim’s session. RFC 6749 section 10.12 describes the threat, and RFC 9700 requires one-time tokens in state bound to the user agent, unless the client relies on PKCE or, in OpenID Connect, on nonce. A state that is constant or guessable gives no protection.
  • The implicit flow is still in use. The implicit grant returns the access token directly in the redirect, in the URL fragment (RFC 6749, section 4.2.2). RFC 9700 section 2.1.2 says such tokens are open to leakage and replay and cannot be sender-constrained, and tells clients not to use that response type. The OWASP cheat sheet marks the grant as deprecated.
  • Tokens end up in URLs. RFC 6750 allows sending an access token as a query parameter but says it should not be, partly because the URL is likely to be logged. RFC 9700 section 4.2 adds the Referer header: URLs holding codes or state values can leak to third-party content on the page that receives the response. Logs, browser history and analytics tools keep URLs for a long time.
  • No PKCE. Proof Key for Code Exchange makes a stolen authorization code useless on its own: the token request must carry a code_verifier whose hash matches the code_challenge from the first request. RFC 7636 sets the verifier at 43 to 128 characters, and a client able to use S256 must use it. RFC 9700 asks the same of every public client and recommends it for confidential ones; it also says the server must refuse a verifier when no challenge was sent, so PKCE cannot be quietly skipped.
  • The password grant. Handing the user’s password to the client defeats the point of delegation. RFC 9700 section 2.4 says the resource owner password credentials grant must not be used.
  • Tokens that are too broad or too long-lived. A bearer token works for whoever holds it (RFC 6750). The OWASP cheat sheet advises restricting each token to its intended resource server, and RFC 9700 favours short-lived access tokens. A token meant for one API should not open another; the audience check for JWT-form tokens is covered on the JWT page.

What a sound setup looks like

These are configuration and code changes, and each belongs to the authorization server or the client. None of them can be added at the edge. Plain API keys are the simpler alternative: API key security.

  1. Register full redirect URIs and compare them as strings. No wildcards, no “anything under this host”.
  2. Use the authorization code flow with PKCE for every client type, browser apps included, with S256.
  3. Generate state per request, bind it to the user’s session and reject a callback that does not carry the stored value.
  4. Send access tokens in the Authorization header, never in the query string, and keep them out of logs.
  5. Keep codes short-lived and single-use, and keep access tokens short-lived with a narrow scope and audience.
  6. Remove open redirectors from the client and the authorization server.

To confirm the result, test the client against each rule above; the approach for checking an API is on API security testing. A leaked or stolen token is a session problem in practice, and session hijacking covers what follows. If a token can be read by page script, cross-site scripting is the usual route to it.

Where a WAF helps and where it does not

A WAF sees HTTP requests. In OAuth traffic that means the parameters on the authorization request, the callback and the token request: redirect_uri, state, response_type, code. It does not see the client registry, so it does not know which redirect addresses are the registered ones. It does not know which state your session stored, whether a code was already used, or what scope a token was granted. Those decisions sit in the authorization server and in your application, and a rule cannot reproduce them.

Some things are out of its sight entirely. The URL fragment that carries an implicit-flow token is not sent to the server when the page is requested (MDN: URI fragment), so a request filter never receives it. A token leaked through a Referer header or a log file is leaked outside the request path that a filter inspects.

Narrow help is possible. A filter that is documented for named attack categories can flag requests to your authorization endpoints that look like injection attempts, since those endpoints are ordinary web inputs. Rate limits fit the token and login endpoints, where repeated guessing and credential stuffing land; a client that goes over the cap gets a 429 response. Bridge WAF is not described here as having OAuth-specific rules, so do not plan around one.

Start in monitor mode. Matches are logged while every request is still served, and the monitor-mode log shows what a rule would have touched before you turn on block. The fixes in the previous section still have to be made in your own code.

Questions

What are the most common OAuth 2.0 security mistakes?

Accepting redirect URIs that are not compared exactly, leaving out or not checking the state parameter, using the implicit flow, sending tokens in URLs and skipping PKCE. RFC 9700, the OAuth 2.0 Security Best Current Practice, addresses each of them.

Why does redirect_uri need an exact match?

The authorization server sends the code or token to the redirect URI. If the server accepts patterns or prefixes, a response can be delivered to an address the client owner did not register. RFC 9700 requires exact string matching, with one exception for the port of a native app on localhost.

Should I still use the implicit flow?

No. RFC 9700 tells clients not to use the implicit grant, because access tokens in the redirect can leak and be replayed and cannot be sender-constrained. Use the authorization code flow with PKCE, which also suits single-page and mobile apps.

What does PKCE protect against?

PKCE stops a stolen or injected authorization code from being redeemed by someone else. The token request must include a code verifier that matches the challenge sent in the first request. RFC 9700 requires it for public clients and recommends it for confidential ones.

Can a WAF stop OAuth attacks?

Only in part. A WAF can log and filter requests and rate-limit login and token endpoints, but it does not know your registered redirect URIs, stored state values or token grants. Fixing OAuth flaws means changing the client and the authorization server.