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

Guidance for using html_safe #260

Open
andyw8 opened this issue Feb 11, 2020 · 4 comments
Open

Guidance for using html_safe #260

andyw8 opened this issue Feb 11, 2020 · 4 comments

Comments

@andyw8
Copy link
Contributor

andyw8 commented Feb 11, 2020

I noticed there's no mention of #html_safe in views and helpers. I've found this to be a commonly misused/misunderstood part of Rails, but I'm not sure if it should be considered as 'style'. Any thoughts?

@pirj
Copy link
Member

pirj commented Feb 21, 2021

My rule of thumb is to sanitize anything that can be user-provided (including when it's coming from the database, or uploaded/fetched files, not just HTTP params), and only allow it to participate as a source for rendering with html_safe.
For anything that comes from the application source code/resources, use html_safe deliberately.

@pirj
Copy link
Member

pirj commented Feb 21, 2021

@andyw8 Your writing skills are evidently superior to mine, so who would be better than you to send a PR?

@andyw8
Copy link
Contributor Author

andyw8 commented Feb 21, 2021

I haven't done anything with html_safe for a while, so I'd need a refresh on the best practices – I'll try come back to this next I'm working on something related to that.

@pirj
Copy link
Member

pirj commented Feb 21, 2021

Note to future self: it's possible to check/harvest for usage examples in https://github.com/eliotsykes/real-world-rails, https://github.com/pirj/real-world-rspec (slightly more up to date, but fewer Rails repos).

From Rails Guides:

Active Support has the concept of (html) safe strings. A safe string is one that is marked as being insertable into HTML as is. It is trusted, no matter whether it has been escaped or not.

Strings are considered to be unsafe by default:

NOTE: When using html: option, HTML entities will be escaped if the string is not composed with html_safe-aware APIs.

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

2 participants