【问题标题】:HTMLPurifier allow class attributeHTMLPurifier 允许类属性
【发布时间】:2012-07-31 19:51:59
【问题描述】:

如何在 HTMLPurifier 中允许“类”?我正在尝试净化这个:

 <div class="txt_r" id="test">Blah</div>

我得到:

 <div id="test">Blah</div>

为什么课堂消失了?我正在使用下一个配置:

 $config->set('Attr.EnableID', true);
 $config->set('CSS.Trusted', true);
 $config->set('HTML.AllowedAttributes', 'style, src, class');

【问题讨论】:

  • 你为什么使用 CSS.Trusted?
  • 我不确定,是否启用内联 css?
  • 不,那是错误的。我真的犯了一个很大的错误,称这些配置指令为“Trusted”,它们真的应该被称为“UnsafeAllowXSS”。如果您不知道为什么要使用它,请将其关闭。
  • 好吧 tnx 寻求建议,但如果我关闭它,我不知道如何允许 CSS 像:top、left、bottom、right 等等。

标签: html htmlpurifier


【解决方案1】:

您的问题可能是 HTML.AllowedAttributes 实际上并不是这样工作的。 :) 来自文档:

全局属性(style、id、class、dir、lang、xml:lang)的语法是“tag.attr”或“*.attr”。

你可能想要的是……

$config->set('HTML.AllowedAttributes', 'img.src,*.style,*.class');

您也不应该单独使用HTML.AllowedAttributes,而应与HTML.AllowedElements一起使用:

$config->set('HTML.AllowedElements', 'img,div');

或者,使用既不 HTML.AllowedAttributes 也不 HTML.AllowedElements,而改为使用HTML.Allowed。看起来像这样:

$config->set('HTML.Allowed', 'div, *[style|class], img[src]');

【讨论】:

  • 嗨,tnx 的答案,它将对未来有用:) 我今天早上找到了解决方案: $config = HTMLPurifier_Config::createDefault(); $config->set('Attr.EnableID', true); $config->set('CSS.Trusted', true);当我需要所有属性时,我什至不知道为什么要过滤属性。我希望这仍然可以过滤 JS:S
  • HTML Purifier 的默认白名单功能强大、安全且经过严格测试。你必须不遗余力地教它接受&lt;script&gt; 或其他诡计。你绝对应该是安全的。 :) (虽然你真的应该重新评估是否需要CSS.Trusted,正如爱德华已经说过的那样。)
  • 好吧,我重新评估了 CSS.Trusted 并删除了它,但现在内联 css 中的 top、left、bottom 和 right 属性丢失了:(现在我觉得猫在追自己的尾巴:)跨度>
  • left, right, bottom, top 都依赖于position。这种组合可以让使用您网站的人掩盖嵌入其内容的网站的某些部分,从而阻碍其使用、破坏网站或以其他方式恶意破坏您的布局(例如网络钓鱼)。但是看看htmlpurifier.org/live/configdoc/plain.html#CSS.AllowTricky - 这应该是你想要的;继续远离CSS.Trusted
  • @pinkgothic 在 laravel 净化器配置中:'HTML.Allowed' =&gt; 'div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]'。我将p[style] 更新为p[style|class]。有没有办法指定类?所以我只想允许像“my-class”这样的自定义类。
猜你喜欢
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多