【问题标题】:How do I allow images in the HTMLPurifier?如何在 HTMLPurifier 中允许图像?
【发布时间】:2023-11-29 22:04:01
【问题描述】:

我想在我的 HTML Purifier 过滤器中允许图像。不幸的是,它们仍在被过滤。这段代码有什么问题?

$config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
$config->set('URI.DisableExternalResources', false);
$config->set('URI.DisableResources', false);
$config->set('HTML.Allowed', 'u,p,b,i,span[style],p,strong,em,li,ul,ol,div[align],br,img');

感谢您的帮助。

【问题讨论】:

  • 我们能看到一些您尝试使用的 HTML 示例吗?您是否尝试从外部站点提取图像?请提供更多信息。

标签: php html frameworks xss htmlpurifier


【解决方案1】:

要接受 b64 图像,您必须添加新的 URIScheme:

 if(!class_exists("HTMLPurifier_URIScheme_data")){
        class HTMLPurifier_URIScheme_data extends HTMLPurifier_URIScheme {

            public $default_port = null;
            public $browsable = false;
            public $hierarchical = true;

            public function validate(&$uri, $config, $context) {
                return true;
            }

        }
    }

HTMLPurifier_URISchemeRegistry::instance()->register("data", new HTMLPurifier_URIScheme_data());

来源:http://htmlpurifier.org/phorum/read.php?3,4316,4318,quote=1

编写此代码的人“Nick”说必须设置:

$browsable = true;
$hierarchical = false;

但就我而言,我没有。

【讨论】:

    【解决方案2】:

    HTML Purifier 在src=" 之后搜索http://https://,这在发布数据(inline src="data:image/png;base64,....)或您的链接时可能会丢失。希望这会起作用对你也一样,因为它对我有用。

    【讨论】:

      【解决方案3】:

      将这行代码添加到您的代码中应该可以解决问题:

      $config->set('HTML.AllowedAttributes', 'src, height, width, alt');
      

      【讨论】:

        【解决方案4】:

        您需要允许 src 和 alt 属性。如果您未能允许元素的必需属性,HTML Purifier 可能会警告您/

        【讨论】: