Share some knowledge, skills and others — security research, pentesting notes and more.

View on GitHub
23 January 2022

OAuth

by allencharp

What is OAuth 2.0

OAuth 2.0 (RFC 6749) is an authorization framework that lets a user grant a third-party application limited access to their resources without sharing credentials. OAuth defines four roles:

Access Token and ID Token

Access Token and Refresh Token

Authorization Code Flow (simplified)

  1. The client redirects the user to the authorization server (/authorize?response_type=code&client_id=...&redirect_uri=...).
  2. The user authenticates and grants consent.
  3. The authorization server redirects back with an authorization code.
  4. The client exchanges the code (plus its own credentials) at the token endpoint for an access token (and optionally a refresh token).

The authorization code never goes through the browser on the final exchange — only the client sees the token.

Common OAuth Vulnerabilities and Protections

1. Missing or weak state (CSRF)

Without a state parameter binding the request to the user’s session, an attacker can force a victim into a session the attacker started (login CSRF).

Fix: always generate a random, unguessable state and verify it on the redirect back.

2. redirect_uri manipulation

If the authorization server does not strictly validate redirect_uri, an attacker can use an open redirector or a client-controlled URI to steal the authorization code.

Fix: register exact redirect URIs per client and match them exactly.

3. Missing PKCE

In the code flow, an attacker who obtains the authorization code (e.g. via a compromised redirect) could exchange it. PKCE (RFC 7636) binds the code to the client with a code_challenge (SHA-256 of a code_verifier) and code_verifier.

Fix: use PKCE (code_challenge / code_verifier) even for confidential clients, to protect the code exchange.

Security notes

Summary

OAuth is a protocol about delegation — the security depends on the details: strict redirect_uri registration, state for CSRF protection, and PKCE to bind the code exchange. Small implementation mistakes turn into token theft and account takeover.

tags: web-security - oauth - authentication