Course contentsShow
← Software Engineering
Lesson 1024 of 1,93331. Authentication and AuthorizationFree lesson

Common OAuth2/OIDC Vulnerabilities and Mitigations

Preventing redirect URI attacks, token leakage, CSRF, and implementing secure OAuth2 clients.

Common OAuth2/OIDC Vulnerabilities and Mitigations

What You'll Learn

You'll learn how to protect OAuth2 and OIDC flows from redirect attacks, token leakage, CSRF, and other critical vulnerabilities.

The Security Landscape

OAuth2 and OIDC are powerful but complex. Like leaving your house keys with a valet service, delegation introduces new attack surfaces. If any step in the authorization flow is compromised, attackers can steal tokens, hijack sessions, or impersonate users.

Critical Vulnerabilities and Defenses

Redirect URI Attacks

Attackers try to redirect tokens to malicious sites by manipulating the redirect_uri parameter. Mitigation: Register exact redirect URIs with your authorization server—no wildcards. Always validate the redirect URI against your allowlist before issuing tokens.

Token Leakage

Tokens can leak through browser history, referrer headers, or logs. Mitigation: Use Authorization Code flow with PKCE (never Implicit flow). Store tokens securely (not in localStorage for sensitive apps). Set short token lifetimes and rotate refresh tokens.

Cross-Site Request Forgery (CSRF)

An attacker tricks a user into completing an OAuth flow that links the attacker's account. Mitigation: Always use the state parameter—generate a random, unguessable value, store it in your session, and verify it matches when the callback returns.

Insecure Client Implementation

Public clients (mobile/SPA) can't keep secrets. Mitigation: Use PKCE for all public clients. Never embed client secrets in client-side code. Implement proper token validation—verify signatures, issuer, audience, and expiration on every request.

Open Redirectors

Using unvalidated redirects in your application creates phishing opportunities. Mitigation: Validate all redirect destinations. Use allowlists, not blocklists.

Key Takeaway: OAuth2/OIDC security depends on strict redirect URI validation, PKCE for public clients, CSRF protection via state parameters, and treating tokens as highly sensitive credentials that must be validated, protected, and short-lived.