react如何防止xss攻击
react防止xss攻击的方法:
react在渲染html内容和渲染dom属性时都会将 "'&<>这几个字符进行转义,转义部分源码如下:
for(index=match.index;index<str.length;index++){
switch(str.charCodeAt(index)){
case34://"
escape='"';
break;
case38://&
escape='&';
break;
case39://'
escape=''';
break;
case60://<
escape='<';
break;
case62://>
escape='>';
break;
default:
continue;
}
}
使用以上方法,恶意代码在渲染到html前都被转成了字符串,例如:
//一段恶意代码
<imgsrc="empty.png"onerror="alert('xss')">
//转义后输出到html中
<imgsrc="empty.png"onerror="alert('xss')">
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:CentOS中怎么设置硬盘I/O调度器来优化性能 下一篇:ChatGPT需要付费吗
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。