跳到內容 跳到搜尋

Action View Sanitize Helpers

SanitizeHelper 模組提供一組方法來清除雜亂的 HTML 元素文字。這些輔助方法會延伸 Action View,讓它們可以在範本檔案中呼叫。

方法
S

執行個體公開方法

sanitize(html, options = {})

消毒 HTML 輸入,移除所有已知的安全標籤和屬性。

它還會移除帶有「javascript:」等不安全協定的 hrefsrc 屬性,同時還能防範使用 Unicode、ASCII 和十六進位字元參考來規避這些協定的篩選器。

預設的消毒器是 Rails::HTML5::SafeListSanitizer。更多資訊請參閱 Rails HTML Sanitizers

也可以提供自訂的消毒規則。

請注意,消毒使用者提供的文字並不保證產生的標記是有效的,甚至成形的。

選項

: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 scrubber 的文件,請參閱 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 剖析器進行消毒(如果它可用,請參閱下列註記)。如果您希望還原為先前的 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 程式碼。當碰到 style 屬性時,會使用 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