highCWE-352A07:2021

OAuth State Parameter Missing

OAuth authorization flow implemented without a random `state` parameter, allowing CSRF attacks that link a victim's account to the attacker's OAuth identity.

How It Works

The OAuth `state` parameter is a random value you generate, include in the authorization URL, and verify when the callback returns. Without it, an attacker can initiate an OAuth flow, get the callback URL (before completing login), and send it to a victim. The victim clicks it, completes OAuth, and their account is now linked to the attacker's social identity.

Vulnerable Code
// BAD: OAuth redirect without state
export async function GET() {
  const authUrl = `https://github.com/login/oauth/authorize?client_id=${CLIENT_ID}&scope=user`;
  return Response.redirect(authUrl);
}
Secure Code
// GOOD: generate and verify state parameter
export async function GET(req: Request) {
  const state = crypto.randomUUID();
  // Store state in session to verify later
  const session = await getSession();
  session.oauthState = state;
  await session.save();
  const authUrl = `https://github.com/login/oauth/authorize?client_id=${CLIENT_ID}&state=${state}&scope=user`;
  return Response.redirect(authUrl);
}

Real-World Example

Facebook, Twitter, and GitHub OAuth implementations in third-party apps have all had state-parameter-missing vulnerabilities reported. Successful exploitation means an attacker can link their GitHub account to a victim's app account, gaining access.

How to Prevent It

  • Always generate a cryptographically random state parameter (use crypto.randomUUID() or nanoid)
  • Store the state in the user's session before redirecting to the OAuth provider
  • On callback, verify the returned state matches the stored state exactly
  • Use an OAuth library (next-auth, passport.js) that handles state management for you

Affected Technologies

nodejsNext.jsPython

Data Hogo detects this vulnerability automatically.

Scan Your Repo Free

Related Vulnerabilities