OWASP WSTG: Part 7 - Session Management
WSTG v4.2 section 4.6 explained: session schema and token randomness, cookie attributes, session fixation, exposed session variables, CSRF, logout, session timeout, session puzzling and session hijacking, with tests and fixes.
HTTP has no memory: every request is independent. To know that request #2 comes from the same logged-in user as request #1, applications issue a session token. That token is the user’s identity for the rest of the visit, so stealing it, fixing it, or tricking the browser into sending it is as good as stealing the password.
Session Management testing (WSTG section 4.6, IDs WSTG-SESS-*) checks how tokens are generated, protected, transmitted, renewed and destroyed, and whether an attacker can ride a victim’s session (CSRF) or take it over (hijacking, fixation).
Source: WSTG v4.2 §4.6 Session Management Testing (CC BY-SA 4.0), explained and expanded. Only test systems you are authorised to test.
Key Takeaways
- Tokens must be long, random and unpredictable, and must change on login and privilege change.
- Cookie attributes are the first defence:
Secure,HttpOnly,SameSite, and the__Host-prefix. - Rotate on authentication to kill session fixation; destroy on the server at logout.
- CSRF abuses an authenticated session from another site; hijacking steals the token itself.
- Most fixes are configuration and framework defaults done right, plus anti-CSRF tokens on state-changing requests.
The Life of a Session Token
flowchart LR
GEN["Generation<br/>random, unpredictable (SESS-01)"] --> STORE["Stored in cookie<br/>Secure/HttpOnly/SameSite (SESS-02)"]
STORE --> AUTH["Login<br/>rotate the ID (SESS-03)"]
AUTH --> USE["In use<br/>not in URL/logs (SESS-04)<br/>anti-CSRF (SESS-05)"]
USE --> END["Termination<br/>logout (SESS-06), timeout (SESS-07)"]
USE -. "attacks" .-> ATT["Puzzling (SESS-08)<br/>Hijacking (SESS-09)"]
WSTG-SESS-01: Session Management Schema
What it checks: how session tokens are created and whether they are predictable, forgeable or reversible.
How to test:
- Collect tokens: gather many session IDs (same user re-logging in, different users) through Burp.
- Analyse structure: is the token opaque random, or does it encode data you can read and change?
1
ID=5a0acfc7ffeb919:CR=1:TM=1120514521:LM=1120514521:S=j3am5KzC4v01ba3q
A token like that mixes a timestamp (TM) and a counter (CR), which hints at predictability. Base64/hex-decode tokens and look for usernames, roles, dates or sequential parts.
- Measure randomness: feed hundreds of tokens to Burp Sequencer, which estimates entropy. Low entropy means tokens can be predicted or brute-forced.
- Modify unsigned tokens: if the token carries
role=useroruid=100without a signature, change it and see if the server trusts it.
Fix: use the framework’s cryptographically secure session generator (never hand-rolled), keep session state server-side with an opaque ID of at least 128 bits of entropy, and sign/encrypt any token that carries data (and verify it).
WSTG-SESS-02: Cookie Attributes
What it checks: that session cookies carry the right security attributes.
How to test: read every Set-Cookie:
1
curl -sI https://example.com/login -d 'user=..&pass=..' | grep -i set-cookie
| Attribute | What it does | Why it matters |
|---|---|---|
Secure |
Sent only over HTTPS | Without it, the cookie leaks over any HTTP request |
HttpOnly |
Hidden from JavaScript | Blocks cookie theft via XSS |
SameSite=Lax/Strict |
Not sent on cross-site requests | Strong CSRF defence |
Domain |
Which hosts receive it | Too broad shares the cookie with every subdomain |
Path |
Which paths receive it | Scope it to the app |
Expires/Max-Age |
Lifetime | Session cookies should be short-lived or per-session |
Cookie prefixes add guarantees the attributes alone cannot:
__Secure-requiresSecureand an HTTPS origin.__Host-additionally requiresPath=/and noDomain, so a subdomain cannot overwrite it (a defence against cookie-tossing).
What a finding looks like: a session cookie with no HttpOnly (so any XSS steals it) and no SameSite (so it rides CSRF).
Fix: set Secure; HttpOnly; SameSite=Lax (or Strict for sensitive apps) on session cookies, scope Domain/Path tightly, and prefer a __Host- prefixed name.
WSTG-SESS-03: Session Fixation
What it checks: whether the session ID stays the same before and after login. If it does, an attacker who plants a known ID in the victim’s browser becomes authenticated as them once they log in.
How it works: the attacker sets SESSIONID=KNOWN in the victim’s browser (via a link, an XSS, or a sub-domain), the victim logs in, the server keeps the same ID, and the attacker, holding KNOWN, is now inside the victim’s account.
How to test:
- Get a pre-login session cookie.
- Log in using that same cookie value.
- Check the cookie after login: if the ID is unchanged, the application is vulnerable.
1
2
GET /login HTTP/1.1 -> Set-Cookie: SESSION=AAA (anonymous)
POST /login (with SESSION=AAA) -> if the response keeps SESSION=AAA, it's fixation
Fix: always issue a new session ID on authentication (and invalidate the old one); regenerate on any privilege change too.
WSTG-SESS-04: Exposed Session Variables
What it checks: that session tokens are not exposed where they can be captured: in URLs, logs, caches, proxies, or over plain HTTP.
How to test:
- In the URL: a token as a query parameter (
?sid=...) ends up in browser history, server logs, proxy logs and theRefererheader sent to third parties. - GET vs POST: sensitive values (including login) sent via GET are logged; they should be POST.
- Caching: can an authenticated page with the token be cached by a proxy? Check
Cache-Control. - Transport: is the token ever sent over HTTP? (ties to SESS-02’s
Secure).
Fix: keep tokens in cookies (never in URLs), send security-sensitive data via POST over HTTPS, mark authenticated responses non-cacheable, and set Secure on cookies.
WSTG-SESS-05: Cross-Site Request Forgery (CSRF)
What it checks: whether an attacker can make a logged-in victim’s browser perform a state-changing action (transfer money, change email, add an admin) without their intent. The browser automatically attaches the session cookie, so the request looks genuine.
How to test:
- Capture a state-changing request (change email, change password, transfer).
- Look for an anti-CSRF token: a secret, per-session (or per-request) value the attacker cannot predict.
- Try the attack: remove the token, reuse another user’s token, or submit from a different origin.
1
2
3
4
5
<!-- Proof-of-concept: auto-submits as the victim if no CSRF protection -->
<form action="https://example.com/account/email" method="POST">
<input type="hidden" name="email" value="attacker@evil.com">
</form>
<script>document.forms[0].submit()</script>
Also check whether the action works as a GET (even easier to force), and whether SameSite cookies already block the cross-site send (a strong built-in defence).
Fix: use anti-CSRF tokens (synchroniser or double-submit) on every state-changing request, set SameSite=Lax/Strict on session cookies, require re-authentication for the most sensitive actions, and never make state changes on GET. See the OWASP CSRF Prevention Cheat Sheet.
WSTG-SESS-06: Logout Functionality
What it checks: that logging out truly ends the session, on the server, not just in the browser.
How to test:
- Log in, capture the session cookie, then log out.
- Replay an authenticated request with the old cookie (Burp Repeater). If it still works, the server did not destroy the session, it only cleared the client cookie.
- Check that a logout link is present and reachable on every page, and that the browser Back button does not restore an authenticated view (ties to ATHN-06).
- In SSO environments, test single-logout: logging out of one app should end the federated session, not leave others open.
Fix: invalidate the session server-side on logout, clear the cookie, and in SSO propagate the logout.
WSTG-SESS-07: Session Timeout
What it checks: that idle sessions expire automatically, so an unattended or stolen session does not live forever.
How to test:
- Log in, stay idle, and after the expected timeout replay a request: it should require re-authentication.
- Confirm the timeout is enforced server-side (just clearing a client cookie is not enough).
- Check for both an idle timeout (inactivity) and an absolute timeout (maximum session age regardless of activity).
Fix: set an idle timeout appropriate to the data’s sensitivity (minutes for banking, longer for low-risk apps) and an absolute maximum lifetime, enforced on the server.
WSTG-SESS-08: Session Puzzling (Session Variable Overloading)
What it checks: whether the same session variable is used for more than one purpose, letting an attacker reach a state they should not.
How it works: suppose the app stores the username in a session variable during password recovery, and also checks that same variable to decide if someone is logged in. By starting the recovery flow (which sets the variable) and then jumping straight to an authenticated page, an attacker may appear logged in without ever authenticating.
How to test:
- Enumerate the session variables the app sets in different flows (registration, recovery, multi-step forms).
- Start a flow that populates a variable, then break the expected order: skip to a later or different step, and see whether the half-set state is accepted as authentication or authorization.
Fix: use each session variable for a single, consistent purpose; track flow state explicitly; and verify every step’s preconditions rather than inferring state from a shared variable.
WSTG-SESS-09: Session Hijacking
What it checks: whether a session cookie can be captured and replayed to impersonate a user, especially against a network attacker.
How to test (authorised, with test accounts):
- Confirm the cookie is exposed somewhere (missing
Secure, sent over HTTP, readable by XSS withoutHttpOnly, or leaked in a URL/log). - Take a valid session cookie from one browser/account and present it from another client; if the application accepts it with no further binding, the session is hijackable.
- Check whether anything binds the session to the client (re-authentication for sensitive actions, re-issuing IDs); strict IP binding is often impractical but some apps use coarse checks.
Fix: this is the pay-off of the earlier tests: enforce HTTPS + HSTS, Secure/HttpOnly/SameSite cookies, rotate IDs on login, short timeouts, and re-authenticate for high-value actions so a stolen session has limited value.
Worked Example: CSRF on “Change Email” and Its Fix (SESS-05)
If a state-changing request is authenticated only by the session cookie, the browser attaches that cookie automatically, even when the request is triggered by another site.
Vulnerable (Express) — no anti-CSRF token, cookie is default SameSite:
1
2
3
4
app.post('/account/email', requireLogin, async (req, res) => {
await db.users.update(req.user.id, { email: req.body.email }); // trusts the cookie alone
res.redirect('/account');
});
Exploit — a page on the attacker’s site that auto-submits as the logged-in victim:
1
2
3
4
5
<!-- hosted on evil.example; fires the moment the victim opens it -->
<form id="x" action="https://app.example.com/account/email" method="POST">
<input name="email" value="attacker@evil.example">
</form>
<script>document.getElementById('x').submit()</script>
The victim’s browser sends their session cookie with the request; their account email is changed, and the attacker then uses “forgot password” to take over.
Fixed — two layers: a per-session CSRF token the attacker cannot read, and SameSite cookies:
1
2
3
4
5
6
7
8
9
10
11
12
13
// 1. issue the session cookie with SameSite (blocks the cross-site send by default)
app.use(session({
cookie: { httpOnly: true, secure: true, sameSite: 'lax' }
}));
// 2. require a synchroniser token on every state-changing request
const csrf = require('csurf');
app.use(csrf());
app.post('/account/email', requireLogin, async (req, res) => {
// csurf rejects the request if the token is missing or wrong (403)
await db.users.update(req.user.id, { email: req.body.email });
res.redirect('/account');
});
1
2
3
4
5
<!-- the real form includes the token; the attacker's page cannot read it -->
<form action="/account/email" method="POST">
<input type="hidden" name="_csrf" value="">
<input name="email">
</form>
For the highest-value actions (change password, change email, transfer), also re-authenticate or require the current password, so a single CSRF or XSS cannot complete them silently.
Note on
SameSite. Modern browsers default cookies toSameSite=Lax, which blocks the simplest cross-sitePOST. That is real defence in depth, but do not rely on it alone:GET-based state changes, sibling-subdomain attacks, and older clients still need explicit anti-CSRF tokens.
Hands-on Practice
- PortSwigger Academy — the CSRF labs walk through token-validation flaws,
SameSitebypasses and method-based bypasses; the session material covers fixation and token weaknesses. - Juice Shop — the CSRF and “Change the href of…” challenges.
- Burp Sequencer — capture a few hundred session tokens from a test app and measure their entropy (SESS-01).
Checklist: Session Management
| ID | Test | Main tools |
|---|---|---|
| WSTG-SESS-01 | Session management schema | Burp Sequencer, decoders |
| WSTG-SESS-02 | Cookie attributes | Burp/ZAP, curl, EditThisCookie |
| WSTG-SESS-03 | Session fixation | Burp, cookie comparison |
| WSTG-SESS-04 | Exposed session variables | Burp, log/URL review |
| WSTG-SESS-05 | Cross-site request forgery | Burp, PoC HTML, CSRF PoC generator |
| WSTG-SESS-06 | Logout functionality | Burp Repeater |
| WSTG-SESS-07 | Session timeout | Burp Repeater, waiting |
| WSTG-SESS-08 | Session puzzling | Burp, flow manipulation |
| WSTG-SESS-09 | Session hijacking | Burp, cookie replay |
Previous: Part 6 - Authorization · Next: Part 8 - Input Validation
References
- WSTG-SESS-01 Session Management Schema
- WSTG-SESS-02 Cookies Attributes
- WSTG-SESS-03 Session Fixation
- WSTG-SESS-04 Exposed Session Variables
- WSTG-SESS-05 Cross Site Request Forgery
- WSTG-SESS-06 Logout Functionality
- WSTG-SESS-07 Session Timeout
- WSTG-SESS-08 Session Puzzling
- WSTG-SESS-09 Session Hijacking
- OWASP Session Management Cheat Sheet
Comments
Questions, corrections or another approach? Comment with your GitHub account.