Skip to content

ActionText::ContentHelper.allowed_tags (and attributes) are no longer easily added to #54478

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

Open
brendon opened this issue Feb 10, 2025 · 10 comments
Assignees

Comments

@brendon
Copy link
Contributor

brendon commented Feb 10, 2025

Since Rails 7.1 and this change in particular:

e8137c5#diff-3ae50dc6dc778a98f32121997298af3643f7ca400cd35638a1236948a10fedd3R58

We can no longer easily add additional allowed_tags to ActionText::ContentHelper because it's computed at runtime and there's no configuration option.

Expected behavior

One could previously do this in an initialiser to add additional tags:

ActionText::ContentHelper.allowed_tags.add 'iframe'

Actual behavior

Now that raises an error saying allowed_tags is nil because it has no default value.

System configuration

Rails version: 7.1+

Ruby version: Any

Probably the best way forward is to add this as a configuration option unless a better option exists?

https://stackoverflow.com/questions/77366033/allow-actiontext-tags-in-rails-7-1-with-new-sanitizers

@flavorjones, issue opened at your request :)

brendon referenced this issue Feb 10, 2025
which were being set to the HTML4 defaults before the sanitizer
configuration could be applied.

Also, backfill some light tests for sanitization.

Related to #48644
@brendon
Copy link
Contributor Author

brendon commented Feb 10, 2025

One proposed solution was this:

ActiveSupport.on_load :action_text_content do
  default_allowed_tags = Class.new.include(ActionText::ContentHelper).new.sanitizer_allowed_tags
  ActionText::ContentHelper.allowed_tags = default_allowed_tags.merge(['iframe', ...])
end

Having to initialise the module into a new class just to get at the defaults feels backward.

@flavorjones flavorjones self-assigned this Feb 10, 2025
@flavorjones
Copy link
Member

This has been challenging to get right because Action Text doesn't have a default set of allowed tags and attributes, it delegates that to the sanitizer, which is also configurable.

How would you feel if the recommended idiom for adding allowed tags and attrs was something like:

ActiveSupport.on_load :action_text_content do
  ActionText::ContentHelper.extra_allowed_tags = ["embed"]
  ActionText::ContentHelper.extra_allowed_attributes = ["foobar"]
end

The idea is that the allowlist would be the union of the sanitizer's allowlist and the "extra_allowed" values.

@brendon
Copy link
Contributor Author

brendon commented Feb 10, 2025

That would work in my case as I just want to add extra tags but I wonder about the case when someone wants to remove a default tag. I suppose it would be weird for them to be removing the trix defaults and they can manipulate the sanitiser defaults elsewhere.

Would it be better to make this an official configuration option? Something like:

config.action_text.extra_allowed_tags
config.action_text.extra_allowed_attributes

@Alexander-Senko
Copy link

the sanitizer, which is also configurable.

config.action_view.sanitized_allowed_attributes lacks a default value as well being nil. That should be fixed first, I guess.

@flavorjones
Copy link
Member

flavorjones commented Mar 14, 2025

@Alexander-Senko What I am trying to say is that Action View has no opinion on allowed attributes and tags; it relies on the underlying sanitizer's lists, and that's why the default value is now nil.

The problem I think we need to solve first is "how to safely modify the allowlist" and not "I need my code to know what the default is", because I imagine the most common reason people need to know that is ... to safely modify it. No?

@flavorjones
Copy link
Member

To be clear, I want to solve both problems. I'm trying to share my mental models to move the conversation forward.

@Alexander-Senko
Copy link

config.action_view.sanitized_allowed_attributes lacks a default value as well being nil. That should be fixed first, I guess.

At the same time ActionView::Base.sanitized_allowed_attributes is set to defaults. Why is config.action_view.sanitized_allowed_attributes not? That looks inconsistent to me.

@flavorjones
Copy link
Member

I agree it is inconsistent and I've said several times already in this thread that I want to fix it.

@rails-bot
Copy link

rails-bot bot commented Jun 12, 2025

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 8-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot added the stale label Jun 12, 2025
@brendon
Copy link
Contributor Author

brendon commented Jun 12, 2025

Not Stale

@rails-bot rails-bot bot removed the stale label Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants