HTMLPurifier iframe Vimeo和Youtube视频

swa*_*er7 12 php xss video iframe htmlpurifier

如何使用HTMLPurifier过滤xss,还允许使用iframe Vimeo和Youtube视频?

require_once 'htmlpurifier/library/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Trusted', true);

$config->set('Filter.YouTube', true);
$config->set('HTML.DefinitionID', '1');
$config->set('HTML.SafeObject', 'true');
$config->set('Output.FlashCompat', 'true');

$config->set('HTML.FlashAllowFullScreen', 'true');

$purifier = new HTMLPurifier($config);
$temp = $purifier->purify($temp);
Run Code Online (Sandbox Code Playgroud)

Mal*_*lte 30

HTMLPurifier版本4.4.0具有允许YouTube和Vimeo iframe的新配置指令:

//allow iframes from trusted sources
$cfg->set('HTML.SafeIframe', true);
$cfg->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%'); //allow YouTube and Vimeo
Run Code Online (Sandbox Code Playgroud)

  • @PanagiotisKoursaris 将其添加到正则表达式中,如下所示 `%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/| www\.facebook\.com/plugins/video.php\?)%` (2认同)

Son*_*nny 9

我刚刚阅读了这篇博客文章,并成功创建并使用了自定义过滤器.我对代码进行了一些更改并添加了Vimeo支持:

/**
 * Based on: http://sachachua.com/blog/2011/08/drupal-html-purifier-embedding-iframes-youtube/
 * Iframe filter that does some primitive whitelisting in a somewhat recognizable and tweakable way
 */
class HTMLPurifier_Filter_MyIframe extends HTMLPurifier_Filter
{
    public $name = 'MyIframe';

    /**
     *
     * @param string $html
     * @param HTMLPurifier_Config $config
     * @param HTMLPurifier_Context $context
     * @return string
     */
    public function preFilter($html, HTMLPurifier_Config $config, HTMLPurifier_Context $context)
    {
        $html = preg_replace('#<iframe#i', '<img class="MyIframe"', $html);
        $html = preg_replace('#</iframe>#i', '</img>', $html);
        return $html;
    }

    /**
     *
     * @param string $html
     * @param HTMLPurifier_Config $config
     * @param HTMLPurifier_Context $context
     * @return string
     */
    public function postFilter($html, HTMLPurifier_Config $config, HTMLPurifier_Context $context)
    {
        $post_regex = '#<img class="MyIframe"([^>]+?)>#';
        return preg_replace_callback($post_regex, array($this, 'postFilterCallback'), $html);
    }

    /**
     *
     * @param array $matches
     * @return string
     */
    protected function postFilterCallback($matches)
    {
        // Domain Whitelist
        $youTubeMatch = preg_match('#src="https?://www.youtube(-nocookie)?.com/#i', $matches[1]);
        $vimeoMatch = preg_match('#src="http://player.vimeo.com/#i', $matches[1]);
        if ($youTubeMatch || $vimeoMatch) {
            $extra = ' frameborder="0"';
            if ($youTubeMatch) {
                $extra .= ' allowfullscreen';
            } elseif ($vimeoMatch) {
                $extra .= ' webkitAllowFullScreen mozallowfullscreen allowFullScreen';
            }
            return '<iframe ' . $matches[1] . $extra . '></iframe>';
        } else {
            return '';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将过滤器添加到HTML Purifier配置中

$config->set('Filter.Custom', array(new HTMLPurifier_Filter_MyIframe()));
Run Code Online (Sandbox Code Playgroud)


sum*_*eet 9

对于任何正在苦苦挣扎的人(如何启用 iframe 和 allowfullscreen)

    $config = \HTMLPurifier_Config::createDefault();
    $config->set('HTML.SafeIframe', true);
    $config->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%'); //allow YouTube and Vimeo
    // This line is important allow iframe in allowed elements or it will not work    
    $config->set('HTML.AllowedElements', array('iframe'));// <-- IMPORTANT
    $config->set('HTML.AllowedAttributes','iframe@src,iframe@allowfullscreen');

    $def = $config->getHTMLDefinition(true);
    $def->addAttribute('iframe', 'allowfullscreen', 'Bool');

    $purifier = new \HTMLPurifier($config);
    $purifiedHtml = $purifier->purify($html);
Run Code Online (Sandbox Code Playgroud)


Edw*_*ang 0

删除 %HTML.Trusted、%Filter.YouTube 和 %HTML.DefinitionID。他们可能与 SafeObject/FlashCompat 交互不佳。