您好,欢迎访问一九零五行业门户网

在 Laravel 5 中使用 Purifier 扩展包集成 HTMLPurifier 防止 XSS 跨站攻击

1、安装 htmlpurifier 是基于 php 编写的富文本html 过滤器,通常我们可以使用它来防止xss 跨站攻击,更多关于 htmlpurifier的详情请参考其官网: http://htmlpurifier.org/ 。purifier 是在laravel 5 中集成 htmlpurifier 的扩展包,我们可以通过 composer 来安装这个扩展包:
composer require mews/purifier
安装完成后,在配置文件 config/app.php 的 providers 中注册htmlpurifier服务提供者:
'providers' => [ // ... mews\purifier\purifierserviceprovider::class,]
然后在 aliases 中注册purifier门面:
'aliases' => [ // ... 'purifier' => mews\purifier\facades\purifier::class,]
2、配置 要使用自定义的配置,发布配置文件到 config 目录:
php artisan vendor:publish
这样会在 config 目录下生成一个 purifier.php 文件:
return [ 'encoding' => 'utf-8', 'finalize' => true, 'preload' => false, 'cachepath' => null, 'settings' => [ 'default' => [ 'html.doctype' => 'xhtml 1.0 strict', 'html.allowed' => 'div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]', 'css.allowedproperties' => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align', 'autoformat.autoparagraph' => true, 'autoformat.removeempty' => true ], 'test' => [ 'attr.enableid' => true ], youtube => [ html.safeiframe => 'true', uri.safeiframeregexp => %^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%, ], ],];
3、使用示例 可以使用辅助函数 clean :
clean(input::get('inputname'));
或者使用 purifier 门面提供的 clean 方法:
purifier::clean(input::get('inputname'));
还可以在应用中进行动态配置:
clean('this is my h1 title', 'titles');clean('this is my h1 title', array('attr.enableid' => true));
或者你也可以使用 purifier 门面提供的方法:
purifier::clean('this is my h1 title', 'titles');purifier::clean('this is my h1 title', array('attr.enableid' => true));
其它类似信息

推荐信息