溫馨提示×

react如何防止xss攻擊

小新
267
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
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女