国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

Laravel 5 中防止 XSS 跨站攻擊的例子

2024-05-04 21:50:24
字體:
來源:轉載
供稿:網友

本文實例講述了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服務提供者:

  1. 'providers' => [ 
  2.  // ... 
  3.  MewsPurifierPurifierServiceProvider::class

然后在aliases中注冊Purifier門面:

  1. 'aliases' => [ 
  2.  // ... 
  3.  'Purifier' => MewsPurifierFacadesPurifier::class

2、配置

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

php artisan vendor:publish

這樣會在config目錄下生成一個purifier.php文件:

  1. return [ 
  2.  'encoding' => 'UTF-8'
  3.  'finalize' => true, 
  4.  'preload' => false, 
  5.  'cachePath' => null, 
  6.  'settings' => [ 
  7.   'default' => [ 
  8.    'HTML.Doctype'    => 'XHTML 1.0 Strict'
  9.    'HTML.Allowed'    => 'div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]'
  10.    'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align'
  11.    'AutoFormat.AutoParagraph' => true, 
  12.    'AutoFormat.RemoveEmpty' => true 
  13.   ], 
  14.   'test' => [ 
  15.    'Attr.EnableID' => true 
  16.   ], 
  17.   "youtube" => [ 
  18.    "HTML.SafeIframe" => 'true'
  19.    "URI.SafeIframeRegexp" => "%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%"
  20.   ], 
  21.  ], 
  22. ]; 

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攻擊

  1. <?PHP 
  2. function clean_xss(&$string$low = False) 
  3.  if (! is_array ( $string )) 
  4.  { 
  5.  $string = trim ( $string ); 
  6.  $string = strip_tags ( $string ); 
  7.  $string = htmlspecialchars ( $string ); 
  8.  if ($low
  9.  { 
  10.  return True; 
  11.  } 
  12.  $string = str_replace ( array ('"', "//", "'", "/", "..", "../", "./", "//" ), '', $string ); 
  13.  $no = '/%0[0-8bcef]/'
  14.  $string = preg_replace ( $no''$string ); 
  15.  $no = '/%1[0-9a-f]/'
  16.  $string = preg_replace ( $no''$string ); 
  17.  $no = '/[/x00-/x08/x0B/x0C/x0E-/x1F/x7F]+/S'
  18.  $string = preg_replace ( $no''$string ); 
  19.  return True; 
  20.  } 
  21.  $keys = array_keys ( $string ); 
  22.  foreach ( $keys as $key ) 
  23.  { //Vevb.com 
  24.  clean_xss ( $string [$key] ); 
  25.  } 
  26. //just a test 
  27. $str = 'Vevb.com<meta http-equiv="refresh" content="0;">'
  28. clean_xss($str); //如果你把這個注釋掉,你就知道xss攻擊的厲害了 
  29. echo $str
  30. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹棱县| 板桥市| 鄂州市| 奇台县| 宁阳县| 浦江县| 读书| 和田县| 永善县| 梅州市| 宿迁市| 仁怀市| 辰溪县| 故城县| 土默特左旗| 通州区| 永平县| 左权县| 红河县| 西盟| 大余县| 永济市| 苏尼特右旗| 广元市| 汾西县| 江津市| 额济纳旗| 临高县| 邹平县| 兴安县| 扬中市| 财经| 六安市| 晋城| 周至县| 阜平县| 苏尼特左旗| 明光市| 海安县| 尚志市| 阳谷县|