laravel使用{!!content!!}的时候要怎么防止xss攻击?

查看:170
本文介绍了laravel使用{!!content!!}的时候要怎么防止xss攻击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

一般使用{{$article->title}}
但是有时有格式,所以需要这种:
{!!$article->body!!}
但是这种会导致xss攻击,要怎么处理一下防止xss攻击?

解决方案

推荐使用Purifier进行过滤一下,可以配置过滤规则。

'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/)%",
    ],
],

使用方法

{!! clean($article->body) !!}

这篇关于laravel使用{!!content!!}的时候要怎么防止xss攻击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆