Module: ActionView::Helpers::SanitizeHelper
- Extended by:
- ActiveSupport::Concern
- Included in:
- ActionView::Helpers, TextHelper
- Defined in:
- lib/action_view/helpers/sanitize_helper.rb
Overview
The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements. These helper methods extend Action View making them callable within your template files.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#sanitize(html, options = {}) ⇒ Object
Sanitizes HTML input, stripping all tags and attributes that aren’t whitelisted.
-
#sanitize_css(style) ⇒ Object
Sanitizes a block of CSS code.
-
#strip_links(html) ⇒ Object
Strips all link tags from
html
leaving just the link text. -
#strip_tags(html) ⇒ Object
Strips all HTML tags from
html
, including comments and special characters.
Instance Method Details
#sanitize(html, options = {}) ⇒ Object
Sanitizes HTML input, stripping all tags and attributes that aren’t whitelisted.
It also strips href/src attributes with unsafe protocols like javascript:
, while also protecting against attempts to use Unicode, ASCII, and hex character references to work around these protocol filters. All special characters will be escaped.
The default sanitizer is Rails::Html::WhiteListSanitizer. See Rails HTML Sanitizers for more information.
Custom sanitization rules can also be provided.
Please note that sanitizing user-provided text does not guarantee that the resulting markup is valid or even well-formed.
Options
-
:tags
- An array of allowed tags. -
:attributes
- An array of allowed attributes. -
:scrubber
- A Rails::Html scrubber or Loofah::Scrubber object that defines custom sanitization rules. A custom scrubber takes precedence over custom tags and attributes.
Examples
Normal use:
<%= sanitize @comment.body %>
Providing custom whitelisted tags and attributes:
<%= sanitize @comment.body, tags: %w(strong em a), attributes: %w(href) %>
Providing a custom 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 %>
See Rails HTML Sanitizer for documentation about Rails::Html scrubbers.
Providing a custom Loofah::Scrubber:
scrubber = Loofah::Scrubber.new do |node|
node.remove if node.name == 'script'
end
<%= sanitize @comment.body, scrubber: scrubber %>
See Loofah’s documentation for more information about defining custom Loofah::Scrubber objects.
To set the default allowed tags or attributes across your application:
# In config/application.rb
config.action_view. = ['strong', 'em', 'a']
config.action_view.sanitized_allowed_attributes = ['href', 'title']
82 83 84 |
# File 'lib/action_view/helpers/sanitize_helper.rb', line 82 def sanitize(html, = {}) self.class.white_list_sanitizer.sanitize(html, ).try(:html_safe) end |
#sanitize_css(style) ⇒ Object
Sanitizes a block of CSS code. Used by sanitize
when it comes across a style attribute.
87 88 89 |
# File 'lib/action_view/helpers/sanitize_helper.rb', line 87 def sanitize_css(style) self.class.white_list_sanitizer.sanitize_css(style) end |
#strip_links(html) ⇒ Object
Strips all link tags from html
leaving just the link text.
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:[email protected]">[email protected]</a>.')
# => Please e-mail me at [email protected].
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>')
# => <malformed & link
121 122 123 |
# File 'lib/action_view/helpers/sanitize_helper.rb', line 121 def strip_links(html) self.class.link_sanitizer.sanitize(html) end |
#strip_tags(html) ⇒ Object
Strips all HTML tags from html
, including comments and special characters.
("Strip <i>these</i> tags!")
# => Strip these tags!
("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
# => Bold no more! See more here...
("<div id='top-bar'>Welcome to my website!</div>")
# => Welcome to my website!
("> A quote from Smith & Wesson")
# => > A quote from Smith & Wesson
104 105 106 |
# File 'lib/action_view/helpers/sanitize_helper.rb', line 104 def (html) self.class.full_sanitizer.sanitize(html) end |