跳到正文
Skillpedia
安全75k · #3059

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/token-storage-security/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#3059
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能