Skip to content

Commit

Permalink
Port a Brakeman update to content_tag for Rails 6 (#1552)
Browse files Browse the repository at this point in the history
presidentbeef/brakeman#1778
Since Rails 6.1.7.3
  • Loading branch information
Greg-Myers-SB authored Dec 9, 2024
1 parent f387056 commit fd1d04f
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions cheatsheets/Ruby_on_Rails_Cheat_Sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,11 @@ By default, protection against XSS comes as the default behavior. When string da

# Wrong! Do not do this!
<%= @product.name.html_safe %>
# Wrong! Do not do this!
<%= content_tag @product.name %>
```
Unfortunately, any field that uses `raw`, `html_safe`, `content_tag` or similar like this will be a potential XSS target. Note that there are also widespread misunderstandings about `html_safe()`.

[This writeup](https://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html) describes the underlying SafeBuffer mechanism in detail. Other tags that change the way strings are prepared for output can introduce similar issues, including content_tag.
Unfortunately, any field that uses `raw`, `html_safe` or similar like this will be a potential XSS target. Note that there are also widespread misunderstandings about `html_safe()`.
``` ruby
content_tag("/><script>alert('hack!');</script>") # XSS example
# produces: </><script>alert('hack!');</script>><//><script>alert('hack!');</script>>
```
[This writeup](https://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html) describes the underlying SafeBuffer mechanism in detail. Other tags that change the way strings are prepared for output can introduce similar issues.
The method `html_safe` of String is somewhat confusingly named. It means that we know for sure the content of the string is safe to include in HTML without escaping. **This method itself is un-safe!**
Expand Down

0 comments on commit fd1d04f

Please sign in to comment.