XSS XSS攻击

react如何防止xss攻击

小新
249
2020-12-23 15:02:51
栏目: 网络安全

react如何防止xss攻击

react防止xss攻击的方法:

react在渲染html内容和渲染dom属性时都会将 "'&<>这几个字符进行转义,转义部分源码如下:

for (index = match.index; index < str.length; index++) {

  switch (str.charCodeAt(index)) {

    case 34: // "

      escape = '&quot;';

      break;

    case 38: // &

      escape = '&amp;';

      break;

    case 39: // '

      escape = '&#x27;';

      break;

    case 60: // <

      escape = '&lt;';

      break;

    case 62: // >

      escape = '&gt;';

      break;

    default:

      continue;

    }

  }

使用以上方法,恶意代码在渲染到html前都被转成了字符串,例如:

// 一段恶意代码

<img src="empty.png" onerror ="alert('xss')"> 

// 转义后输出到 html 中

&lt;img src=&quot;empty.png&quot; onerror =&quot;alert(&#x27;xss&#x27;)&quot;&gt;

0
看了该问题的人还看了