我正在使用HTMLPurifier清理HTML字符串(这与安全性有关)。

调用HTMLPurifier时,将删除某些属性(如widthheight)。我不认为这是安全问题。

如何在不重新定义白名单的情况下添加此属性?

我搜索了Stackoverflow和HTMLPurifier文档,但唯一的解决方案似乎是:

$config->set('HTML.Allowed', 'p,b,a[href],i');

但这不是解决方案,因为我不想重新定义白名单(我信任默认的HTMLPurifier配置,我只想添加一个异常(exception))。

最佳答案

这段代码:

<?php

require('purifier/library/HTMLPurifier.auto.php');

$html = "<img width='200' height='200' src='test.jpg' alt='bla>";
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
echo $purifier->purify($html) . "\n";

$html = "<table width='100'><tr><td>test</td></tr></table>";
echo $purifier->purify($html) . "\n";

?>

产生以下输出:
<img width="200" height="200" src="test.jpg" alt="bla" />
<table width="100"><tr><td>test</td></tr></table>

使用php 5.3.10和HTMLPurifier版本4.4.0。因此,默认情况下不会剥离这些属性(我使用的是HTMLPurifier的全新安装)

您在哪个HTML元素上使用width/height属性?

另请注意,使用xhtml strict时,无效的属性将被删除。据我所知,img和table元素的宽度和高度是允许的,但应小写。除了图像元素上的“width ='100%'”(在rap-2-h他的评论后添加为完整起见)

通常,使用addAttribute而不是白名单来添加允许的属性。

关于php - HTMLPurifier : How to allow a single attribute without redefining the whole whitelist,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11308739/

10-09 12:58