是否有必要添加缺失的结束标签作为清理 HTML 的一部分以防止 XSS 攻击?

发布于 2024-11-29 10:42:12 字数 222 浏览 0 评论 0 原文

我正在使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴梦长久 2024-12-06 10:42:12

Sanitize 构建于 Nokogiri

因为它基于 Nokogiri(一个成熟的 HTML 解析器),而不是一堆脆弱的正则表达式,所以 Sanitize 可以轻松处理格式错误或恶意形成的 HTML,并且始终输出有效的 HTML 或 XHTML< /strong>.


强调我的。所以答案是“不”,您必须修复损坏的 HTML。

Nokogiri 必须修复 HTML,以便可以正确解释它并可以构建 DOM,然后 Sanitize 将修改 Nokogiri 构建的 DOM,最后修改后的 DOM 将被序列化以获得您要存储的 HTML。

如果您浏览 Sanitize 源代码,您会发现所有内容最终都会经过 clean! 并且将使用 Nokogiri 的 to_htmlto_xhtml 方法:

if @config[:output] == :xhtml
  output_method = fragment.method(:to_xhtml)
  output_method_params[:save_with] = Nokogiri::XML::Node::SaveOptions::AS_XHTML
elsif @config[:output] == :html
  output_method = fragment.method(:to_html)
else
  raise Error, "unsupported output format: #{@config[:output]}"
end

result = output_method.call(output_method_params)

所以你得到了 Nokogiri 的HTML 版本,而不仅仅是删除了不良部分的 HTML。

Sanitize is built on top of Nokogiri:

Because it’s based on Nokogiri, a full-fledged HTML parser, rather than a bunch of fragile regular expressions, Sanitize has no trouble dealing with malformed or maliciously-formed HTML, and will always output valid HTML or XHTML.

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's to_html or to_xhtml methods:

if @config[:output] == :xhtml
  output_method = fragment.method(:to_xhtml)
  output_method_params[:save_with] = Nokogiri::XML::Node::SaveOptions::AS_XHTML
elsif @config[:output] == :html
  output_method = fragment.method(:to_html)
else
  raise Error, "unsupported output format: #{@config[:output]}"
end

result = output_method.call(output_method_params)

So you get Nokogiri's version of the HTML, not simply your HTML with the bad parts removed.

迷迭香的记忆 2024-12-06 10:42:12

也许您可以按照文档中的说明配置 sanitize:

默认情况下,Sanitize 会删除所有 HTML。您可以使用内置的之一
配置告诉 Sanitize 允许某些属性和元素:

Sanitize.clean(html, Sanitize::Config::RESTRICTED)
# => 'foo'

Sanitize.clean(html, Sanitize::Config::BASIC)
# => 'foo'

Sanitize.clean(html, Sanitize::Config::RELAXED)
# => 'foo

src="http://foo.com/bar.jpg"/>'

或者,如果您想更好地控制允许的内容,您可以提供
您自己的自定义配置:

Sanitize.clean(html, :elements => ['a', 'span'],
    :属性=> {'a' =>; ['href', '标题'], '跨度' =>

['类']},
:协议=> {'a' =>; {'href'=>; ['http', 'https',
'mailto']}})

引用自 wonko.com

Perhaps you can configure sanitize as demonstrated in the documentation:

By default, Sanitize removes all HTML. You can use one of the built-in
configs to tell Sanitize to allow certain attributes and elements:

Sanitize.clean(html, Sanitize::Config::RESTRICTED)
# => '<b>foo</b>'

Sanitize.clean(html, Sanitize::Config::BASIC)
# => '<b><a href="http://foo.com/" rel="nofollow">foo</a></b>'

Sanitize.clean(html, Sanitize::Config::RELAXED)
# => '<b><a href="http://foo.com/">foo</a></b><img

src="http://foo.com/bar.jpg" />'

Or, if you’d like more control over what’s allowed, you can provide
your own custom configuration:

Sanitize.clean(html, :elements => ['a', 'span'],
    :attributes => {'a' => ['href', 'title'], 'span' =>

['class']},
:protocols => {'a' => {'href' => ['http', 'https',
'mailto']}})

Quoted from wonko.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文