QA Interview Hub · Knowledge base · Quizzes · Українська

QA Fundamentals › Web and client-server

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

Browser, one origincookiessent with every requestlocalStorageJS only, until removedsessionStorageJS only, one tabServer
Figure 1. Only cookies travel with a request automatically, so only they are seen by the server without the help of page scripts.
CookieslocalStoragesessionStorage
Sent to the serverYes, with every requestNoNo
LifetimeUntil Expires or Max-Age; without them until the browser session endsUntil removedWhile the tab is open
Capacityabout 4 KB per cookieabout 5 MB per originabout 5 MB per origin
Shared between tabsYesYesNo, each tab has its own
Access from a scriptYes, unless HttpOnlyYesYes

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:

ActionWhat we expect
Log outThe 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 tabcookies and localStorage are shared, sessionStorage is empty
Close the tab and open it againsessionStorage is empty; whether a session cookie stays depends on the browser and on tab restore settings
Incognito modeNothing from the normal window is visible, after the window closes everything disappears
Delete the cookie by hand in DevToolsThe 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.

In an interview. Junior: Cookies are sent to the server with every request and hold a few kilobytes. localStorage lives in the browser until it is cleared, and sessionStorage only while the tab is open. All of it is visible in DevTools on the Application tab. Middle/Senior: Cookies are for the server, Web Storage for client code: that difference decides what to check. For a session cookie I look at the HttpOnly, Secure and SameSite flags and at the lifetime, and after logout I check not only that the cookie disappears but also that an old request with it gives 401. Separately I check a new tab, incognito and manual deletion of values, because that is where a "left-over" and a "lost" state become visible.

More articles in this chapter