How do cookies, localStorage and sessionStorage differ?
Cookies are sent to the server with every request, while localStorage and sessionStorage stay in the browser. Lifetimes differ: localStorage lives until cleared, sessionStorage only while the tab is open. Cookies have attributes (HttpOnly, Secure, SameSite). After logout the check covers not only the cookie disappearing but also that the old cookie gets a 401.
QA Fundamentals · Web and client-server
A user logged out, and after a page refresh they are "logged in" again. Where is that stored?
QA tests logging out: clicks "Log out", sees the sign-in page and moves on. A week later a complaint reaches support: on a shared computer the next person opened the site and saw someone else's account. It turns out that the "Log out" button only cleared the state in the interface, while the session cookie stayed and kept working. This lesson is about the three places where a browser stores data, how they differ, and how to build a check from that which catches such mistakes.
How it works and why. Cookies were invented so that a server could remember a user: HTTP remembers nothing itself, so the server hands the client a cookie (the Set-Cookie header), and the browser adds it to every next request to that site (the Cookie header). Hence the automatic sending and the limited size. Web Storage (localStorage and sessionStorage) appeared later for data needed only by client code: settings, drafts, a cache. It is not sent over the network, so it costs no traffic and has a bigger capacity. Both storages work only through JavaScript and are bound to an origin (scheme, host and port).
What goes to the server and what stays in the browser
| Cookies | localStorage | sessionStorage | |
|---|---|---|---|
| Sent to the server | Yes, with every request | No | No |
| Lifetime | Until Expires or Max-Age; without them until the browser session ends | Until removed | While the tab is open |
| Capacity | about 4 KB per cookie | about 5 MB per origin | about 5 MB per origin |
| Shared between tabs | Yes | Yes | No, each tab has its own |
| Access from a script | Yes, unless HttpOnly | Yes | Yes |
A cookie has attributes that control its behaviour. HttpOnly hides it from JavaScript. Secure allows sending only over HTTPS. SameSite restricts sending in cross-site requests. Max-Age or Expires set the lifetime, Domain and Path set which addresses it belongs to. The capacities in the table are approximate: exact limits depend on the browser.
Example: a logout test that catches a "left-over" session. A weak check: click "Log out", see the sign-in page. It checks only the interface. A full check asks what changed in each storage and whether the server really stops letting the user in. For that, in the logout response the server must cancel the cookie, and the old value must stop working:
HTTP/1.1 200 OK
Set-Cookie: sid=; Max-Age=0; Path=/; Secure; HttpOnly
GET /api/me HTTP/1.1
Cookie: sid=old-session-value
HTTP/1.1 401 Unauthorized
The first line tells the browser to delete the cookie (Max-Age=0), and the second block shows the main point: even if somebody copied the cookie value earlier and sent it by hand, the server answers 401. Deleting the cookie without cancelling the session on the server is half of the job. These observations then form a table of expectations:
| Action | What we expect |
|---|---|
| Log out | The session cookie is deleted, tokens and user data are removed from the storages, a request with the old cookie gives 401 |
| Open the site in a new tab | cookies and localStorage are shared, sessionStorage is empty |
| Close the tab and open it again | sessionStorage is empty; whether a session cookie stays depends on the browser and on tab restore settings |
| Incognito mode | Nothing from the normal window is visible, after the window closes everything disappears |
| Delete the cookie by hand in DevTools | The next request to a protected section leads to sign-in and does not crash with an error |
All of this is visible in DevTools on the Application tab: cookies with their flags, localStorage and sessionStorage with keys and values. They can be edited and deleted by hand there, which is handy for negative scenarios: an expired cookie, broken JSON in localStorage.
Limits and alternatives. Web Storage is synchronous and stores only strings: objects must be serialised through JSON, and an unexpected format under a key (an old app version wrote another format) can break the new version after an update. A browser may clear storage when space runs short or by a privacy policy: for example, Safari limits the lifetime of data written by script. For big or structured data there is IndexedDB, for offline resources the Cache API. Data in localStorage and in cookies without HttpOnly is available to any page script, so an XSS vulnerability lets it be stolen; why session tokens are better kept in an HttpOnly cookie is covered by a separate lesson about storage security.
Common mistakes.
- Checking logout only by the look of the page: the session on the server may stay valid.
- Assuming cookies, localStorage and sessionStorage "remember" the user alike: each has its own rules of life and visibility.
- Not trying a new tab and incognito: bugs with a "left-over" or "lost" state show up exactly there.
- Storing sensitive data in localStorage and treating it as normal for a test: a security check is also a part of QA's job.
- Forgetting about size: long values in a cookie enlarge every request and can cause a server or proxy error.
Check yourself
After "Log out" the sign-in page appears, but in DevTools on the Application tab the sid cookie is still there. What can it mean and what do you check?
Answer
Maybe the client only changed the interface. I send a request to a protected endpoint with the old cookie: if it is 200, the session on the server is alive, which is a serious security defect. I also check the logout response: whether it has Set-Cookie with Max-Age=0.
A user filled in a draft in one tab and opened the site in a second one. The draft is empty there. Which kind of storage was most likely used?
Answer
sessionStorage: it is separate for each tab. If the draft must live between tabs, localStorage or saving on the server is needed, and that is a question for the requirements.
Why do large values in a cookie do harm, and in localStorage they do not?
Answer
A cookie is added automatically to every request to the site, so a large cookie bloats all the traffic and can exceed the header limit of a proxy or server. localStorage does not go to the network, its size affects only the browser memory.