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

Port a Brakeman update to content_tag for Rails 6 #1552

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>>
Copy link
Contributor Author

@Greg-Myers-SB Greg-Myers-SB Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Rails 8.0

$ rails c
class MyClass
  include ActionView::Helpers::TagHelper
end
=> MyClass
foo = MyClass.new
=> #<MyClass:0x000000012ec9caa0>
foo.content_tag("/><script>alert('hack!');</script>")
=> "<___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