跳至内容 跳至搜索

Action View Sanitize 帮助器

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

方法
S

实例公共方法

sanitize(html, options = {})

清理 HTML 输入,删除所有已知的安全标签和属性。

它还删除了具有不安全协议(例如 javascript:)的 href / src 属性,同时还防止尝试使用 Unicode、ASCII 和十六进制字符引用来解决这些协议过滤器。

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

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

请注意,清理用户提供的文本并不能保证生成的标记有效或格式良好。

选项

:tags

允许的标签数组。

:attributes

允许的属性数组。

:scrubber

Rails::HTML 清理器Loofah::Scrubber 对象,用于定义自定义清理规则。自定义清理器优先于自定义标签和属性。

示例

正常使用
<%= sanitize @comment.body %>
提供允许的标签和属性的自定义列表
<%= sanitize @comment.body, tags: %w(strong em a), attributes: %w(href) %>
提供自定义 Rails::HTML 清理器
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 清理器的文档,请参阅Rails HTML 清理器

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

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

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

全局配置

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

# 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 解析器进行清理(如果可用,请参阅下面的注意)。如果您希望恢复到以前的 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 111
def sanitize(html, options = {})
  self.class.safe_list_sanitizer.sanitize(html, options)&.html_safe
end

sanitize_css(style)

清理一段 CSS 代码。当遇到样式属性时,sanitize 会使用它。

# File actionview/lib/action_view/helpers/sanitize_helper.rb, line 116
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 133
def strip_tags(html)
  self.class.full_sanitizer.sanitize(html)&.html_safe
end