Study guides · Intermediate

Design a session from issuance through revocation

Trace login, identifier rotation, cookie delivery, authorization and logout as one lifecycle, separating each protection from the guarantees it does not provide.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Connect later requests to a verified actor
  2. Keep identifier meaning and generation narrow
  3. Update state when authentication changes
  4. Read cookie attributes by responsibility
  5. Separate CSRF defenses from resource authorization
  6. Enforce expiration and revocation at the server
  7. Verify a sequence rather than one happy-path request
  8. Test the request after logout, not only the screen
  9. Exercise: decide what an old identifier can do
  10. Ask Logeia about this articleCheck your understanding

Connect later requests to a verified actor

When an HTTP request arrives, the server needs a basis for treating it as an action by a particular actor. A session associates an actor established at login with later requests. Our running design keeps a server-side session table and sends the browser an opaque, unpredictable identifier in a cookie. When that identifier returns, the server looks up the actor, expiration and revocation state. A self-contained signed token would have a different state and invalidation design. Do not merge those choices into a claim that cookies always implement one authentication architecture. Cookies provide browser storage and delivery behavior; the application decides what their values mean. Begin by drawing where each piece of state lives and which component verifies the value before relying on the associated identity.

Keep identifier meaning and generation narrow

Do not base a session identifier only on a username, sequence number or another easily predicted value. In a possession-based design, someone able to present the identifier may be treated as the actor associated with it. Generate opaque identifiers with an established cryptographically suitable platform API rather than inventing a function that mixes timestamps and user numbers. Complex-looking output is not evidence of unpredictability. Generation is only one part of the lifecycle: decide where identifiers are retained, through which channel they travel and until when they remain valid. Putting a usable identifier in a URL or complete diagnostic log can create extra copies in histories, downstream systems or investigation tools. Operational correlation should be designed separately from exposing a value that can itself be presented as authority.

Update state when authentication changes

After successful login, avoid unconditionally promoting a caller’s pre-authentication identifier into a powerful authenticated session. In our example policy, issue a new identifier and invalidate the old one, examining the ordering of those actions. A major privilege change can also require reviewing rotation and session state. Rotation does not repair resource authorization by itself: access to document D still requires current policy for that actor and resource. Concurrent requests during rotation introduce another contract. State when an old identifier stops working and which operations may be retried. If a transitional acceptance period is deliberately supported, bound its scope rather than allowing the old authority to survive indefinitely. Follow the server-side accepted states, not only a visible change to the browser’s cookie string.

Read cookie attributes by responsibility

Secure concerns the channel on which a cookie may be sent. HttpOnly restricts access to its value from ordinary page JavaScript. SameSite affects delivery on requests crossing site boundaries. Domain and Path influence delivery scope, but none of these attributes performs resource authorization at the server. HttpOnly can hide an identifier from document.cookie without preventing malicious same-origin script from causing authenticated actions. Also distinguish site from origin: scheme, host and port define an origin, while two different origins can still belong to the same site. Test a delivery table instead of treating a row of protective attributes as a complete argument. Browser versions and privacy settings can impose additional restrictions, so eligibility under an attribute rule and observed delivery are different forms of evidence.

Separate CSRF defenses from resource authorization

Automatic cookie attachment does not prove that a user intended the requested state change. Depending on the application design, validate a token associated with the session, the expected source of a request, or other suitable CSRF defenses. Passing that check still does not authorize B to modify A’s document. An intended action from the genuine browser may remain forbidden for the selected resource. Place authentication, request-intent or source checks and resource authorization in separate columns so each mitigation has a clear responsibility. State changes performed by GET and same-origin script execution through XSS raise additional problems. A single cookie attribute cannot be used as a blanket explanation that all of these distinct failure modes have been eliminated.

Enforce expiration and revocation at the server

A cookie remaining in browser storage does not establish that the server will accept its session. Map expiration and logout to server-side state and checks. Conversely, deleting one browser cookie does not necessarily make another retained copy of the same identifier invalid. Verify logout by attempting a later request with the earlier identifier, not merely by observing a changed screen. If the design uses both idle timeout and an absolute lifetime, state their reference points and renewal conditions separately. Clock differences and propagation across session stores also need an explicit scope. Closing a browser must not be assumed to revoke every server-side session in every implementation. Drawing browser retention and server acceptance as two separate timelines makes this distinction easier to inspect.

Verify a sequence rather than one happy-path request

Build a test history containing anonymous retrieval, login, identifier rotation, permitted retrieval, a forbidden update, retrieval after sharing revocation and retrieval after logout. At each point record cookie delivery, resolved actor, applied permission and returned information without unnecessarily publishing usable identifiers in the evidence. Controlled clocks or a dedicated short lifetime can test timeout boundaries without long waits. Happy-path login alone misses acceptance of stale identifiers. Rejecting every request is not correct either, so establish that A’s legitimate retrieval works in the appropriate earlier state. Preserve the sequence as regression coverage. When a configuration change breaks access, distinguish a browser no longer sending the cookie from a server that receives it and deliberately rejects the actor or operation.

Test the request after logout, not only the screen

Logout is more than hiding a username. Test whether a request carrying the invalidated server-side session is rejected, and separately whether the browser cookie is removed using the appropriate attributes. Cookie removal involves matching Path and Domain scope as well as its name. Record a signed-out screen and rejection of an old session identifier as distinct outcomes. The first verifies presentation; the second verifies the authorization state on a subsequent request. Keeping both avoids treating a successful interface transition as evidence of revocation.

Exercise: decide what an old identifier can do

Suppose login rotates s1 to s2 and the server invalidates s1. A request using s2 can retrieve A’s document when expiration and authorization also permit it. A separately retained s1 must fail even if a client successfully sends it in a cookie. Now delete only the browser’s s2 cookie while leaving the server record valid. That deletion alone does not establish rejection of another retained s2 copy; server-side invalidation needs its own condition. Finally explain why HttpOnly does not remove the need for CSRF or resource-permission checks. It restricts script access to the cookie value, while those other controls address different properties of a request. Explaining all three cases along one request history connects attribute knowledge to implementable session verification.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 22

Choose a learning path