01 //Introduction
OAuth 2.0 is the de facto standard for authorization on the web, powering "Login with Google/Facebook/GitHub" features across millions of applications. However, its complexity and flexibility make it a common source of security vulnerabilities. A single misconfiguration can lead to complete account takeover.
OAuth security issues consistently appear in bug bounty programs, with payouts ranging from hundreds to tens of thousands of dollars. Understanding OAuth vulnerabilities is essential for any security-focused code reviewer.
What is the primary purpose of OAuth 2.0?
02 //OAuth 2.0 Fundamentals
Before diving into vulnerabilities, understanding the OAuth flow is essential. The Authorization Code flow with PKCE is the recommended approach for most applications.
OAuth 2.0 Grant Types
| Grant Type | Use Case | Security Level |
|---|---|---|
| Authorization Code + PKCE | Web apps, SPAs, mobile apps | Highest |
| Authorization Code | Server-side web apps only | High (with client_secret) |
| Client Credentials | Machine-to-machine | High (no user context) |
| Refresh Token | Long-lived access | Medium (requires secure storage) |
| Implicit (deprecated) | Legacy SPAs | LOW - Do not use! |
| Password Grant (deprecated) | Legacy/trusted apps | LOW - Do not use! |
1GET /authorize?
2 response_type=code # Request authorization code
3 &client_id=CLIENT_ID # Your application's ID
4 &redirect_uri=https://app.com/callback # Where to send the code
5 &scope=openid profile email # Requested permissions
6 &state=RANDOM_STATE_VALUE # CSRF protection (REQUIRED!)
7 &code_challenge=CHALLENGE # PKCE challenge (REQUIRED for public clients)
8 &code_challenge_method=S256 # PKCE method
9
10# Security-critical parameters:
11# - state: Prevents CSRF attacks
12# - redirect_uri: Must be strictly validated
13# - code_challenge: Prevents code interception
14# - scope: Should be minimal (least privilege)Why is the Implicit flow now considered insecure?
03 //CSRF & State Parameter
The state parameter is the primary defense against Cross-Site Request Forgery (CSRF) attacks in OAuth. Without it, an attacker can trick a victim into completing an OAuth flow with an authorization code controlled by the attacker.
1# ATTACK: OAuth CSRF (Login CSRF / Account Linking Attack)
2
31. Attacker starts OAuth flow on legitimate site
4 GET /authorize?client_id=APP&redirect_uri=https://app.com/callback
5
62. Attacker authenticates with THEIR account
7 Authorization server issues code for ATTACKER's account
8
93. Attacker gets callback URL but DOESN'T complete it:
10 https://app.com/callback?code=ATTACKERS_CODE
11
124. Attacker tricks victim into visiting this URL
13 (via email, chat, or CSRF in another page)
14
155. Victim's browser completes OAuth flow with ATTACKER's code
16
176. RESULT: Victim's local account is now linked to ATTACKER's
18 social account, or attacker gains access to victim's session
19
20# Without state parameter, the app cannot distinguish this attack
21# from a legitimate OAuth callback!1// VULNERABLE: No state parameter!
2app.get('/login/google', (req, res) => {
3 const authUrl = `https://accounts.google.com/o/oauth2/auth?
4 client_id=${CLIENT_ID}
5 &redirect_uri=${REDIRECT_URI}
6 &response_type=code
7 &scope=openid email profile`;
8 // Missing: &state=RANDOM_VALUE
9 res.redirect(authUrl);
10});
11
12// VULNERABLE: No state validation!
13app.get('/callback', async (req, res) => {
14 const { code } = req.query;
15 // Missing: Validate state matches session
16
17 const tokens = await exchangeCodeForTokens(code);
18 // Attacker's code could be used here!
19
20 await linkAccountToUser(req.session.userId, tokens);
21});1import crypto from 'crypto';
2
3// Generate cryptographically secure state
4function generateState() {
5 return crypto.randomBytes(32).toString('hex');
6}
7
8app.get('/login/google', (req, res) => {
9 // Generate and store state in session
10 const state = generateState();
11 req.session.oauthState = state;
12
13 const authUrl = new URL('https://accounts.google.com/o/oauth2/auth');
14 authUrl.searchParams.set('client_id', CLIENT_ID);
15 authUrl.searchParams.set('redirect_uri', REDIRECT_URI);
16 authUrl.searchParams.set('response_type', 'code');
17 authUrl.searchParams.set('scope', 'openid email profile');
18 authUrl.searchParams.set('state', state); // Include state!
19
20 res.redirect(authUrl.toString());
21});
22
23app.get('/callback', async (req, res) => {
24 const { code, state } = req.query;
25
26 // CRITICAL: Validate state before using code!
27 if (!state || state !== req.session.oauthState) {
28 // Log the attempt for security monitoring
29 console.warn('OAuth state mismatch - possible CSRF attack');
30 return res.status(403).json({ error: 'Invalid state parameter' });
31 }
32
33 // Clear state to prevent replay
34 delete req.session.oauthState;
35
36 // Now safe to exchange code
37 const tokens = await exchangeCodeForTokens(code);
38 // ... rest of authentication flow
39});What should happen if the state parameter doesn't match?
04 //Redirect URI Attacks
Redirect URI manipulation is one of the most impactful OAuth vulnerabilities. If an attacker can control where the authorization code or token is sent, they can steal credentials and take over accounts.
1# ATTACK 1: Open Redirect via path traversal
2Registered: https://app.com/callback
3Attack: https://app.com/callback/../../../attacker.com
4 https://app.com/callback/..%2F..%2Fattacker.com
5
6# ATTACK 2: Subdomain takeover
7Registered: https://*.app.com/callback
8Attack: https://attacker.app.com/callback
9(If attacker can claim unclaimed subdomain)
10
11# ATTACK 3: Fragment theft (Implicit flow)
12Registered: https://app.com/callback
13Attack: https://app.com/callback#access_token=STOLEN
14(Token in fragment is exposed to JavaScript on page)
15
16# ATTACK 4: Parameter pollution
17Registered: https://app.com/callback
18Attack: https://app.com/callback?redirect=https://evil.com
19(If app has secondary redirect after callback)
20
21# ATTACK 5: Localhost bypass (mobile apps)
22Registered: http://localhost:8080/callback
23Attack: Malicious app registers same localhost port first1// VULNERABLE: Substring matching
2function validateRedirectUri(uri, registeredUri) {
3 // Attacker: https://app.com.evil.com/callback
4 return uri.includes(registeredUri); // Returns true!
5}
6
7// VULNERABLE: Regex without anchors
8function validateRedirectUri(uri, pattern) {
9 // Pattern: /app\.com/
10 // Attacker: https://evil.com/app.com/steal
11 return pattern.test(uri); // Returns true!
12}
13
14// VULNERABLE: Protocol downgrade
15function validateRedirectUri(uri, registeredUri) {
16 const parsed = new URL(uri);
17 const registered = new URL(registeredUri);
18 // Only checks host, not protocol
19 return parsed.host === registered.host;
20 // Attacker: http://app.com/callback (MITM possible)
21}
22
23// VULNERABLE: Ignoring path
24function validateRedirectUri(uri, registeredUri) {
25 const parsed = new URL(uri);
26 const registered = new URL(registeredUri);
27 return parsed.origin === registered.origin;
28 // Attacker: https://app.com/open-redirect?url=evil.com
29}1// SECURE: Exact string match (strictest)
2function validateRedirectUri(uri, allowedUris) {
3 return allowedUris.includes(uri);
4}
5
6// Usage at OAuth provider
7const ALLOWED_REDIRECT_URIS = [
8 'https://app.com/callback',
9 'https://app.com/auth/callback',
10 // No wildcards, no patterns
11];
12
13function handleAuthorize(req, res) {
14 const { redirect_uri, client_id } = req.query;
15
16 // Get registered URIs for this client
17 const client = await getClient(client_id);
18
19 // STRICT: Exact match only
20 if (!client.redirect_uris.includes(redirect_uri)) {
21 return res.status(400).json({
22 error: 'invalid_redirect_uri',
23 error_description: 'Redirect URI not registered'
24 });
25 }
26
27 // Also validate:
28 // - Must be HTTPS (except localhost for dev)
29 // - No fragments allowed
30 // - No open redirect parameters
31
32 const parsed = new URL(redirect_uri);
33 if (parsed.protocol !== 'https:' && parsed.hostname !== 'localhost') {
34 return res.status(400).json({ error: 'HTTPS required' });
35 }
36
37 if (parsed.hash) {
38 return res.status(400).json({ error: 'Fragments not allowed' });
39 }
40
41 // Proceed with authorization...
42}What is the safest way to validate redirect_uri?
05 //PKCE Implementation
PKCE (Proof Key for Code Exchange, pronounced "pixy") prevents authorization code interception attacks. It binds the authorization request to the token request, ensuring only the original client can exchange the code.
1# PKCE adds two parameters to OAuth flow:
2
31. CODE_VERIFIER: A random string (43-128 chars)
4 - Generated client-side
5 - Stored securely until token exchange
6 - Example: "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
7
82. CODE_CHALLENGE: Derived from verifier
9 - Method: S256 (recommended) or plain
10 - S256: BASE64URL(SHA256(code_verifier))
11 - Sent in authorization request
12
13# Why this works:
14- Attacker intercepts authorization code
15- Attacker cannot exchange code without code_verifier
16- code_verifier never sent over network until token exchange
17- Token endpoint validates: SHA256(verifier) == challenge1import crypto from 'crypto';
2
3// Generate code verifier (43-128 characters)
4function generateCodeVerifier() {
5 return crypto.randomBytes(32)
6 .toString('base64url'); // URL-safe base64
7}
8
9// Generate code challenge from verifier
10function generateCodeChallenge(verifier) {
11 return crypto.createHash('sha256')
12 .update(verifier)
13 .digest('base64url'); // URL-safe base64
14}
15
16// Authorization request with PKCE
17app.get('/login', (req, res) => {
18 // Generate PKCE values
19 const codeVerifier = generateCodeVerifier();
20 const codeChallenge = generateCodeChallenge(codeVerifier);
21 const state = crypto.randomBytes(16).toString('hex');
22
23 // Store verifier and state securely
24 req.session.pkceVerifier = codeVerifier;
25 req.session.oauthState = state;
26
27 const authUrl = new URL('https://auth.example.com/authorize');
28 authUrl.searchParams.set('client_id', CLIENT_ID);
29 authUrl.searchParams.set('redirect_uri', REDIRECT_URI);
30 authUrl.searchParams.set('response_type', 'code');
31 authUrl.searchParams.set('scope', 'openid profile');
32 authUrl.searchParams.set('state', state);
33 authUrl.searchParams.set('code_challenge', codeChallenge);
34 authUrl.searchParams.set('code_challenge_method', 'S256');
35
36 res.redirect(authUrl.toString());
37});
38
39// Token exchange with PKCE
40app.get('/callback', async (req, res) => {
41 const { code, state } = req.query;
42
43 // Validate state
44 if (state !== req.session.oauthState) {
45 return res.status(403).json({ error: 'Invalid state' });
46 }
47
48 // Exchange code with verifier
49 const tokenResponse = await fetch('https://auth.example.com/token', {
50 method: 'POST',
51 headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
52 body: new URLSearchParams({
53 grant_type: 'authorization_code',
54 code: code,
55 redirect_uri: REDIRECT_URI,
56 client_id: CLIENT_ID,
57 code_verifier: req.session.pkceVerifier, // Prove we started the flow
58 }),
59 });
60
61 // Clear session values
62 delete req.session.pkceVerifier;
63 delete req.session.oauthState;
64
65 const tokens = await tokenResponse.json();
66 // ... handle tokens
67});Why must the code_verifier be generated client-side and never sent until token exchange?
06 //Token Security
How you store, transmit, and handle tokens is critical. Token leakage through logs, referrer headers, or insecure storage can lead to account compromise.
1// VULNERABLE: Token in URL (leaks via Referrer header)
2window.location = '/dashboard?access_token=' + token;
3// If user clicks external link, token sent in Referer header!
4
5// VULNERABLE: Token in localStorage (XSS can steal it)
6localStorage.setItem('access_token', token);
7// Any XSS vulnerability can read this!
8
9// VULNERABLE: Logging tokens
10console.log('User authenticated:', { user, token });
11// Tokens in logs = major security incident
12
13// VULNERABLE: Token in error messages
14catch (err) {
15 res.status(500).json({
16 error: err.message,
17 context: { token: req.headers.authorization } // Leaked!
18 });
19}
20
21// VULNERABLE: Long-lived tokens
22const token = jwt.sign({ userId }, SECRET, { expiresIn: '1y' });
23// 1 year token = 1 year for attacker if leaked1// SECURE: Store tokens in httpOnly cookies
2res.cookie('access_token', accessToken, {
3 httpOnly: true, // JavaScript cannot access
4 secure: true, // HTTPS only
5 sameSite: 'lax', // CSRF protection
6 maxAge: 15 * 60 * 1000, // 15 minutes
7 path: '/',
8});
9
10// SECURE: Separate access and refresh token storage
11res.cookie('access_token', accessToken, {
12 httpOnly: true,
13 secure: true,
14 sameSite: 'strict',
15 maxAge: 15 * 60 * 1000, // Short-lived
16});
17
18res.cookie('refresh_token', refreshToken, {
19 httpOnly: true,
20 secure: true,
21 sameSite: 'strict',
22 maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
23 path: '/api/auth/refresh', // Only sent to refresh endpoint
24});
25
26// SECURE: Token rotation on use
27async function refreshTokens(refreshToken) {
28 // Validate refresh token
29 const session = await validateRefreshToken(refreshToken);
30
31 // Rotate refresh token (one-time use)
32 await revokeRefreshToken(refreshToken);
33 const newRefreshToken = await generateRefreshToken(session.userId);
34
35 // Generate new access token
36 const newAccessToken = generateAccessToken(session.userId);
37
38 return { accessToken: newAccessToken, refreshToken: newRefreshToken };
39}Token Storage Options
| Storage | XSS Risk | CSRF Risk | Recommendation |
|---|---|---|---|
| localStorage | HIGH - JS can read | None | Avoid for tokens |
| sessionStorage | HIGH - JS can read | None | Avoid for tokens |
| Cookie (default) | LOW | HIGH | Not recommended |
| Cookie (httpOnly) | NONE | MEDIUM | Good with SameSite |
| Cookie (httpOnly + SameSite) | NONE | LOW | Recommended |
| Memory only | LOW | None | Good for SPAs (lost on refresh) |
Why should access tokens have short expiration times?
07 //Prevention Techniques
Securing OAuth requires attention to detail across multiple components. Here is a comprehensive checklist for OAuth security.
OAuth Security Checklist
| Control | Implementation | Priority |
|---|---|---|
| State parameter | Cryptographically random, validated on callback | Critical |
| PKCE | S256 challenge, verifier stored securely | Critical |
| Redirect URI validation | Exact string match, no wildcards | Critical |
| HTTPS only | All OAuth endpoints and redirects | Critical |
| Token storage | httpOnly cookies or secure memory | Critical |
| Short token lifetime | Access: 15-60 min, Refresh: 7-30 days | High |
| Token rotation | Rotate refresh tokens on use | High |
| Scope minimization | Request only necessary permissions | High |
| Client secret protection | Never expose in client-side code | Critical |
| Nonce for OIDC | Prevent token replay attacks | High |
1import crypto from 'crypto';
2import { OAuth2Client } from 'google-auth-library';
3
4const oauthClient = new OAuth2Client(
5 process.env.GOOGLE_CLIENT_ID,
6 process.env.GOOGLE_CLIENT_SECRET,
7 process.env.GOOGLE_REDIRECT_URI
8);
9
10// Secure login initiation
11app.get('/auth/google', (req, res) => {
12 // Generate security parameters
13 const state = crypto.randomBytes(32).toString('hex');
14 const nonce = crypto.randomBytes(32).toString('hex');
15 const codeVerifier = crypto.randomBytes(32).toString('base64url');
16 const codeChallenge = crypto.createHash('sha256')
17 .update(codeVerifier)
18 .digest('base64url');
19
20 // Store in session (server-side)
21 req.session.oauth = {
22 state,
23 nonce,
24 codeVerifier,
25 initiatedAt: Date.now(),
26 };
27
28 const authUrl = oauthClient.generateAuthUrl({
29 access_type: 'offline',
30 scope: ['openid', 'email', 'profile'],
31 state,
32 nonce,
33 code_challenge: codeChallenge,
34 code_challenge_method: 'S256',
35 prompt: 'consent', // Force consent to get refresh token
36 });
37
38 res.redirect(authUrl);
39});
40
41// Secure callback handling
42app.get('/auth/google/callback', async (req, res) => {
43 try {
44 const { code, state, error } = req.query;
45 const oauthSession = req.session.oauth;
46
47 // Check for OAuth errors
48 if (error) {
49 console.error('OAuth error:', error);
50 return res.redirect('/login?error=oauth_denied');
51 }
52
53 // Validate session exists
54 if (!oauthSession) {
55 console.warn('No OAuth session found');
56 return res.redirect('/login?error=session_expired');
57 }
58
59 // Validate state (CSRF protection)
60 if (!state || state !== oauthSession.state) {
61 console.warn('State mismatch - possible CSRF');
62 return res.redirect('/login?error=invalid_state');
63 }
64
65 // Validate timing (prevent old auth attempts)
66 const maxAge = 10 * 60 * 1000; // 10 minutes
67 if (Date.now() - oauthSession.initiatedAt > maxAge) {
68 return res.redirect('/login?error=expired');
69 }
70
71 // Exchange code for tokens with PKCE verifier
72 const { tokens } = await oauthClient.getToken({
73 code,
74 codeVerifier: oauthSession.codeVerifier,
75 });
76
77 // Validate ID token
78 const ticket = await oauthClient.verifyIdToken({
79 idToken: tokens.id_token,
80 audience: process.env.GOOGLE_CLIENT_ID,
81 });
82
83 const payload = ticket.getPayload();
84
85 // Validate nonce (replay protection)
86 if (payload.nonce !== oauthSession.nonce) {
87 console.warn('Nonce mismatch - possible replay attack');
88 return res.redirect('/login?error=invalid_nonce');
89 }
90
91 // Clear OAuth session
92 delete req.session.oauth;
93
94 // Create application session
95 const user = await findOrCreateUser(payload);
96 req.session.userId = user.id;
97
98 // Store tokens securely if needed
99 if (tokens.refresh_token) {
100 await storeRefreshToken(user.id, tokens.refresh_token);
101 }
102
103 res.redirect('/dashboard');
104
105 } catch (error) {
106 console.error('OAuth callback error:', error);
107 res.redirect('/login?error=auth_failed');
108 }
109});What is the purpose of the nonce parameter in OpenID Connect?
08 //Implicit Flow Dangers
The Implicit flow was designed for browser-based applications before CORS was widely available. It returns tokens directly in the URL fragment, creating multiple security vulnerabilities.
1# Implicit Flow Response
2https://app.com/callback#access_token=eyJhbGc...&token_type=bearer&expires_in=3600
3
4# VULNERABILITY 1: Browser History
5- Token is in URL, saved in browser history
6- Anyone with access to browser can see it
7- Shared computers = token exposure
8
9# VULNERABILITY 2: Referrer Header Leakage
10- User clicks external link from the callback page
11- Browser sends Referer header with full URL
12- External site receives the access token!
13
14# VULNERABILITY 3: Token Injection Attack
15- Attacker crafts URL with their own token
16- Tricks victim into visiting the URL
17- Victim's app accepts attacker's token
18- No way to verify token came from auth server
19
20# VULNERABILITY 4: No Refresh Tokens
21- Implicit flow cannot return refresh tokens
22- Forces longer-lived access tokens
23- Increases impact of token theft
24
25# VULNERABILITY 5: Client-Side Only
26- Tokens exposed to JavaScript
27- Any XSS vulnerability = token theft
28- No server-side validation possible1// OLD: Implicit flow (VULNERABLE)
2function loginImplicit() {
3 const authUrl = 'https://auth.example.com/authorize?' +
4 'response_type=token' + // Returns token directly
5 '&client_id=' + CLIENT_ID +
6 '&redirect_uri=' + REDIRECT_URI +
7 '&scope=openid profile';
8
9 window.location = authUrl;
10}
11
12// Callback extracts token from fragment
13function handleCallback() {
14 const hash = window.location.hash.substring(1);
15 const params = new URLSearchParams(hash);
16 const token = params.get('access_token');
17 // Token exposed in URL, history, and to JS
18}
19
20// NEW: Auth Code + PKCE (SECURE)
21async function loginWithPKCE() {
22 // Generate PKCE
23 const verifier = generateRandomString(64);
24 const challenge = await sha256Base64Url(verifier);
25
26 // Store verifier (sessionStorage for SPAs is acceptable for verifier)
27 sessionStorage.setItem('pkce_verifier', verifier);
28
29 const authUrl = 'https://auth.example.com/authorize?' +
30 'response_type=code' + // Returns code, not token
31 '&client_id=' + CLIENT_ID +
32 '&redirect_uri=' + REDIRECT_URI +
33 '&scope=openid profile' +
34 '&code_challenge=' + challenge +
35 '&code_challenge_method=S256' +
36 '&state=' + generateRandomString(32);
37
38 window.location = authUrl;
39}
40
41// Callback exchanges code for token
42async function handleCallback() {
43 const params = new URLSearchParams(window.location.search);
44 const code = params.get('code');
45 const verifier = sessionStorage.getItem('pkce_verifier');
46
47 // Exchange code for token (server-side ideally)
48 const response = await fetch('https://auth.example.com/token', {
49 method: 'POST',
50 headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
51 body: new URLSearchParams({
52 grant_type: 'authorization_code',
53 code: code,
54 redirect_uri: REDIRECT_URI,
55 client_id: CLIENT_ID,
56 code_verifier: verifier,
57 }),
58 });
59
60 const tokens = await response.json();
61 // Token never in URL!
62}Why can't the Implicit flow use refresh tokens?
09 //Scope & Consent Attacks
Scope manipulation attacks occur when attackers modify the requested scopes to gain additional privileges, or when consent screens can be bypassed or manipulated.
1# ATTACK 1: Scope Upgrade
2Original request: scope=read:profile
3Attacker modifies: scope=read:profile admin:all delete:users
4
5# If authorization server doesn't validate against registered scopes,
6# attacker gets admin token!
7
8# ATTACK 2: Scope Downgrade for Bypass
9App requires: scope=openid email profile
10Attacker requests: scope=openid
11# If app doesn't validate returned scopes, attacker bypasses
12# email verification requirement
13
14# ATTACK 3: Consent Bypass via prompt=none
15GET /authorize?...&prompt=none
16# Silently authenticates without showing consent screen
17# Useful for SSO, but can be abused if user previously consented
18# to different scopes
19
20# ATTACK 4: Incremental Authorization Abuse
21# User consents to: email
22# Attacker's app silently adds: contacts, drive
23# If provider allows incremental auth without re-consent, attacker
24# gains additional permissions
25
26# ATTACK 5: Scope Parameter Injection
27scope=profile%20admin # Space-separated scopes
28scope=profile%0Aadmin # Newline injection
29scope=profile%00admin # Null byte injection1// Server-side: Validate scopes against registration
2const REGISTERED_SCOPES = {
3 'client-123': ['openid', 'profile', 'email'], // No admin!
4 'client-456': ['openid', 'profile', 'email', 'admin'],
5};
6
7function validateScopes(clientId, requestedScopes) {
8 const allowedScopes = REGISTERED_SCOPES[clientId];
9 if (!allowedScopes) {
10 throw new Error('Unknown client');
11 }
12
13 // Parse and validate each scope
14 const scopes = requestedScopes.split(' ').filter(Boolean);
15
16 for (const scope of scopes) {
17 // Check for injection attempts
18 if (!/^[a-z0-9:_-]+$/i.test(scope)) {
19 throw new Error('Invalid scope format');
20 }
21
22 // Check against allowlist
23 if (!allowedScopes.includes(scope)) {
24 throw new Error(`Scope not allowed: ${scope}`);
25 }
26 }
27
28 return scopes;
29}
30
31// Client-side: Validate returned scopes
32async function handleTokenResponse(tokens) {
33 // Decode token to check scopes
34 const payload = decodeJwt(tokens.access_token);
35 const returnedScopes = payload.scope?.split(' ') || [];
36 const requiredScopes = ['openid', 'email'];
37
38 // Verify all required scopes are present
39 for (const required of requiredScopes) {
40 if (!returnedScopes.includes(required)) {
41 throw new Error(`Missing required scope: ${required}`);
42 }
43 }
44
45 // Verify no unexpected scopes (scope escalation)
46 const allowedScopes = ['openid', 'profile', 'email'];
47 for (const returned of returnedScopes) {
48 if (!allowedScopes.includes(returned)) {
49 console.warn(`Unexpected scope: ${returned}`);
50 // Decide: reject or proceed with caution
51 }
52 }
53}Why should the client validate scopes in the returned token?
10 //Real-World Case Studies
These real-world OAuth vulnerabilities demonstrate the practical impact of the security issues discussed. Learning from these cases helps identify similar patterns.
Case Study: Slack OAuth CSRF (2017)
Slack's OAuth implementation had a missing state parameter validation. Attackers could perform OAuth CSRF to link victim's Slack account to attacker's external service account. This allowed attackers to receive webhooks and notifications from victim's Slack workspace. Bug bounty payout: $3,000.
1# Attacker's OAuth flow
21. Attacker initiates OAuth: Slack → External Service
32. Attacker authenticates with THEIR External Service account
43. Attacker captures callback URL before completion:
5 https://slack.com/oauth/callback?code=ATTACKERS_CODE
6
74. Attacker sends URL to victim (phishing, XSS, etc.)
85. Victim's browser completes OAuth with attacker's code
9
10Result: Victim's Slack workspace is connected to attacker's
11 External Service account
12
13# Missing state parameter meant Slack couldn't detect
14# that victim didn't initiate the OAuth flowCase Study: Microsoft OAuth Redirect Vulnerability (2020)
Researchers discovered that Microsoft's OAuth implementation allowed redirect URIs with subdomain variations that could be attacker-controlled. Combined with Azure services that allowed subdomain registration, attackers could steal tokens by redirecting to attacker-controlled Azure subdomains. Microsoft paid $13,000 for this vulnerability.
1# Registered redirect_uri: https://*.azurewebsites.net/callback
2
3# Attack:
41. Attacker creates Azure Web App: evil-app.azurewebsites.net
52. Attacker crafts OAuth URL:
6 /authorize?redirect_uri=https://evil-app.azurewebsites.net/steal
7
83. Microsoft OAuth validates: *.azurewebsites.net ✓
94. User completes authentication
105. Token sent to attacker's Azure app!
11
12# Root cause: Wildcard redirect_uri validation
13# Fix: Require exact match or app-specific subdomainsWhat common pattern appears across these OAuth case studies?
11 //Security Tools
These tools help identify OAuth vulnerabilities during development and security testing.
OAuth Security Testing Tools
| Tool | Type | Best For |
|---|---|---|
| Burp Suite | Proxy | Intercepting and modifying OAuth flows |
| OWASP ZAP | Proxy | Automated OAuth vulnerability scanning |
| oauth-hunt | CLI | OAuth misconfiguration detection |
| JWT.io | Web | Decoding and debugging JWT tokens |
| Postman | API Client | Testing OAuth endpoints manually |
| oauth2-proxy | Proxy | Testing auth proxy configurations |
1# OAuth Security Testing Steps
2
31. STATE PARAMETER
4 □ Remove state parameter - does callback accept request?
5 □ Modify state to different value - is it validated?
6 □ Reuse old state value - is replay prevented?
7
82. REDIRECT_URI
9 □ Try different paths: /callback/../admin
10 □ Try subdomain: attacker.example.com
11 □ Try different protocol: http:// instead of https://
12 □ Try URL encoding bypass: %2e%2e%2f
13 □ Try adding parameters: /callback?evil=param
14
153. PKCE
16 □ Is PKCE required? Try without code_challenge
17 □ Try wrong code_verifier at token exchange
18 □ Try "plain" challenge method instead of S256
19
204. TOKEN HANDLING
21 □ Check token storage (localStorage = bad)
22 □ Check for tokens in URLs/logs
23 □ Test token expiration
24 □ Attempt token replay
25
265. SCOPE
27 □ Request additional unauthorized scopes
28 □ Modify scope in authorization URL
29 □ Check if granted scopes are validated- Burp Suite - Web security testing
- JWT.io - JWT decoder and debugger
- OAuth.net - OAuth 2.0 specification reference
- OpenID Connect - OIDC specification
- OAuth Security Best Practices (RFC)
12 //Conclusion
OAuth 2.0 is complex, and its security depends on correct implementation of multiple interacting components. A single missing validation can lead to complete account takeover. When reviewing OAuth implementations, assume nothing is secure until verified.
When reviewing code, trace the entire OAuth flow from login initiation to token usage. Check that state is generated, stored, and validated. Verify redirect_uri validation is strict. Confirm PKCE is implemented correctly. And always assume attackers will manipulate every parameter.