Application Security In progress
Intermediate 18 min read Application Security
Authentication and Session Security
Secure the keys to the kingdom: password storage, MFA, session lifetime, cookie flags, and the account-recovery paths attackers weaponize.
sessionscookiesJWTMFAaccount recoverypassword resetsession fixation
Introduction
What is this?
Authentication and session security cover how a user proves identity, how that proof is carried for the rest of the session, and how logout/recovery actually end it.
Motivation
Why do you need it?
- A login bypass or session leak is the crown-jewel incident: one account, one cookie, whole kingdom.
- The OWASP lists identity/session flaws near the top every year because they are the easiest to exploit and the hardest to undo.
- MFA and session hygiene are the difference between 'credential leaked' and 'breach' in modern forensics.
Think about it
Mental model
Imagine
A session cookie
Think of
A room key that also works in the lobby after checkout
The key is fine until it stops expiring, stops being unique, or outlives the stay.
Mechanics
How it works
- Passwords are stored as salted hashes (bcrypt/argon2) never plaintext; login compares against the computed hash.
- A session is a server-side token (or signed cookie) given at login; the cookie flags (HttpOnly, Secure, SameSite) decide how transportable it is.
- JWT is a stateless alternative: signed claims with expiry, refreshed at an appropriate interval.
- Recovery flows (reset link, security questions, recovery codes) are convenient → high-value targets; they must be equally hardened.
The session lifecycle
Logincredential + MFA verified
Sessiontoken issued, cookie flags set
Userequests carry the token
Expireidle/timeout kills server side
Logoutserver invalidates, not just client
Real world
In production
- An org stores only bcrypt hashes and rotated the pepper; a DB leak of the hash table brings attackers brute-force pain.
- A SaaS enforces MFA via TOTP, rotates sessions on password change, and revokes a session the instant a logout request lands.
- A bank's password-reset requires the email link AND a one-time code, closing the 'researcher links' attack the standard reset had.
Guidelines
Best practices
- Store passwords as bcrypt/argon2 with per-user salt; never MD5/SHA-1/plaintext.
- Set HttpOnly; Secure; SameSite=Lax on session cookies; keep sensitive routes behind proper auth.
- Expire sessions on idle timeout and on password change; honor logout server-side.
- Protect recovery: rate-limit reset, bind links to time + user, and expire them fast.
Watch out
Common mistakes
- Using JWT as a 'session' without revocation — logout does nothing because the token still validates.
- Only clearing the client cookie on logout, leaving a live server session.
- No MFA on admin accounts, then a stolen password equals system admin.
- Rate-limiting login but not the reset endpoint where the attacker actually sits.
When things break
Debugging tips
- Inspect the Set-Cookie header for the flags; missing HttpOnly is the first thing to fix.
- Reproduce logout bugs by replaying the old cookie after logout — a 200 means the server never ended it.
- Examine reset flows twice: once as the email owner, once replaying the old link.
Security
Security considerations
- Session logs must exclude raw credentials; only hash-based verification records are stored.
- Design recovery flows so a flighted support agent cannot reset passwords without a second factor.
- All session tests happen on your own accounts; don't prod real user sessions, even 'just to test'.
Interviews
Interview checkpoint
Practice
Hands-on lab
Session Spring Cleaning in Your Own App
45 min · intermediate
Objectives
- Observe cookie flags on a real local app login.
- Prove logout leaves a live server session on a vulnerable lab build.
- Wire a server-side revocation and confirm the replayed cookie dies.
Session Spring Cleaning in Your Own App
45 min · intermediate
0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself
Quick quiz
0/2
1
Which cookie attribute prevents JavaScript from reading the session token?
2
A logout that only deletes the browser cookie is incomplete when...
Go deeper