React防止xss攻击的方法:react在渲染html内容和渲染dom属性时都会将 "'&<>这几个字符进行转义,转义部分源码如下:for (index = match.
React防止xss攻击的方法:
react在渲染html内容和渲染dom属性时都会将 "'&<>这几个字符进行转义,转义部分源码如下:
for (index = match.index; index < str.length; index++) {
switch (str.charCodeAt(index)) {
case 34: // "
escape = '"';
break;
case 38: // &
escape = '&';
break;
case 39: // '
escape = ''';
break;
case 60: // <
escape = '<';
break;
case 62: // >
escape = '>';
break;
default:
continue;
}
}
使用以上方法,恶意代码在渲染到html前都被转成了字符串,例如:
// 一段恶意代码
<img src="empty.png" onerror ="alert('xss')">
// 转义后输出到 html 中
<img src="empty.png" onerror ="alert('xss')">
--结束END--
本文标题: react如何防止xss攻击
本文链接: https://www.lsjlt.com/news/116056.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0