Laravel5中防止XSS跨站攻擊的方法
本文實(shí)例講述了Laravel5中防止XSS跨站攻擊的方法。分享給大家供大家參考,具體如下:
Laravel 5本身沒有這個(gè)能力來防止xss跨站攻擊了,但是這它可以使用Purifier 擴(kuò)展包集成 HTMLPurifier 防止 XSS 跨站攻擊。
1、安裝
HTMLPurifier 是基于 PHP 編寫的富文本 HTML 過濾器,通常我們可以使用它來防止 XSS 跨站攻擊,更多關(guān)于 HTMLPurifier的詳情請(qǐng)參考其官網(wǎng):http://htmlpurifier.org/。Purifier 是在 Laravel 5 中集成 HTMLPurifier 的擴(kuò)展包,我們可以通過 Composer 來安裝這個(gè)擴(kuò)展包:
composer require mews/purifier
安裝完成后,在配置文件config/app.php的providers中注冊(cè)HTMLPurifier服務(wù)提供者:
'providers' => [ // ... Mews\Purifier\PurifierServiceProvider::class, ] 然后在aliases中注冊(cè)Purifier門面: '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門面提供的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門面提供的方法:
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框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- 詳解Laravel5.6 Passport實(shí)現(xiàn)Api接口認(rèn)證
- 在Laravel5.6中使用Swoole的協(xié)程數(shù)據(jù)庫查詢
- 淺析Laravel5中隊(duì)列的配置及使用
- Laravel5框架自定義錯(cuò)誤頁面配置操作示例
- Laravel5中Cookie的使用詳解
- Laravel5中contracts詳解
- Laravel5權(quán)限管理方法詳解
- laravel5.4+vue+element簡單搭建的示例代碼
- CKeditor4 字體顏色功能配置方法教程
- 手把手教你 CKEDITOR 4 擴(kuò)展插件制作
- CKEditor 4.4.1 添加代碼高亮顯示插件功能教程【使用官方推薦Code Snippet插件】
- Laravel5.6框架使用CKEditor5相關(guān)配置詳解
相關(guān)文章
PHP遞歸獲取目錄內(nèi)所有文件的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狿HP遞歸獲取目錄內(nèi)所有文件的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11在laravel中使用Symfony的Crawler組件分析HTML
這篇文章主要介紹了在laravel中使用Symfony的Crawler組件分析HTML,需要的朋友可以參考下2017-06-06

2個(gè)Codeigniter文件批量上傳控制器寫法例子

PHP explode()函數(shù)的幾個(gè)應(yīng)用和implode()函數(shù)有什么區(qū)別

PHP利用Cookie設(shè)置用戶30分鐘未操作自動(dòng)退出功能

laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn)

laravel withCount 統(tǒng)計(jì)關(guān)聯(lián)數(shù)量的方法