Cookies and sessions
Why applications need state
HTTP does not itself remember a user between independent requests. A session is an application mechanism for associating related activity with an authenticated user or other state. A cookie is a small value managed by a browser under cookie rules. Notes uses a server-side session: the browser carries an opaque identifier and the server stores the associated user and expiry. Other session architectures exist.
Follow a fictional session
After successful sign-in, the server issues Set-Cookie with a fictional session marker S1. Later the browser may send a Cookie header containing S1 according to its rules. The application looks up S1 and checks expiry and revocation before identifying Mina. The marker in this worksheet is not a usable credential. Production session identifiers need secure generation and protection.
Cookie protection attributes
In an HTTPS session design, Secure restricts cookie transmission to secure channels, with browser-specific exceptions such as localhost; HttpOnly prevents ordinary script access to the cookie value but does not stop scripts from initiating requests. SameSite constrains certain cross-site cookie sending; its modes are not identical and it is not a complete request-forgery defense. Scope and lifetime also matter. These are defense layers, not permission checks.
Expiry and logout
At minute 5, a valid S1 may identify Mina. At minute 30, suppose its server-side expiry has passed: the same marker must no longer identify an authenticated user. Logging out should invalidate the relevant server-side session and clear the browser cookie in this design. Merely removing a cookie locally does not establish server-side invalidation. Avoid placing actual session identifiers in logs or teaching examples.
Practice with three states
Classify fictional markers S1 (valid), S2 (expired) and S3 (unknown). Only S1 may identify a signed-in user. Even S1 does not by itself grant permission to every note: the application checks the requested action and record separately. Next, locate those checks at trust boundaries.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cookies and sessions — Knowledge check
Reason about fictional web application components and security decisions.
Where is Mina’s identity stored in this session design?
Which attribute prevents ordinary JavaScript from directly reading a cookie?
What should happen when server-side S2 expires?
Which action completes logout in the described design?
Does a valid session guarantee access to every record?