Make page_title HTML-escape only once#92
Merged
Conversation
This wraps the string returned by page_title with escape_once from ActionView::Helpers::TagHelper to make sure it HTML-escapes only once. Without it, rendering <%= page_title %> in an ERB template causes it to be HTML-escaped twice.
elias19r
commented
Jun 9, 2025
| content_for(page_title.page_title_symbol), | ||
| page_title.separator, | ||
| options[:reverse], | ||
| ).to_s, |
Contributor
Author
There was a problem hiding this comment.
there shouldn't be any backwards compatibility issue in making
flutiereturn HTML-safe strings
My first solution was calling .html_safe here and it worked, but only for the page title content. To make sure custom separator and custom app_name are also html-escaped, and once, I think we need to go with escape_once
Member
There was a problem hiding this comment.
Oooh, that's a great point. I hadn't thought of separators and app names needing to be escaped, as in "Johnson & Johnson > São Paulo Headquarters". TIL escape_once.
escape_once returns an html-safe string but only in Rails >= 7.1. So for compatibility with older versions, we need to explicitly apply html_safe here. Co-authored-by: Neil Carvalho <me@neil.pro>
| page_title.separator, | ||
| options[:reverse], | ||
| ).to_s, | ||
| ).html_safe |
There was a problem hiding this comment.
Rails/OutputSafety: Tagging a string as html safe may be a security risk.
Member
|
Thanks for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This wraps the string returned by page_title with escape_once from
ActionView::Helpers::TagHelper to make sure it HTML-escapes only once.
Without it, rendering <%= page_title %> in an ERB template causes it
to be HTML-escaped twice.
https://api.rubyonrails.org/v6.1.0/classes/ActionView/Helpers/TagHelper.html#method-i-escape_once
Issue #73