0
使用HTML Purifier 过滤HTML 防范 XSS攻击
最近使用Parsedown转换Markdown语法为HTML, 但是该包不会转义和过滤XSS, 因为Markdown语法里面只支持直接写HTML的。所以只能找其他的方法实现。
HTML Purifier 很多富文本编辑器都是使用它来过滤XSS。除了过滤恶意代码外,还支持下面的一些特性。
- 闭合HTML TAG,比如你只输入了一个
<a href="#hash">
会自动给你补全为<a href="#hash"></a>
- 清除不需要的标签和属性。
- 过滤iframe加载URL
- 删除无用的空TAG
使用
<?php
require_once '/path/to/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($dirty_html);
项目地址: https://github.com/ezyang/htmlpurifier
Laravel 封装
Laravel封装的版本。更易用一些。 项目地址:https://github.com/mewebstudio/Purifier
只用像下面这样配置即可
//config/purifier.php
return [
'encoding' => 'UTF-8',
'finalize' => true,
'cachePath' => storage_path('app/purifier'),
'cacheFileMode' => 0755,
'settings' => [
'default' => [
'HTML.Doctype' => 'HTML 4.01 Transitional',
'HTML.Allowed' => 'div,b,strong,i,em,u,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/)%",
],
'custom_definition' => [
'id' => 'html5-definitions',
'rev' => 1,
'debug' => false,
'elements' => [
// http://developers.whatwg.org/sections.html
['section', 'Block', 'Flow', 'Common'],
['nav', 'Block', 'Flow', 'Common'],
['article', 'Block', 'Flow', 'Common'],
['aside', 'Block', 'Flow', 'Common'],
['header', 'Block', 'Flow', 'Common'],
['footer', 'Block', 'Flow', 'Common'],
// Content model actually excludes several tags, not modelled here
['address', 'Block', 'Flow', 'Common'],
['hgroup', 'Block', 'Required: h1 | h2 | h3 | h4 | h5 | h6', 'Common'],
// http://developers.whatwg.org/grouping-content.html
['figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common'],
['figcaption', 'Inline', 'Flow', 'Common'],
// http://developers.whatwg.org/the-video-element.html#the-video-element
['video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
'src' => 'URI',
'type' => 'Text',
'width' => 'Length',
'height' => 'Length',
'poster' => 'URI',
'preload' => 'Enum#auto,metadata,none',
'controls' => 'Bool',
]],
['source', 'Block', 'Flow', 'Common', [
'src' => 'URI',
'type' => 'Text',
]],
// http://developers.whatwg.org/text-level-semantics.html
['s', 'Inline', 'Inline', 'Common'],
['var', 'Inline', 'Inline', 'Common'],
['sub', 'Inline', 'Inline', 'Common'],
['sup', 'Inline', 'Inline', 'Common'],
['mark', 'Inline', 'Inline', 'Common'],
['wbr', 'Inline', 'Empty', 'Core'],
// http://developers.whatwg.org/edits.html
['ins', 'Block', 'Flow', 'Common', ['cite' => 'URI', 'datetime' => 'CDATA']],
['del', 'Block', 'Flow', 'Common', ['cite' => 'URI', 'datetime' => 'CDATA']],
],
'attributes' => [
['iframe', 'allowfullscreen', 'Bool'],
['table', 'height', 'Text'],
['td', 'border', 'Text'],
['th', 'border', 'Text'],
['tr', 'width', 'Text'],
['tr', 'height', 'Text'],
['tr', 'border', 'Text'],
],
],
'custom_attributes' => [
['a', 'target', 'Enum#_blank,_self,_target,_top'],
],
'custom_elements' => [
['u', 'Inline', 'Inline', 'Common'],
],
],
];
回复
Recommend
Recent search keywords
- bead
- plus de 8000 chaines
- 回来找我
- inspector calls
- Louisiana
- Self-enabling
- flexibility
- Infrastructure
- United Arab Emirates
- repurpose
- El Salvador Colon US Dollar
- Mountains
- Switzerland
- indexing
- Ergonomic
- e-markets
- Administrator
- Sausages
- Program
- engineer
- Bahrain
- cross-media
- product
- holistic
- Ports
- Open-architected
- Synergistic
- Automotive
- Moroccan Dirham
- Generic Steel Salad
- communities
- algorithm
- Kina
- reboot
- Future
- SMTP
- Lake
- Cotton
- Clothing
- monetize
- revolutionize
- purple
- Generic
- Tirol
- Applications
- architecture
- Customizable
- portals
- Rubber
- Integrated
- Total
- Credit Card Account
- Avon
- User-friendly
- Angola
- schemas
- US Dollar
- clicks-and-mortar
- Pants
- Grnit
- Intelligent
- multi-byte
- Filippinerna
- online
- -1" or 3+860-860-1=0+0+0+1 --
- answers
- @@l00ww
- iecho.cc
- golbel
- 129084
- cssbuilder
- DOVE加速器
- Graphcore
- n950f
- eb341820cd3a3485461a61b1e97d31b1
- onepiece
- Samsung gaming
- contact
- hindi
- golang9478590
- http\x3A\x2F\x2Fbestbuy.com
- 哥们
- 黑兽
- -4307" or 5781=5781 and "yvut"="yvut
- yiyo
- Inda
- -1" or 2+501-501-1=0+0+0+1 --
- upi
- 印度支付通道
- AC8227L+Android+head+unit+4gb/32gb
- xperia+5+v
- org+chart
- Iptv+2023
- san+francisco+web+development
- 8257+logo
- hacks+in+blooket
- 深入理解+Gem5
- xperia+5+v+kernel
- hacks in blooket
- Iptv 2023