Module: ActionView::Helpers::SanitizeHelper
Overview
Action View Sanitize Helpers
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.
Class Attribute Summary
- .sanitizer_vendor (also: #sanitizer_vendor) rw
Class Method Summary
::ActiveSupport::Concern - Extended
| class_methods | Define class methods from given block. |
| included | Evaluate given block in context of base class, so that you can write class macros here. |
| prepended | Evaluate given block in context of base class, so that you can write class macros here. |
| append_features, prepend_features | |
Instance Attribute Summary
Instance Method Summary
-
#sanitize(html, options = {})
Sanitizes HTML input, stripping all but known-safe tags and attributes.
-
#sanitize_css(style)
Sanitizes a block of CSS code.
-
#strip_links(html)
Strips all link tags from
htmlleaving just the link text. -
#strip_tags(html)
Strips all HTML tags from
html, including comments and special characters.
Class Attribute Details
.sanitizer_vendor (rw) Also known as: #sanitizer_vendor
[ GitHub ]# File 'actionview/lib/action_view/helpers/sanitize_helper.rb', line 14
mattr_accessor :sanitizer_vendor, default: Rails::HTML4::Sanitizer
Instance Attribute Details
#sanitizer_vendor (rw)
[ GitHub ]# File 'actionview/lib/action_view/helpers/sanitize_helper.rb', line 14
mattr_accessor :sanitizer_vendor, default: Rails::HTML4::Sanitizer
Instance Method Details
#sanitize(html, options = {})
Sanitizes HTML input, stripping all but known-safe tags and attributes.
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.
The default sanitizer is Rails::HTML5::SafeListSanitizer. See Rails HTML
Sanitizers for more information.
Custom sanitization rules can also be provided.
Warning: Adding disallowed tags or attributes to the allowlists may introduce vulnerabilities into your application. Please rely on the default allowlists whenever possible, because they are curated to maintain security and safety. If you think that the default allowlists should be expanded, please open an issue on the rails-html-sanitizer project.
Please note that sanitizing user-provided text does not guarantee that the resulting markup is valid or even well-formed.
The return value is HTML markup. It is safe only as element content where the HTML parser
applies the "in body" insertion mode rules, such as inside a SeeRails HTML Sanitizer for
documentation about SeeLoofah's documentation for more
information about defining custom To set the default allowed tags or attributes across your application: The default, starting in Rails 7.1, is to use an HTML5 parser for sanitization (if it is
available, see NOTE below). If you wish to revert back to the previous HTML4 behavior, you
can do so by setting the following in your application configuration: Or, if you're upgrading from a previous version of Rails and wish to opt into the HTML5
behavior: NOTE: Sanitizes a block of CSS code. Used by #sanitize when it comes across a style attribute. Strips all link tags from Strips all HTML tags from ,
element. Anywhere else it is unsafe or incorrect, including in an attribute value and inside
a , , or element. For text in a quoted attribute
value, use #strip_tags.
Options
:tags
: An array of allowed tags.:attributes
: An array of allowed attributes.:scrubber
: ARails::HTML scrubber
orLoofah::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 lists of permitted tags and attributes
<%= sanitize @comment.body, tags: %w(strong em a), attributes: %w(href) %>
Providing a custom
Rails::HTML scrubberclass CommentScrubber < {Rails::HTML::PermitScrubber}
def initialize
super
self. = %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.Providing a custom
Loofah::Scrubberscrubber = {Loofah::Scrubber.new} do |node|
node.remove if node.name == 'script'
end<%= sanitize @comment.body, scrubber: scrubber %>
Loofah::Scrubber objects.Global Configuration
#### In config/application.rb
config.action_view. = ['strong', 'em', 'a']
config.action_view.sanitized_allowed_attributes = ['href', 'title']#### In config/application.rb
config.action_view.sanitizer_vendor = {Rails::HTML4::Sanitizer}#### In config/application.rb
config.action_view.sanitizer_vendor = {Rails::HTML5::Sanitizer}Rails::HTML5::Sanitizer is not supported on JRuby, so on JRuby platforms Rails will
fall back to using Rails::HTML4::Sanitizer.# File 'actionview/lib/action_view/helpers/sanitize_helper.rb', line 143
def sanitize(html, = {})
self.class.safe_list_sanitizer.sanitize(html, )&.html_safe
end
#sanitize_css(style)
# File 'actionview/lib/action_view/helpers/sanitize_helper.rb', line 148
def sanitize_css(style)
self.class.safe_list_sanitizer.sanitize_css(style)
end
#strip_links(html)
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: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>')
#### => <malformed & link# File 'actionview/lib/action_view/helpers/sanitize_helper.rb', line 187
def strip_links(html)
self.class.link_sanitizer.sanitize(html)
end
#strip_tags(html)
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# File 'actionview/lib/action_view/helpers/sanitize_helper.rb', line 167
def (html)
stripped = self.class.full_sanitizer.sanitize(html)
ERB::Util.html_escape_once(stripped) if stripped
end