OAuth2 Security Best Practices: 6 Vulnerabilities That Get Apps Breached
OAuth2 Is Everywhere. Most Implementations Are Broken. If you're implementing OAuth2 in your app -- whether as a provider or consumer -- these are the mistakes that get developers breached. Vulnera...

Source: DEV Community
OAuth2 Is Everywhere. Most Implementations Are Broken. If you're implementing OAuth2 in your app -- whether as a provider or consumer -- these are the mistakes that get developers breached. Vulnerability 1: Missing State Parameter The state parameter prevents CSRF attacks on OAuth flows. Without it, an attacker can trick a user into connecting their account to the attacker's credentials. Wrong: GET /oauth/authorize?client_id=...&redirect_uri=...&response_type=code Right: // Generate a random state, store in session const state = crypto.randomBytes(32).toString('hex') req.session.oauthState = state const authUrl = new URL('https://provider.com/oauth/authorize') authUrl.searchParams.set('client_id', CLIENT_ID) authUrl.searchParams.set('redirect_uri', REDIRECT_URI) authUrl.searchParams.set('response_type', 'code') authUrl.searchParams.set('state', state) // Critical // In callback: if (req.query.state !== req.session.oauthState) { throw new Error('State mismatch -- possible CSRF a