Highlights

Contents

Part II
02

Sessions and cookies

We preserve a login across requests. A session credential can be copied, fixed, or left valid after the user thinks it ended. We will trace session creation, cookie transport, expiration, rotation, and revocation.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Authentication vsauthorization2Sessions and cookies3JWT and API keys4OAuth 2.0 and PKCE5RBAC, ACLs, and resourcepolicy6Encryption and secrets7Password hashing andsigned URLs8CORS, CSRF and abuseprevention9Case study: securing oneoperation

#Section 5A session links an opaque token to server state

Mira signs in successfully. The service creates an unpredictable token and stores a record identifying Mira, creation time, expiry, and other policy context. A session is authenticated application state continued across requests, commonly referenced by a client credential. An opaque session token carries no application meaning to the client; the server resolves it.

The next request presents the token, and the service looks up its current record before authorizing the requested action. The session record can be revoked centrally. Its identifier must be generated with a cryptographically secure source, protected during transport and storage, and excluded from ordinary logs.

AN OPAQUE COOKIE POINTS TO CURRENT SERVER-SIDE SESSION STATEAN OPAQUE COOKIE POINTS TO CURRENT SERVER-SIDE SESSION STATEbrowseropaque IDsession recordtoken hashsubject + scopeexpiry + revoked?lookup + current checkthe credential identifies server state; current policy decides the effect
Figure 18.5Illustrative opaque-session flow. Resource authorization occurs after session resolution.

An illustrative thirty-two-byte random token has 256 bits and 22562^{256} possible byte strings. This counts a chosen token space, not a universal prescription or an attack-success guarantee. Real security also depends on randomness quality, exposure, lifecycle, and endpoint controls. OWASP session guidance covers those lifecycle concerns.

#Section 6Cookies transport credentials under browser rules

A cookie is browser-managed state associated with a name, scope, and delivery attributes. The browser can automatically attach a session cookie to qualifying requests. This convenience also creates cross-site-request concerns because the application code did not explicitly add the credential for each action.

Secure restricts cookie delivery to secure transport under the browser’s rules. HttpOnly prevents script access through the ordinary document cookie interface; it does not stop injected script from making authenticated requests. SameSite controls some cross-site sending behavior, with different modes and contextual rules. Domain and path select scope but are not replacements for resource authorization.

Browser cookie scope must be tested against the intended subdomains and paths. A broad domain can expose a credential to additional hosts under that domain, while a path attribute is not a security boundary against all same-origin code. Prefer deliberate host scoping and verify the provider or framework behavior. Cookie delivery policy is separate from the application resource predicate.

Use a deliberately scoped cookie and avoid broader domain access than needed. The application must also review lifetime, logout, rotation, and cross-site flows. A cookie marked HttpOnly is still a bearer credential on the wire. Do not log it or expose it in URLs because another copy can remain usable after the browser is protected.

#Section 7Rotate after identity changes

An attacker persuades Mira to use a session identifier the attacker already knows, then waits for Mira to log in. Session fixation is an attack in which a known pre-authentication session becomes the victim’s authenticated session. Regenerating the session identifier at authentication or privilege changes prevents that old reference from becoming the new credential.

The service creates a new authenticated session and invalidates the old identifier according to a controlled transition. It should not merely attach Mira’s identity to the existing attacker-known token. The browser receives the new credential. Concurrent requests and old-session cleanup need defined behavior so the transition does not reopen the previous state.

LOGIN ISSUES A NEW SESSION TOKEN B; THE PRE-LOGIN TOKEN A IS INVALIDATEDLOGIN ISSUES A NEW SESSION TOKEN B; THE PRE-LOGIN TOKEN A IS INVALIDATEDtoken Aanonymouslogintoken BMiranever attach the identity to a token an attacker may have planted
Figure 18.7Illustrative fixation-resistant session transition. A new credential accompanies the identity change.

Rotation also follows privilege changes and recovery flows where the policy requires it. A stolen old token should not retain elevated access after a new session is issued. Central session records help enforce invalidation, but distributed caches can delay it unless their consistency and lifetime are chosen deliberately.

#Section 8Expiration and logout are server decisions

Mira clicks logout. Deleting a browser cookie does not revoke another stolen copy of the session token. Revocation makes a credential or its associated permission unusable before its ordinary expiry. For server sessions, invalidate the session record and clear the browser credential under the chosen scope.

A session can have both idle and absolute limits. An idle limit expires after no qualifying activity; an absolute limit expires regardless of activity. Define which requests renew idle activity so automated background calls do not unintentionally keep a session alive forever. Reauthentication for sensitive actions can establish a fresher assurance boundary.

LOGOUT MUST KILL THE SERVER RECORD; CLEARING THE COOKIE LEAVES A COPIED TOKEN ALIVELOGOUT MUST KILL THE SERVER RECORD; CLEARING THE COOKIE LEAVES A COPIED TOKEN ALIVElogoutrequestinvalidate recordserver sideclear cookiebrowsercopied tokennow rejectedidle and absolute timeouts end sessions nobody logs out of
Figure 18.8Illustrative server-side logout flow. Clearing the local cookie alone would leave another copy valid.

Fifty thousand illustrative two-hundred-byte session records occupy 10,000,000 raw bytes before indexes, replication, and runtime overhead. The session store’s availability and cache policy influence login continuity. Do not keep sessions valid indefinitely to conceal an outage. State whether stale cached records can be used and what revocation delay that allows.