Key points
- Mark session cookies to hide them from scripts and limit where they are sent.
- Expire sessions and issue a new token at sign-in.
- Use TLS everywhere and CSRF tokens on state-changing requests.
- Logout must end the session on the server.
A web session lets a server recognise a returning user across many requests, which makes the session token a valuable target. Web security is covered more broadly in application security and under web topics. See session in the glossary. Short lifetimes and fresh sign-in checks for sensitive actions limit the damage when a token is stolen.
Cookie attributes
Session cookies should be marked so that scripts cannot read them, so they are sent only over encrypted connections, and so that browsers restrict sending them with requests started by other sites. These attributes remove several common attack routes at almost no cost.
Lifetime and rotation
Sessions should expire after inactivity and after a maximum age. The token should be replaced whenever privilege changes, especially at sign-in, so that an old token becomes useless.
Session fixation
In a fixation attack, the attacker plants a known token and waits for the victim to sign in under it. Issuing a fresh token at sign-in, and never accepting tokens supplied in a link, prevents it.
TLS and CSRF tokens
Serving every page over TLS stops tokens being read in transit. A CSRF token, a secret value checked on each state-changing request, stops other sites from making a signed-in browser perform actions unintentionally.
Logout
Logout should destroy the session on the server, not just delete the cookie in the browser, so that a captured token cannot be reused afterwards.