您的当前位置:首页>全部文章>文章详情

使用HTML Purifier在thinkphp6中过滤富文本&防止XSS攻击

发表于:2023-05-09 01:04:37浏览:512次TAG: #ThinkPHP #PHP #XSS #HTMLPurifier

XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中。比如这些代码包括HTML代码和客户端脚本。那么如何防止XSS?
这里推荐使用HTML Purifier的方式的过滤掉,比如编辑框里的文本是1 2 3 ,然后过滤之后,存进数据库的是1 2 3,所以访问文章就不会有提示框弹出啦。
HTML Purfiler怎么用?
官方文档:http://htmlpurifier.org/live/configdoc/plain.html

1、要在ThinkPHP6中使用HTMLPurifier进行输入过滤,需要先安装HTMLPurifier库。可以通过Composer来安装:

composer require ezyang/htmlpurifier

2、安装完成后,可以在控制器中使用以下代码对输入进行过滤:

use HTMLPurifier;
use HTMLPurifier_Config;

// 创建HTMLPurifier配置对象
$config = HTMLPurifier_Config::createDefault();

// 添加允许的HTML标签和属性
$config->set('HTML.Allowed', 'p,b,a[href]');

// 创建HTMLPurifier对象
$purifier = new HTMLPurifier($config);

// 过滤输入并输出结果
$input = $purifier->purify(input('param.name'));
echo $input;

在上面的示例中,添加了三个允许的HTML标签(p、b、a)以及a标签的href属性。任何不符合这些规则的标签或属性都将被过滤掉。
PS:官方有很多不同的配置,可以去官方文档里面深扒,我这里只列举一些常用的:

<?php

use HTMLPurifier;
use HTMLPurifier_Config;

// 创建HTMLPurifier配置对象
$config = HTMLPurifier_Config::createDefault();

//$config->set('Attr.EnableID', true); // 允许使用id
//$config->set('Attr.IDPrefix', 'test_'); // 给所有id加上前缀test_
//$config->set('Attr.AllowedClasses', array('test_by_willko'));// 设置允许使用的class名
//$config->set('Attr.ForbiddenClasses', array('ignore'));// 设置拒绝使用的class名

//$config->set('HTML.AllowedElements',array('tr','div','h1'),true);//设置允许的tagname
//$config->set('HTML.ForbiddenElements',array('div'),true);//设置拒绝使用的tagname
//$config->set('HTML.Allowed','div'); //设置允许的标签名
//$config->set('HTML.SafeScripting',array(''));//第二个参数是什么好像结果都是一样的...

//$config->set('CSS.AllowedProperties',array('width'),true);//设置允许的CSS属性
//$config->set('AutoFormat.RemoveEmpty', true);  // 清除空标签

$purifier = new HTMLPurifier($config);

$echo_html = "<h1 id='haha'>123</h1><p style='width:200px;height:200px;'>321</p><script>alert(123)</script>";
$echo_Html = $purifier->purify($echo_html);
echo "处理前<br/>";
echo $dirty_html;
echo "处理后<br/>";
echo $clean_Html;
?>

以下是HTMLPurifier增加HTML5标签不过滤,过滤script标签的示例:

$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.DefinitionID', 'html5-definitions');
$config->set('HTML.DefinitionRev', 1);
$config->set('HTML.ForbiddenAttributes', ['width', 'height']);
$config->set('HTML.ForbiddenElements',array('script'),true);//设置拒绝使用的tagname
if ($def = $config->maybeGetRawHTMLDefinition()) {
    $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
        'src' => 'URI',
        'type' => 'Text',
        'poster' => 'URI',
        'preload' => 'Enum#auto,metadata,none',
        'controls' => 'Bool',
        ]);
    $def->addElement('source', 'Block', 'Flow', 'Common', [
        'src' => 'URI',
        'type' => 'Text',
    ]);
    $def->addElement('section', 'Block', 'Flow', 'Common');
    $def->addElement('nav', 'Block', 'Flow', 'Common');
    $def->addElement('article', 'Block', 'Flow', 'Common');
    $def->addElement('aside', 'Block', 'Flow', 'Common');
    $def->addElement('header', 'Block', 'Flow', 'Common');
    $def->addElement('footer', 'Block', 'Flow', 'Common');
    $def->addElement('main', 'Block', 'Flow', 'Common');
}        
// 创建HTMLPurifier对象
$purifier = new HTMLPurifier($config);
//防止xss,过滤输入并输出结果
//$param['content'] = '测试<script>alert(0);</script>';
$param['content'] = $purifier->purify($param['content']);