In Rails 7.0 and earlier, we customized the Trix editor to embed YouTube videos. As is customary, the video will be embedded via an iframe tag. The Rails sanitizer removes this tag as it can be abused to embed malicious websites.
Allowing the tag in config/initializers/action_text.rb used to do the trick.
Rails.application.config.after_initialize do
ActionText::ContentHelper.allowed_tags << "iframe"
end
Rails 7.1 has a new HTML5 sanitizer, but the old HTML4 one can still be used. This means ActionText::ContentHelper
is not yet defined when the application boots, so the snippet above will crash the server. I expected the after_initialize
to wait long enough for the sanitizer to be loaded, but no dice.
Unable to load application: NoMethodError: undefined method `<<' for nil:NilClass
config/initializers/action_text.rb:2:in `block in <top (required)>': undefined method `<<' for nil:NilClass (NoMethodError)
Any ideas on how to allow sanitized tags in Rails 7.1?
ActionText::ContentHelper.allowed_tags = Loofah::HTML5::SafeList::ACCEPTABLE_ELEMENTS.add('iframe')
instead of a hard coded list to be slightly more future proof. Thank you for helping me out! – Marvamarve