Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ActionView::Helpers::SanitizeHelper for upcoming rails-html-sanitizer release #36275

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion actiontext/app/helpers/action_text/content_helper.rb
Expand Up @@ -4,7 +4,7 @@

module ActionText
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_attributes) { sanitizer.class.allowed_attributes + ActionText::Attachment::ATTRIBUTES }
mattr_accessor(:scrubber)
Expand Down
3 changes: 3 additions & 0 deletions actionview/CHANGELOG.md
@@ -1,3 +1,6 @@
* [ActionView::Helpers::SanitizeHelper] Deprecate `#white_list_sanitizer`,
please use `#safe_list_sanitizer` instead.

*Juanito Fatas*

Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/actionview/CHANGELOG.md) for previous changes.
25 changes: 15 additions & 10 deletions actionview/lib/action_view/helpers/sanitize_helper.rb
Expand Up @@ -17,7 +17,7 @@ module SanitizeHelper
# 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
# The default sanitizer is Rails::Html::SafeListSanitizer. See {Rails HTML
# Sanitizers}[https://github.com/rails/rails-html-sanitizer] for more information.
#
# Custom sanitization rules can also be provided.
Expand Down Expand Up @@ -80,12 +80,12 @@ module SanitizeHelper
# config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a']
# config.action_view.sanitized_allowed_attributes = ['href', 'title']
def sanitize(html, options = {})
self.class.white_list_sanitizer.sanitize(html, options).try(:html_safe)
self.class.safe_list_sanitizer.sanitize(html, options).try(:html_safe)
end

# Sanitizes a block of CSS code. Used by +sanitize+ when it comes across a style attribute.
def sanitize_css(style)
self.class.white_list_sanitizer.sanitize_css(style)
self.class.safe_list_sanitizer.sanitize_css(style)
end

# Strips all HTML tags from +html+, including comments and special characters.
Expand Down Expand Up @@ -123,20 +123,20 @@ def strip_links(html)
end

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.
# Vendors the full, link and safe list sanitizers.
# Provided strictly for compatibility and can be removed in Rails 6.
def sanitizer_vendor
Rails::Html::Sanitizer
end

def sanitized_allowed_tags
sanitizer_vendor.white_list_sanitizer.allowed_tags
sanitizer_vendor.safe_list_sanitizer.allowed_tags
end

def sanitized_allowed_attributes
sanitizer_vendor.white_list_sanitizer.allowed_attributes
sanitizer_vendor.safe_list_sanitizer.allowed_attributes
end

# Gets the Rails::Html::FullSanitizer instance used by +strip_tags+. Replace with
Expand All @@ -161,15 +161,20 @@ def link_sanitizer
@link_sanitizer ||= sanitizer_vendor.link_sanitizer.new
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+.
#
# class Application < Rails::Application
# config.action_view.white_list_sanitizer = MySpecialSanitizer.new
# config.action_view.safe_list_sanitizer = MySpecialSanitizer.new
# end
#
def safe_list_sanitizer
@safe_list_sanitizer ||= sanitizer_vendor.safe_list_sanitizer.new
end

def white_list_sanitizer
@white_list_sanitizer ||= sanitizer_vendor.white_list_sanitizer.new
ActiveSupport::Deprecation.warn("Use safe_list_sanitizer instead")
safe_list_sanitizer
end
end
end
Expand Down