我正在使用 Sanitize gem 来禁止可用于 XSS 攻击的 HTML 代码。副作用是,HTML 也会被清理。添加缺少的结束标签。这通常没问题,但在许多情况下它会改变内容的格式。
最终,我想完全清理 HTML,但不想将其作为保护网站免受 XSS 攻击的一部分。
那么,缺少结束标签(例如
)是否是潜在的 XSS 漏洞?如果没有,我该如何阻止 Sanitizer 尝试清理 HTML?
I'm using the Sanitize gem to disallow HTML code that could be used for an XSS attack. As a side effect, the HTML also gets cleaned up. Missing closing tags get added. This would normally be fine but in many cases it changes the formatting of the content.
Ultimately, i would like to cleanup the HTML entirely but don't want to have to do this as part of securing the site against XSS.
So, are missing end tags (e.g. </font>
) a potential XSS exploit? If not, how do i stop Sanitizer from trying to clean up the HTML too?
发布评论
评论(2)
Sanitize 构建于 Nokogiri:
强调我的。所以答案是“不”,您必须修复损坏的 HTML。
Nokogiri 必须修复 HTML,以便可以正确解释它并可以构建 DOM,然后 Sanitize 将修改 Nokogiri 构建的 DOM,最后修改后的 DOM 将被序列化以获得您要存储的 HTML。
如果您浏览 Sanitize 源代码,您会发现所有内容最终都会经过
clean!
并且将使用 Nokogiri 的to_html
或to_xhtml
方法:所以你得到了 Nokogiri 的HTML 版本,而不仅仅是删除了不良部分的 HTML。
Sanitize is built on top of Nokogiri:
Emphasis mine. So the answer is "no", you have to fix your broken HTML.
Nokogiri has to fix the HTML so that it can be properly interpreted and a DOM can be built, then Sanitize will modify the DOM that Nokogiri builds, and finally that modified DOM will be serialized to get the HTML that you get to store.
If you scan through the Sanitize source, you'll see that everything ends up going through
clean!
and that will use Nokogiri'sto_html
orto_xhtml
methods:So you get Nokogiri's version of the HTML, not simply your HTML with the bad parts removed.
也许您可以按照文档中的说明配置 sanitize:
引用自 wonko.com
Perhaps you can configure sanitize as demonstrated in the documentation:
Quoted from wonko.com