Update sanitizer in ActionView::Helpers::SanitizeHelper

- The sanitizer has been changed to safe_list_sanitizer.
- deprecate white_list_sanitizer
This commit is contained in:
Juanito Fatas 2019-05-14 13:09:39 +09:00 committed by Kasper Timm Hansen
parent 1af44e4aee
commit 52f0b050e2
No known key found for this signature in database
GPG Key ID: 191153215EDA53D8
3 changed files with 18 additions and 22 deletions

@ -4,7 +4,7 @@
module ActionText module ActionText
module ContentHelper module ContentHelper
mattr_accessor(:sanitizer) { Rails::Html::Sanitizer.white_list_sanitizer.new } mattr_accessor(:sanitizer) { Rails::Html::Sanitizer.safe_list_sanitizer.new }
mattr_accessor(:allowed_tags) { sanitizer.class.allowed_tags + [ ActionText::Attachment::TAG_NAME, "figure", "figcaption" ] } mattr_accessor(:allowed_tags) { sanitizer.class.allowed_tags + [ ActionText::Attachment::TAG_NAME, "figure", "figcaption" ] }
mattr_accessor(:allowed_attributes) { sanitizer.class.allowed_attributes + ActionText::Attachment::ATTRIBUTES } mattr_accessor(:allowed_attributes) { sanitizer.class.allowed_attributes + ActionText::Attachment::ATTRIBUTES }
mattr_accessor(:scrubber) mattr_accessor(:scrubber)

@ -1,3 +1,7 @@
* ActionView::Helpers::SanitizeHelper: support rails-html-sanitizer 1.1.0.
*Juanito Fatas*
* Added `phone_to` helper method to create a link from mobile numbers * Added `phone_to` helper method to create a link from mobile numbers
*Pietro Moro* *Pietro Moro*

@ -1,6 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rails-html-sanitizer" require "rails-html-sanitizer"
require "active_support/deprecation"
module ActionView module ActionView
# = Action View Sanitize Helpers # = Action View Sanitize Helpers
@ -16,7 +17,7 @@ module SanitizeHelper
# ASCII, and hex character references to work around these protocol filters. # ASCII, and hex character references to work around these protocol filters.
# All special characters will be escaped. # All special characters will be escaped.
# #
# The default sanitizer is Rails::Html::WhiteListSanitizer. See {Rails HTML # The default sanitizer is Rails::Html::SafeListSanitizer. See {Rails HTML
# Sanitizers}[https://github.com/rails/rails-html-sanitizer] for more information. # Sanitizers}[https://github.com/rails/rails-html-sanitizer] for more information.
# #
# Custom sanitization rules can also be provided. # Custom sanitization rules can also be provided.
@ -79,12 +80,12 @@ module SanitizeHelper
# config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a'] # config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a']
# config.action_view.sanitized_allowed_attributes = ['href', 'title'] # config.action_view.sanitized_allowed_attributes = ['href', 'title']
def sanitize(html, options = {}) def sanitize(html, options = {})
self.class.white_list_sanitizer.sanitize(html, options)&.html_safe self.class.safe_list_sanitizer.sanitize(html, options)&.html_safe
end end
# Sanitizes a block of CSS code. Used by +sanitize+ when it comes across a style attribute. # Sanitizes a block of CSS code. Used by +sanitize+ when it comes across a style attribute.
def sanitize_css(style) def sanitize_css(style)
self.class.white_list_sanitizer.sanitize_css(style) self.class.safe_list_sanitizer.sanitize_css(style)
end end
# Strips all HTML tags from +html+, including comments and special characters. # Strips all HTML tags from +html+, including comments and special characters.
@ -122,20 +123,14 @@ def strip_links(html)
end end
module ClassMethods #:nodoc: module ClassMethods #:nodoc:
attr_writer :full_sanitizer, :link_sanitizer, :white_list_sanitizer attr_writer :full_sanitizer, :link_sanitizer, :safe_list_sanitizer
# Vendors the full, link and white list sanitizers.
# Provided strictly for compatibility and can be removed in Rails 6.
def sanitizer_vendor
Rails::Html::Sanitizer
end
def sanitized_allowed_tags def sanitized_allowed_tags
sanitizer_vendor.white_list_sanitizer.allowed_tags safe_list_sanitizer.allowed_tags
end end
def sanitized_allowed_attributes def sanitized_allowed_attributes
sanitizer_vendor.white_list_sanitizer.allowed_attributes safe_list_sanitizer.allowed_attributes
end end
# Gets the Rails::Html::FullSanitizer instance used by +strip_tags+. Replace with # Gets the Rails::Html::FullSanitizer instance used by +strip_tags+. Replace with
@ -144,9 +139,8 @@ def sanitized_allowed_attributes
# class Application < Rails::Application # class Application < Rails::Application
# config.action_view.full_sanitizer = MySpecialSanitizer.new # config.action_view.full_sanitizer = MySpecialSanitizer.new
# end # end
#
def full_sanitizer def full_sanitizer
@full_sanitizer ||= sanitizer_vendor.full_sanitizer.new @full_sanitizer ||= Rails::Html::Sanitizer.full_sanitizer.new
end end
# Gets the Rails::Html::LinkSanitizer instance used by +strip_links+. # Gets the Rails::Html::LinkSanitizer instance used by +strip_links+.
@ -155,20 +149,18 @@ def full_sanitizer
# class Application < Rails::Application # class Application < Rails::Application
# config.action_view.link_sanitizer = MySpecialSanitizer.new # config.action_view.link_sanitizer = MySpecialSanitizer.new
# end # end
#
def link_sanitizer def link_sanitizer
@link_sanitizer ||= sanitizer_vendor.link_sanitizer.new @link_sanitizer ||= Rails::Html::Sanitizer.link_sanitizer.new
end end
# Gets the Rails::Html::WhiteListSanitizer instance used by sanitize and +sanitize_css+. # Gets the Rails::Html::SafeListSanitizer instance used by sanitize and +sanitize_css+.
# Replace with any object that responds to +sanitize+. # Replace with any object that responds to +sanitize+.
# #
# class Application < Rails::Application # class Application < Rails::Application
# config.action_view.white_list_sanitizer = MySpecialSanitizer.new # config.action_view.safe_list_sanitizer = MySpecialSanitizer.new
# end # end
# def safe_list_sanitizer
def white_list_sanitizer @safe_list_sanitizer ||= Rails::Html::Sanitizer.safe_list_sanitizer.new
@white_list_sanitizer ||= sanitizer_vendor.white_list_sanitizer.new
end end
end end
end end