关于xss:XSS跨站脚本攻击与防御

8次阅读

共计 721 个字符,预计需要花费 2 分钟才能阅读完成。

在此记录 XSS 的相干常识

XSS 的具体介绍能够查看 XSS 跨站脚本攻打与进攻


// 对用户输出的内容进行转码

/*1. 用浏览器外部转换器实现 html 转码 */
export function htmlEncode (html){
  // 1. 首先动态创建一个容器标签元素,如 DIV
  let temp = document.createElement ("div");
  // 2. 而后将要转换的字符串设置为这个元素的 innerText(ie 反对) 或者 textContent(火狐,google 反对)
  (temp.textContent !== undefined) ? (temp.textContent = html) : (temp.innerText = html);
  // 3. 最初返回这个元素的 innerHTML,即失去通过 HTML 编码转换的字符串了
  const output = temp.innerHTML;
  temp = null;
  return output;
}

/*2. 用浏览器外部转换器实现 html 解码 */
export function htmlDecode (text){
  // 1. 首先动态创建一个容器标签元素,如 DIV
  let temp = document.createElement("div");
  // 2. 而后将要转换的字符串设置为这个元素的 innerHTML(ie,火狐,google 都反对)
  temp.innerHTML = text;
  // 3. 最初返回这个元素的 innerText(ie 反对) 或者 textContent(火狐,google 反对),即失去通过 HTML 解码的字符串了。const output = temp.innerText || temp.textContent;
  temp = null;
  return output;
}
正文完
 0