tp6默认转义script脚本
然后,主要讲解 XSS攻击,为以下内容:
composer下载:
composer require ezyang/htmlpurifier
此方法放入common里,作为公共函数,随时调用,用来过滤信息
//过滤 xss
if (!function_exists('remove_xss')) {
//使用htmlpurifier防范xss攻击
function remove_xss($string){
//相对index.php入口文件,引入HTMLPurifier.auto.php核心文件
//require_once './plugins/htmlpurifier/HTMLPurifier.auto.php';
// 生成配置对象
$cfg = HTMLPurifier_Config::createDefault();
// 以下就是配置:
$cfg -> set('Core.Encoding', 'UTF-8');
// 设置允许使用的HTML标签
$cfg -> set('HTML.Allowed','div,b,strong,i,em,a[href|title],ul,ol,li,br,p[style],span[style],img[width|height|alt|src]');
// 设置允许出现的CSS样式属性
$cfg -> set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align');
// 设置a标签上是否允许使用target="_blank"
$cfg -> set('HTML.TargetBlank', TRUE);
// 使用配置生成过滤用的对象
$obj = new HTMLPurifier($cfg);
// 过滤字符串
return $obj -> purify($string);
}
一、控制器方法里写入以下代码,进行过滤:
// 参数一为要接收的值,参数二为默认值,参数三为方法名
$data = request()->post('','','remove_xss');
dd($data);
二、在app/request文件写入(运用request依赖注入):
protected $filter=['htmlspecialchars']; // 转义
protected $filter=['remove_xss']; // 过滤