前端安全

216 阅读1分钟

XSS攻击

  1. xss(cross site scripting)跨站脚本,主要就是获取cookie信息
  2. 类型: 存储型 反射型
  3. 解决办法:
  • <1>设置cookie的httpOnly属性,使JavaScript获取不到cookie
  • <2>用户输入的所有信息,进行过滤和转义

转义特殊字符函数:

function escapeHtml(value) {
  if (typeof value !== 'string') {
    return value
  }
  return value.replace(/[&<>`"'\/]/g, function(result) {
    return {
      '&': '&amp;',
      '<': '&lt;',
      '>': '&gt;',
      '`': '&#x60;',
      '"': '&quot;',
      "'": '&#x27;',
      '/': '&#x2f;',
    }[result]
  })
}