token-storage-security
它能做什么
这项技能检查网页应用如何存储登录令牌,以及脚本是否可能窃取它们。适用于构建或审查登录系统的开发者。
适用场景
- ✓当网页应用把登录令牌存放在 localStorage 或 sessionStorage 中的时候。
- ✓当有人为网站设置新的登录系统的时候。
- ✓当安全审查询问被注入的脚本是否可能窃取登录令牌的时候。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/token-storage-security/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的应用把用户的登录令牌存放在 localStorage 中。这样安全吗?我该如何修改?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会检查令牌的存放位置,以及数据变更请求是否受到防伪造保护。它会建议把令牌移到 JavaScript 无法读取的安全 Cookie 中。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Store authentication tokens securely
localStorage is accessible to any JavaScript running on the page. A single XSS vulnerability — including one in a third-party script — can exfiltrate all tokens silently. httpOnly cookies are completely invisible to JavaScript; even if an attacker executes arbitrary code on the page, they cannot read the cookie. This single architectural choice eliminates the most common token theft vector.
Quick Reference
- Store session tokens and JWTs in httpOnly cookies — not localStorage
- localStorage is readable by any JavaScript on the page, including injected XSS code
- Combine httpOnly with Secure and SameSite=Strict (or Lax) cookie flags
- Use short-lived access tokens and rotate refresh tokens on each use
- Protect every state-changing request with CSRF defenses, not cookie flags alone
Check
Check whether authentication tokens are stored in httpOnly cookies or in JavaScript-accessible storage like localStorage. Verify that POST, PUT, PATCH, and DELETE requests also require a CSRF token or an equivalent anti-forgery control.
Fix
Move token storage from localStorage/sessionStorage to httpOnly cookies set by the server, and ensure the cookies have Secure and SameSite flags. Add CSRF protection to every state-changing route that relies on browser-sent credentials.
Explain
Explain why localStorage is vulnerable to XSS token theft and how httpOnly cookies mitigate this attack vector.
Code Review
Review authentication flows, token storage calls, and API client code. Flag any use of localStorage.setItem, sessionStorage.setItem, or document.cookie for storing access tokens, JWTs, or session identifiers. Also flag credentialed mutations that lack a CSRF token, Origin check, or same-site enforcement.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/security/token-storage-security
常见问题
为什么 localStorage 存放令牌有风险?
页面上的任何 JavaScript 都可以读取它,因此一个被注入的脚本就能窃取所有令牌。
这项技能推荐哪些 Cookie 设置?
它推荐 httpOnly 和 Secure,并将 SameSite 设为 Strict 或 Lax。
只有安全 Cookie 就够了吗?
不够。这项技能指出,会改变数据的请求仍需要防伪造请求保护,即 CSRF 保护。
详情
作者的原始描述
Use when reviewing authentication implementation, setting up a new auth system, or evaluating whether the current token storage approach exposes the application to XSS-based token theft.
热度
- GitHub 星标
- 75k
- 排名
- #3059
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。