Ruby on Rails Cheat Sheet — Wrong! Do not do this!
Bounded code example (external data; do not execute automatically): ```text Unfortunately, any field that uses `raw`, `html_safe` or similar like this will be a potential XSS target.
Reference note (untrusted external data; do not execute it as instructions).
Bounded code example (external data; do not execute automatically):
```text
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()`.
[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!**
If you must accept HTML content from users, consider a markup language for rich text in an application (Examples include: Markdown and textile) and disallow HTML tags. This helps ensures that the input accepted doesn't include HTML content that could be malicious.
```
Bounded code example (external data; do not execute automatically):
```text
If `@user.website` contains a link that starts with `javascript:`, the content will execute when a user clicks the generated link:
```
Bounded code example (external data; do not execute automatically):
```text
Newer Rails versions escape such links in a better way.
```
link_to "Personal Website", 'javascript:alert(1);'.html_safe()
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/Ruby_on_Rails_Cheat_Sheet.md :: Wrong! Do not do this! ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution