laravel使用{!!content!!}的時候要怎麼防止xss攻擊?
習慣沉默
習慣沉默 2017-05-16 16:48:38
0
1
615

一般使用{{$article->title}}
但是有時有格式,所以需要這種:
{!!$article->body !!}
但是這種會導致xss攻擊,要怎麼處理一下防止xss攻擊?

習慣沉默
習慣沉默

全部回覆(1)
黄舟

推薦使用Purifier進行過濾一下,可以設定過濾規​​則。

'encoding' => 'UTF-8',
'finalize' => true,
'preload'  => false,
'cachePath' => null,
'settings' => [
    'default' => [
        'HTML.Doctype'             => 'XHTML 1.0 Strict',
        'HTML.Allowed'             => 'p,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/)%",
    ],
],

使用方法

{!! clean($article->body) !!}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!