Cookies and sessions: what keeps you logged in
A session rests on an id the server issues after login and the browser sends in a cookie. Whoever has that id is the user as far as the server is concerned, so the id is long and random and the cookie has HttpOnly, Secure and SameSite. Logout destroys the session on the server, not just the cookie. HttpOnly only limits what can be stolen and does not remove XSS.
Pentest · HTTP
A user logged out, but the old token still opens their page. How come?
HTTP does not remember who you are: every request is separate. To make a “login” last, after sign-in the server issues a session identifier and the browser attaches it to every following request. That identifier is the key: whoever has it is the user as far as the server is concerned, even without a password. So most attacks on sessions come down to how to steal the key, guess it, or make the server accept a foreign one.
How it works and why. After sign-in the server creates a session record (user, creation time, expiry) and returns its id in a Set-Cookie header. The browser stores the cookie and sends it in the Cookie header on every request to the matching domain and path. The server finds the record by id and knows who is asking. The alternative is a token that carries data itself (for example a JWT): then the server looks nothing up, but such a token is harder to revoke (see the lesson on sessions and JWT in the authentication chapter).
The life of a session
Cookie attributes decide where it applies, whether a script can reach it, including a foreign one injected into the page through XSS (cross-site scripting), and whether it goes with a request forged by another page (CSRF); both attacks are covered in detail in the client-side chapter:
| Attribute | What it does | Against what |
|---|---|---|
HttpOnly | hides from JavaScript | theft via XSS |
Secure | HTTPS only | eavesdropping |
SameSite | whether to send from other sites | part of CSRF |
Path, Domain | where it applies | excess exposure |
__Host- | prefix: Secure, Path=/, no Domain | overwrite from a subdomain |
Max-Age | lifetime | long-lived theft |
Example: a weak and a stronger cookie. QA opens DevTools (the Application tab, Cookies section) or looks at the response header after sign-in. A weak variant:
HTTP/1.1 200 OK
Set-Cookie: sid=1042; Path=/
There are three defects: the id looks like a sequential number (the neighbour can be guessed: 1041, 1043), there is no HttpOnly (XSS can read the cookie), and no Secure or SameSite. A stronger variant:
HTTP/1.1 200 OK
Set-Cookie: __Host-sid=q8Zk3...long-random;
Path=/; Secure; HttpOnly; SameSite=Lax
Here the value is long and random from a cryptographic generator (OWASP advises at least 64 bits of entropy, in practice people use 128). An explicit SameSite is needed because the default behaviour differs between browsers. One more useful QA test: copy the cookie, log out and repeat a request with the same value.
GET /api/profile HTTP/1.1
Host: shop.example
Cookie: __Host-sid=q8Zk3...long-random
The expected answer is 401 Unauthorized: logout must destroy the session on the server, not just delete the cookie in the browser. If it returns 200, a key stolen earlier keeps working.
A real case. In September 2018 Facebook reported a vulnerability in the “View As” feature (viewing your profile as another person sees it). Attackers stole access tokens, “digital keys that keep people logged in”: at first the company named almost 50 million affected accounts and reset about 40 million more as a precaution, so roughly 90 million people had to log in again; in October it clarified that tokens of about 30 million people were stolen. The issue was found on 25 September; Facebook fixed the code, reset the tokens and temporarily switched the feature off. Lesson for a tester: a session token is equivalent to the password and MFA at once, so the server must be able to revoke it, and stealing one key must not open so many accounts.
Limits. HttpOnly does not stop XSS: the script cannot read the cookie but can send requests on the user’s behalf, and the browser attaches the cookie by itself. SameSite=Lax does not save from every cross-site request, and Secure does not help if the id leaked through a log or a URL. Also check separately: the session id changes after login (otherwise session fixation, covered in the authentication chapter) and there are limits for inactivity and total lifetime.
Common mistakes.
- Assuming that logout deletes the session: check by replaying the old cookie.
- Putting the session id in the URL: it ends up in history, logs and the Referer header.
- Relying on
HttpOnlyas XSS protection: it only limits what can be stolen. - Setting no session lifetime: a stolen key works indefinitely.
Check yourself
The session cookie has HttpOnly, but the page has stored XSS. What can the attacker still do?
Answer
He cannot read the cookie, but the script runs on the user’s page and can send requests (change the password, e-mail, make a transfer), and the browser attaches the cookie itself. So HttpOnly reduces the damage, but the XSS still has to be fixed.
After logging out, the same request with the old cookie returns 200. What is the defect and why is it dangerous?
Answer
Logout does not destroy the session on the server (it only deletes the cookie). A key stolen earlier (through XSS, a log, a shared computer) keeps working, so logging out gives the user no protection.
__Host- prefix, idle and absolute timeouts, session revocation, a new session after login, the difference between a server-side session and a self-contained token, and the Facebook 2018 case where one kind of token opened tens of millions of accounts.