Skip to main content
HighClient-sideOWASP A02:2021CWE-312

How to fix: Sensitive Data Stored in localStorage

The page stores authentication tokens, JWTs, or other sensitive values in localStorage. localStorage is accessible to any JavaScript on the page (including XSS payloads) and persists across sessions.

Why it matters

Unlike HttpOnly cookies, localStorage is fully readable by JavaScript. Any XSS vulnerability on the page can read the stored token and exfiltrate it. A compromised CDN script, third-party chat widget, or analytics library can silently steal tokens from localStorage.

Storing JWTs or session tokens in localStorage is a common pattern promoted by SPA tutorials. The security trade-off is that HttpOnly cookies are invisible to JavaScript (protecting against XSS) while localStorage is always accessible.

How to fix it

  1. 1Store session tokens in HttpOnly, Secure, SameSite=Strict cookies instead of localStorage.
  2. 2If cookies are not feasible (e.g., cross-origin API), use sessionStorage (cleared on tab close) with a tight CSP to reduce the attack window.
  3. 3Never store refresh tokens in localStorage.
  4. 4Consider the BFF (Backend for Frontend) pattern: keep tokens server-side, use session cookies.

Code examples

Move from localStorage to HttpOnly cookie

TypeScript
// Bad (common SPA pattern)
localStorage.setItem('auth_token', token);

// Good: set via server response
// Server sets: Set-Cookie: auth=...; HttpOnly; Secure; SameSite=Strict
// Client just checks if API calls succeed, no token handling in JS

References

Detected automatically by VulnRadar. The check id is localstorage-sensitive-data, stable across every scan so you can filter or diff on it.

See every client-side check

Questions

How severe is Sensitive Data Stored in localStorage?
VulnRadar rates this high severity in the client-side category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id localstorage-sensitive-data, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does Sensitive Data Stored in localStorage map to?
It maps to CWE-312. In the OWASP Top 10 it falls under A02:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for sensitive data stored in localstorage

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

Sensitive Data Stored in localStorage fix | VulnRadar