跳至内容 跳至搜索

Action View 清理助手

SanitizeHelper 模块提供了一组用于清除文本中不需要的 HTML 元素的方法。这些助手方法扩展了 Action View,使其可以在您的模板文件中调用。

方法
S

实例公共方法

sanitize(html, options = {})

清理 HTML 输入,只保留已知的安全标签和属性。

它还会剥离具有不安全协议(如 javascript:)的 href / src 属性,同时还能防止尝试使用 Unicode、ASCII 和十六进制字符引用来规避这些协议过滤器。

默认的清理器是 Rails::HTML5::SafeListSanitizer。有关更多信息,请参阅 Rails HTML Sanitizers

也可以提供自定义的清理规则。

警告:将不允许的标签或属性添加到允许列表中可能会给您的应用程序带来安全漏洞。请尽可能依赖默认的允许列表,因为它们是为了维护安全性和可靠性而精心设计的。如果您认为默认的允许列表应该被扩展,请在 rails-html-sanitizer 项目上打开一个 issue

请注意,清理用户提供的文本并不能保证生成的标记是有效的,甚至是格式正确的。

选项

:tags

允许的标签数组。

:attributes

允许的属性数组。

:scrubber

一个 Rails::HTML scrubberLoofah::Scrubber 对象,用于定义自定义清理规则。自定义 scrubber 的优先级高于自定义标签和属性。

示例

正常使用
<%= sanitize @comment.body %>
提供自定义的允许标签和属性列表
<%= sanitize @comment.body, tags: %w(strong em a), attributes: %w(href) %>
提供自定义的 Rails::HTML scrubber
class CommentScrubber < Rails::HTML::PermitScrubber
  def initialize
    super
    self.tags = %w( form script comment blockquote )
    self.attributes = %w( style )
  end

  def skip_node?(node)
    node.text?
  end
end

<%= sanitize @comment.body, scrubber: CommentScrubber.new %>

有关 Rails::HTML scrubbers 的文档,请参阅 Rails HTML Sanitizer

提供自定义的 Loofah::Scrubber
scrubber = Loofah::Scrubber.new do |node|
  node.remove if node.name == 'script'
end

<%= sanitize @comment.body, scrubber: scrubber %>

有关定义自定义 Loofah::Scrubber 对象的更多信息,请参阅 Loofah 的文档

全局配置

要在您的应用程序中设置默认的允许标签或属性

# In config/application.rb
config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a']
config.action_view.sanitized_allowed_attributes = ['href', 'title']

从 Rails 7.1 开始,默认是使用 HTML5 解析器进行清理(如果可用,请参阅下面的 NOTE)。如果您希望恢复到之前的 HTML4 行为,可以在应用程序配置中设置以下内容:

# In config/application.rb
config.action_view.sanitizer_vendor = Rails::HTML4::Sanitizer

或者,如果您从早期版本的 Rails 升级,并希望选择启用 HTML5 行为

# In config/application.rb
config.action_view.sanitizer_vendor = Rails::HTML5::Sanitizer

注意:Rails::HTML5::Sanitizer 在 JRuby 上不受支持,因此在 JRuby 平台上,Rails 将回退使用 Rails::HTML4::Sanitizer

# File actionview/lib/action_view/helpers/sanitize_helper.rb, line 117
def sanitize(html, options = {})
  self.class.safe_list_sanitizer.sanitize(html, options)&.html_safe
end

sanitize_css(style)

清理 CSS 代码块。当 sanitize 遇到 style 属性时会使用它。

# File actionview/lib/action_view/helpers/sanitize_helper.rb, line 122
def sanitize_css(style)
  self.class.safe_list_sanitizer.sanitize_css(style)
end

html 中剥离所有链接标签,只留下链接文本。

strip_links('<a href="http://www.rubyonrails.org">Ruby on Rails</a>')
# => Ruby on Rails

strip_links('Please e-mail me at <a href="mailto:me@email.com">me@email.com</a>.')
# => Please e-mail me at me@email.com.

strip_links('Blog: <a href="http://www.myblog.com/" class="nav" target=\"_blank\">Visit</a>.')
# => Blog: Visit.

strip_links('<<a href="https://example.org">malformed & link</a>')
# => &lt;malformed &amp; link

strip_tags(html)

html 中剥离所有 HTML 标签,包括注释和特殊字符。

strip_tags("Strip <i>these</i> tags!")
# => Strip these tags!

strip_tags("<b>Bold</b> no more!  <a href='more.html'>See more here</a>...")
# => Bold no more!  See more here...

strip_tags("<div id='top-bar'>Welcome to my website!</div>")
# => Welcome to my website!

strip_tags("> A quote from Smith & Wesson")
# => &gt; A quote from Smith &amp; Wesson
# File actionview/lib/action_view/helpers/sanitize_helper.rb, line 139
def strip_tags(html)
  self.class.full_sanitizer.sanitize(html)&.html_safe
end