Skip to main content

Session Fixation

Description

The cookie session doesn't change after a valid login which allow an attacker to hijack the user session by setting the user cookie for one known by the attacker.

Impact

Hijack a valid user session.

Recommendation

Invalidate the anonymous cookie and create a new one after a successful login.

Threat

Anonymous attacker from Internet.

Expected Remediation Time

⌚ 120 minutes.

Score

Default score using CVSS 3.1. It may change depending on the context of the vulnerability.

Base

  • Attack vector: N
  • Attack complexity: L
  • Privileges required: N
  • User interaction: R
  • Scope: U
  • Confidentiality: L
  • Integrity: L
  • Availability: N

Temporal

  • Exploit code madurity: P
  • Remediation level: X
  • Report confidence: X

Result

  • Vector string: CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:L/I:L/A:N/E:P/RL:X/RC:X
  • Score:
    • Base: 5.4
    • Temporal: 5.1
  • Severity:
    • Base: Medium
    • Temporal: Medium

Code Examples

Compliant code

The application updates user cookies after every sucessful login

const signinHandler = (req, res) => {
// get users credentials from the JSON body
const { username, password } = req.body
if (!username) {
// If the username isn't present, return an HTTP unauthorized code
res.status(401).end();
return;
}

// validate the password against our data
// if invalid, send an unauthorized code
const expectedPassword = users[username]
if (!expectedPassword || expectedPassword !== password) {
res.status(401).end();
return;
}

// generate a random UUID as the session token
const sessionToken = uuid.v4();
const now = new Date();
const expiresAt = new Date(+now + 120 * 1000);
const session = new Session(username, expiresAt);
sessions[sessionToken] = session
res.cookie("session_token", sessionToken, { expires: expiresAt })
res.end()
}

Non compliant code

The application does not update the user cookie after a login

const signinHandler = (req, res) => {
// get users credentials from the JSON body
const { username, password } = req.body
if (!username) {
// If the username isn't present, return an HTTP unauthorized code
res.status(401).end();
return;
}
// Use a user previous session cookie
const sessionToken = user.storedSession;
res.cookie("session_token", sessionToken, { expires: expiresAt });
}

Requirements