Laravel5中防止XSS跨站攻击的方法


Posted in PHP onOctober 10, 2016

本文实例讲述了Laravel5中防止XSS跨站攻击的方法。分享给大家供大家参考,具体如下:

Laravel 5本身没有这个能力来防止xss跨站攻击了,但是这它可以使用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));

php防止xss攻击

<?PHP
function clean_xss(&$string, $low = False)
{
 if (! is_array ( $string ))
 {
 $string = trim ( $string );
 $string = strip_tags ( $string );
 $string = htmlspecialchars ( $string );
 if ($low)
 {
 return True;
 }
 $string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string );
 $no = '/%0[0-8bcef]/';
 $string = preg_replace ( $no, '', $string );
 $no = '/%1[0-9a-f]/';
 $string = preg_replace ( $no, '', $string );
 $no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
 $string = preg_replace ( $no, '', $string );
 return True;
 }
 $keys = array_keys ( $string );
 foreach ( $keys as $key )
 {
 clean_xss ( $string [$key] );
 }
}
//just a test
$str = '3water.com<meta http-equiv="refresh" content="0;">';
clean_xss($str); //如果你把这个注释掉,你就知道xss攻击的厉害了
echo $str;
?>

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

PHP 相关文章推荐
PHP 中的类
Oct 09 PHP
php中文本数据翻页(留言本翻页)
Oct 09 PHP
php is_file 判断给定文件名是否为一个正常的文件
May 10 PHP
php 判断网页是否是utf8编码的方法
Jun 06 PHP
php输入流php://input使用浅析
Sep 02 PHP
php实现每天自动变换随机问候语的方法
May 12 PHP
CodeIgniter钩子用法实例详解
Jan 20 PHP
PHP实现的mysql操作类【MySQL与MySQLi方式】
Oct 07 PHP
PHP count()函数讲解
Feb 03 PHP
php-fpm超时时间设置request_terminate_timeout资源问题分析
Sep 27 PHP
laravel Task Scheduling(任务调度)在windows下的使用详解
Oct 22 PHP
CI框架简单分页类用法示例
Jun 06 PHP
php中让人头疼的浮点数运算分析
Oct 10 #PHP
Laravel实现自定义错误输出内容的方法
Oct 10 #PHP
PHP定时任务获取微信access_token的方法
Oct 10 #PHP
php使用SAE原生Mail类实现各种类型邮件发送的方法
Oct 10 #PHP
PHP简单数据库操作类实例【支持增删改查及链式操作】
Oct 10 #PHP
Ajax实现对静态页面的文章访问统计功能示例
Oct 10 #PHP
PhpStorm terminal无法输入命令的解决方法
Oct 09 #PHP
You might like
PH P5.2至5.5、5.6的新增功能详解
2014/07/14 PHP
PHP中soap的用法实例
2014/10/24 PHP
腾讯CMEM的PHP扩展编译安装方法
2015/09/25 PHP
PHP结合jQuery插件ajaxFileUpload实现异步上传文件实例
2020/08/17 PHP
PHPMailer ThinkPHP实现自动发送邮件功能
2018/06/10 PHP
基于PHP实现用户在线状态检测
2020/11/10 PHP
5款Javascript颜色选择器
2009/10/25 Javascript
jQuery插件实现多级联动菜单效果
2015/12/01 Javascript
jQuery实现产品对比功能附源码下载
2016/08/09 Javascript
微信小程序 WXDropDownMenu组件详解及实例代码
2016/10/24 Javascript
微信小程序 UI布局常用技巧整理总结
2016/12/05 Javascript
Angular组件化管理实现方法分析
2017/03/17 Javascript
JS实现加载和读取XML文件的方法详解
2017/04/24 Javascript
使用puppeteer破解极验的滑动验证码
2018/02/24 Javascript
使用Vue实现图片上传的三种方式
2018/07/17 Javascript
小程序视频或音频自定义可拖拽进度条的示例代码
2018/09/30 Javascript
使用jquery的cookie实现登录页记住用户名和密码的方法
2019/03/13 jQuery
详解React项目如何修改打包地址(编译输出文件地址)
2019/03/21 Javascript
Angular8路由守卫原理和使用方法
2019/08/29 Javascript
vue实现匀速轮播效果
2020/06/29 Javascript
vue组件中实现嵌套子组件案例
2020/08/31 Javascript
vue使用echarts图表自适应的几种解决方案
2020/12/04 Vue.js
[55:04]海涛DOTA2死魂复燃6.82版本介绍
2014/09/28 DOTA
Python处理Excel文件实例代码
2017/06/20 Python
django开发教程之利用缓存文件进行页面缓存的方法
2017/11/10 Python
基于Python的文件类型和字符串详解
2017/12/21 Python
Python查看微信撤回消息代码
2018/06/07 Python
Python实现的拉格朗日插值法示例
2019/01/08 Python
解决pyshp UnicodeDecodeError的问题
2019/12/06 Python
阿拉伯时尚购物网站:Nisnass
2021/02/07 全球购物
上课打牌的检讨书
2014/02/15 职场文书
高中语文课后反思
2014/04/27 职场文书
中学生清明节演讲稿
2015/03/18 职场文书
结婚典礼主持词
2015/06/29 职场文书
董事长新年致辞
2015/07/29 职场文书
mysql如何查询连续记录
2022/05/11 MySQL