Php 使用HTMLPurifier允许数据-*属性

Php 使用HTMLPurifier允许数据-*属性,php,htmlpurifier,Php,Htmlpurifier,目前,我将此代码与HTMLPurifier一起使用,以允许data-*HTML标记属性: $def = $config->getHTMLDefinition(true); $def->addAttribute('div', 'data-aaa', 'Text'); $def->addAttribute('div', 'data-bbb', 'Text'); // ... 是否有一种方法可以同时允许所有数据-*属性,最好是在所有HTML标记上?(

目前,我将此代码与HTMLPurifier一起使用,以允许
data-*
HTML标记属性:

    $def = $config->getHTMLDefinition(true);
    $def->addAttribute('div', 'data-aaa', 'Text');
    $def->addAttribute('div', 'data-bbb', 'Text');
    // ...

是否有一种方法可以同时允许所有
数据-*
属性,最好是在所有HTML标记上?(就我所知,这不是一个安全问题)

不,如果不修改验证属性策略,这是不可能的。

这不是一个完整的解决方案,但我能够用下面的代码将单个
数据-
属性全局白名单,允许将它们放置在任何元素上,而不必逐项列出每个属性的每个元素类型

$def = $config->getHTMLDefinition(true);
$def->info_global_attr['data-aaa-xxx'] = new HTMLPurifier_AttrDef_Text;
$def->info_global_attr['data-bbb-yyy'] = new HTMLPurifier_AttrDef_Text;
$def->info_global_attr['data-ccc-zzz'] = new HTMLPurifier_AttrDef_Text;

这段代码可以改进,但我修改了attrvalidater.php 我添加了以下功能:

    /*=======================================
    ==--    LLS start wildcard handling
    ==--
    ==--    data-*          ^data-(((?![\s=]).)+)
    =========================================*/
    private function checkWildCardAttributes($deflist, $attr_key, $value, $config, $context) {
        $result = false;
        foreach ($deflist as $def_key => $def_value) {
            if (strpos($def_key, '*') !== FALSE) {
                // found a wildcard
                // does wildcard match this attr
                $re = implode('(((?![\s=]).)+)',explode("*",$def_key));
                preg_match('#^'.$re.'#',$attr_key,$wcout);
                if (count($wcout)>0) {
                    // the attribute matched against the wildcard definition
                    $result = $deflist[$attr_key]->validate(
                        $value,
                        $config,
                        $context
                    );
                    break;
                }
            }
        }
        return $result;
    }

在函数validateToken中,找到以下行:

// put the results into effect
在此行之前添加以下内容:

                /*=======================================
                ==--    start wildcard handling
                =========================================*/
                if (!$result) {
                    // definitions
                    $result = $this->checkWildCardAttributes($defs, $attr_key, $value, $config, $context);
                    if (!$result) {
                        // global definitions
                        $result = $this->checkWildCardAttributes($d_defs, $attr_key, $value, $config, $context);
                    }   
                }   
                //=======================================


            // put the results into effect
            if ($result === false || $result === null) {
在此之后,可以在属性定义中使用*通配符。 例如:

就像我说的,它可以改进。。。但它确实起到了作用:)


玩得开心….

你知道怎么做吗?我试过查看代码,但找不到一个可以执行此操作的位置…换句话说,是否有一个
isValidAttribute()
方法可以覆盖?不,问题是“isValidAttribute”是使用哈希查找执行的,因此需要替换整个数据结构。是否考虑将其用于实现?可能非常有用1个错误:$result=$deflist[$attr\u key]->validate(应该是:$result=$deflist[$def\u key]->validate(
    // See: AttrValidator.php in the HTMLPurifier for the wildcard addition
    $def->info_global_attr['data-*'] = new HTMLPurifier_AttrDef_Text;