Web Storage Security
Web Storage is origin-scoped client storage whose contents are directly accessible to JavaScript. Its main security concern is that any script executing in the same origin can read or modify stored data.
`localStorage` and `sessionStorage` provide key-value storage associated with a web origin. `localStorage` normally persists across browser sessions, while `sessionStorage` is scoped to a particular tab or browsing context and lasts for that context's lifetime.
The critical security property is JavaScript accessibility: scripts running under the same origin can access Web Storage. Therefore, storing a bearer credential such as a session token there can expose that credential to an XSS vulnerability. Web Storage does not provide an `HttpOnly` mechanism because its API is intended for script access.
Origin isolation limits direct access between different origins, but it does not protect stored values from compromised code within the same origin. A third-party script, dependency, or injected script that executes with the application's origin can potentially access sensitive Web Storage data.
For authentication, a common alternative is an `HttpOnly`, `Secure` cookie, optionally with an appropriate `SameSite` setting. JavaScript cannot directly read an `HttpOnly` cookie, reducing the impact of token theft through ordinary JavaScript access, although cookies introduce other considerations such as CSRF defenses and session management.
Key points
- Web Storage is scoped by origin.
- JavaScript can directly read and write Web Storage.
- XSS can expose sensitive values stored in Web Storage.
- `HttpOnly` cookies cannot be read by page JavaScript.
- Do not treat Web Storage as a secure secret store.
Interview questions
Is localStorage secure for storing authentication tokens?
It depends on the threat model, but it is generally risky for high-value authentication tokens because JavaScript can read it. An XSS vulnerability can therefore allow an attacker to extract a token stored there. An `HttpOnly` cookie prevents ordinary JavaScript from directly reading the cookie value. The application still needs appropriate cookie and CSRF protections.
What is the difference between localStorage and sessionStorage from a security perspective?
Both are accessible to JavaScript running in the relevant origin, so neither should be considered a secret store. `localStorage` generally persists until explicitly cleared, whereas `sessionStorage` is tied to a browsing context and normally disappears when that context ends. The shorter lifetime of `sessionStorage` can reduce persistence, but it does not prevent XSS from reading the value while the page is running.
Can another website read my localStorage?
A different origin cannot directly access the storage area belonging to your origin because browser storage is origin-scoped. The origin includes the scheme, host, and port. However, code executing as part of your origin can access its storage, which is why XSS is especially relevant. Same-origin restrictions therefore limit cross-origin access but do not make storage secret from same-origin scripts.
Why might an HttpOnly cookie be preferable to localStorage for a session identifier?
An `HttpOnly` cookie cannot be read through the normal JavaScript cookie API, so an XSS payload cannot simply retrieve the session identifier from JavaScript-accessible storage. The browser can still attach the cookie to applicable requests. However, cookie authentication requires careful handling of CSRF, `SameSite`, `Secure`, expiration, and session invalidation. It reduces one token-exfiltration path rather than eliminating the consequences of XSS.