我想停止文本区域上的跨站点脚本。下面的代码作为输入数据将容易受到文本区域的攻击:
<script>alert("Hello")</script>
任何人都可以帮助防止使用示例代码的跨站点脚本吗?
最佳答案
跨端脚本攻击比你想象的要大得多,而且绝对不限于你提到的代码<script>alert("Hello")</script>
.所以严格限制你的问题,这些类型的 XSS 攻击可以被这么多的 php 库阻止。我将在下面提到一些。
- HTML 净化器
- PHP 反 XSS
- xss保护
- XSS HTML 过滤器
- xss_clean.php 过滤器
- 更新 php 版本
让我们来谈谈防止XSS攻击最突出的方法是Php AntiXss
PHP Anti-XSS Library developing for prevent the XSS(Cross Site Scripting) vulnerabilities on the web applications. PHP Anti-XSS Library automatically detect the encoding of the data that you want filter and if you wish its encoding your data again. Also there are 3 type of filtering option.
这里有3种过滤选项
- 黑名单过滤
- 白名单过滤
- 灰名单过滤
用法:
方法 1: 设置数据的编码
$data = AntiXSS::setEncoding($data, "UTF-8");
方法二:
设置数据过滤器
$data = AntiXSS::setFilter($data, "whitelist", "string");
常用用法:
Xssescape 是一个用于防止跨浏览器中的跨站脚本(xss)攻击的包。
用法:
npm install xssescape
应用程序.js
var xs = require('xssescape')
var htmlStr = "<script> alert(document.cookie); </script>";
xs.strictEscape(htmlStr);
或
var htmlStr = "<script> alert(document.cookie); </script>";
xs.unescape(htmlStr);
或
var browserURL = "http://example.com/?<script> document.cookie </script>";
xs.unSafeUrl('/home'); // eg. you can keep what ever you want as a path name.
// it will reload/refresh the page with /home after the default url.
关于javascript - 如何使用 javascript 防止跨站点脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39635472/