highCWE-384A07:2021

Session Fixation

The server reuses the same session ID before and after login, allowing an attacker who planted a known session ID to hijack the authenticated session.

How It Works

An attacker visits your site, gets a session ID, and tricks a victim into using that same ID (via a URL parameter or injected cookie). After the victim logs in, the session is now authenticated — and the attacker, who already knows the session ID, now has a valid authenticated session without any credentials.

Vulnerable Code
// BAD: same session ID used before and after login
async function login(req, res) {
  const user = await authenticate(req.body);
  // Session ID stays the same — attacker already knows it
  req.session.userId = user.id;
  res.redirect('/dashboard');
}
Secure Code
// GOOD: regenerate session ID on login
async function login(req, res) {
  const user = await authenticate(req.body);
  // Regenerate session to get a new ID the attacker doesn't know
  req.session.regenerate((err) => {
    req.session.userId = user.id;
    res.redirect('/dashboard');
  });
}

Real-World Example

Session fixation attacks are common on banking and e-commerce sites that accept session IDs via URL parameters (a legacy pattern). An attacker sends a phishing link containing their known session ID, and the victim unknowingly authenticates it.

How to Prevent It

  • Always regenerate the session ID immediately after a successful login
  • Never accept session IDs from URL parameters — use cookies only
  • Set short session timeouts and regenerate on privilege escalation
  • Use a well-maintained session library (express-session with proper configuration)

Affected Technologies

nodejsNext.jsPythonPHP

Data Hogo detects this vulnerability automatically.

Scan Your Repo Free

Related Vulnerabilities