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

Allow Rails html_safe for non-interpolated String literal receiver #6594

Merged
merged 1 commit into from Dec 21, 2018
Merged

Allow Rails html_safe for non-interpolated String literal receiver #6594

merged 1 commit into from Dec 21, 2018

Conversation

amatsuda
Copy link
Contributor

@amatsuda amatsuda commented Dec 21, 2018

Current Rails/OutputSafety cop seems to be always warning any kinds of html_safe method call as possibly insecure code.

But in fact, html_safe is not such a dangerous thing. It's a part of Rails core API, and it's not something that is discouraged to use that way. It can just be a security risk only when the string could contain random user input values.

Actually, the document of this cop indicates the following code as a "good" code, so I guess this is just an implementation bug. https://github.com/rubocop-hq/rubocop/blob/a5abbde/manual/cops_rails.md#examples-27

out = "<h1>trusted content</h1>".html_safe

And so attached is a patch that allows html_safe call on non-interpolated string literals, just as documented.


  • Wrote [good commit messages][1].
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

Tagging a string as html safe may be a security risk only when the string could contain random user input values.
Marking a statically coded string literal as html safe is a totally valid usage of html_safe.
@bbatsov bbatsov merged commit 8435af4 into rubocop:master Dec 21, 2018
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 21, 2018

Thanks for working on this, @amatsuda! I really appreciate it! 🙇

@amatsuda amatsuda deleted the html_safe_could_be_safe branch December 22, 2018 02:13
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

Successfully merging this pull request may close these issues.

None yet

3 participants