Skip to content

Commit

Permalink
Fix sanitizer vendor config with 7.1 defaults
Browse files Browse the repository at this point in the history
rails-html-santizer is a dependency of Action View and a transitive
dependency of Action Text (via Action Pack), but may not be loaded
until after railties sets configuration defaults.

This change `require`s rails-html-sanitizer immediately before it's
needed, and avoids the possibly-incorrect assumption that
Rails::HTML::Sanitizer is already defined.

Closes #51246

Co-authored-by: Rafael Mendonça França <rafael@rubyonrails.org>
  • Loading branch information
flavorjones and rafaelfranca committed Mar 6, 2024
1 parent f0d433b commit 1a5529e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
7 changes: 7 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,10 @@
* Fix sanitizer vendor configuration in 7.1 defaults.

In apps where rails-html-sanitizer was not eagerly loaded, the sanitizer default could end up
being Rails::HTML4::Sanitizer when it should be set to Rails::HTML5::Sanitizer.

*Mike Dalessio*, *Rafael Mendonça França*

* Setup jemalloc in the default Dockerfile for memory optimization.

*Matt Almeida*, *Jean Boussier*
Expand Down
14 changes: 7 additions & 7 deletions railties/lib/rails/application/configuration.rb
Expand Up @@ -312,14 +312,14 @@ def load_defaults(target_version)
active_support.raise_on_invalid_cache_expiration_time = true
end

if defined?(Rails::HTML::Sanitizer) # nested ifs to avoid linter errors
if respond_to?(:action_view)
action_view.sanitizer_vendor = Rails::HTML::Sanitizer.best_supported_vendor
end
if respond_to?(:action_view)
require "rails-html-sanitizer"
action_view.sanitizer_vendor = Rails::HTML::Sanitizer.best_supported_vendor
end

if respond_to?(:action_text)
action_text.sanitizer_vendor = Rails::HTML::Sanitizer.best_supported_vendor
end
if respond_to?(:action_text)
require "rails-html-sanitizer"
action_text.sanitizer_vendor = Rails::HTML::Sanitizer.best_supported_vendor
end
when "7.2"
load_defaults "7.1"
Expand Down

0 comments on commit 1a5529e

Please sign in to comment.