Harden session cookie attributes
A session cookie is a bearer token: whoever holds it is the user. The flags on that cookie are the only thing stopping script access, network sniffing, and cross-site replay, so they are checked individually rather than assumed from framework defaults.
Procedure
Observe the actual
Set-Cookieon login rather than reading config:curl -si -X POST https://app.example.com/login \ -d 'user=demo&pass=demo' | rg -i '^set-cookie:'Check each attribute against its job:
Attribute Required Rejects HttpOnlyyes JS read via XSS Secureyes plaintext transport SameSite=LaxorStrictyes cross-site CSRF replay Path=/scoped over-broad scope leak Domainomitted (host-only) subdomain injection Max-Age/Expiresbounded immortal sessions Prefer the
__Host-prefix, which the browser only accepts withSecure,Path=/, and noDomain:Set-Cookie: __Host-sid=<token>; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=86400Rotate the session id on every privilege change — login, password change, and role elevation — to close session fixation:
# after successful auth request.session.cycle_key() # Django # or regenerate the id before writing the cookieSet an idle timeout (e.g. 30 min) and an absolute cap (e.g. 12 h) and enforce both server-side.
Invalidate server-side on logout, not just by clearing the cookie client-side; a stolen copy must stop working.
Never store session ids in
localStorage; that is JavaScript-readable and defeatsHttpOnly.
Pitfalls
SameSite=Nonesilently requiresSecure; without it browsers drop the cookie entirely.- A
Domain=.example.comcookie is sent to every subdomain, so one XSS on an old host takes the session. - Clearing a cookie on logout without deleting the server record leaves a valid token.
- Session id in the URL (for "shareable" links) leaks via Referer and logs.
- Frameworks default
SameSitedifferently across versions — verify, do not assume. - Rolling
Max-Ageon every request makes the idle timeout moot unless the absolute cap is kept.
Verification
curl -si https://app.example.com/login -d 'user=demo&pass=demo' \
| rg -i '^set-cookie:' | rg -q 'HttpOnly' && rg -q 'Secure' && echo OK || echo MISSING-FLAG
# session-fixation check: id before login must differ from id after
test "$(curl -s .../login -c - | awk '/sid/{print $7}')" != "$(pre_login_id)" && echo ROTATED
Pass: the cookie carries Secure, HttpOnly, and SameSite, uses the __Host- prefix or omits
Domain, and the id changes across login. Report each attribute observed and the rotation result.