欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年10月10日 14:29:47   作者:swteen  
這篇文章主要介紹了Laravel5中防止XSS跨站攻擊的方法,結(jié)合實(shí)例形式分析了Laravel5基于Purifier擴(kuò)展包集成HTMLPurifier防止XSS跨站攻擊的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Laravel5中防止XSS跨站攻擊的方法。分享給大家供大家參考,具體如下:

Laravel 5本身沒(méi)有這個(gè)能力來(lái)防止xss跨站攻擊了,但是這它可以使用Purifier 擴(kuò)展包集成 HTMLPurifier 防止 XSS 跨站攻擊。

1、安裝

HTMLPurifier 是基于 PHP 編寫(xiě)的富文本 HTML 過(guò)濾器,通常我們可以使用它來(lái)防止 XSS 跨站攻擊,更多關(guān)于 HTMLPurifier的詳情請(qǐng)參考其官網(wǎng):http://htmlpurifier.org/。Purifier 是在 Laravel 5 中集成 HTMLPurifier 的擴(kuò)展包,我們可以通過(guò) Composer 來(lái)安裝這個(gè)擴(kuò)展包:

composer require mews/purifier

安裝完成后,在配置文件config/app.php的providers中注冊(cè)HTMLPurifier服務(wù)提供者:

'providers' => [
 // ...
 Mews\Purifier\PurifierServiceProvider::class,
]
然后在aliases中注冊(cè)Purifier門(mén)面:
'aliases' => [
 // ...
 'Purifier' => Mews\Purifier\Facades\Purifier::class,
]

2、配置

要使用自定義的配置,發(fā)布配置文件到config目錄:

php artisan vendor:publish

這樣會(huì)在config目錄下生成一個(gè)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、使用示例

可以使用輔助函數(shù)clean:

clean(Input::get('inputname'));

或者使用Purifier門(mén)面提供的clean方法:

Purifier::clean(Input::get('inputname'));

還可以在應(yīng)用中進(jìn)行動(dòng)態(tài)配置:

clean('This is my H1 title', 'titles');
clean('This is my H1 title', array('Attr.EnableID' => true));

或者你也可以使用Purifier門(mén)面提供的方法:

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 ('"', "\\", "'", "/", "..", "../", "./", "http://" ), '', $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 = 'jb51.net<meta http-equiv="refresh" content="0;">';
clean_xss($str); //如果你把這個(gè)注釋掉,你就知道xss攻擊的厲害了
echo $str;
?>

更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門(mén)與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門(mén)基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論