XSS prevention for Ruby on Rails
This is a cross-site scripting (XSS) prevention cheat sheet by Semgrep, Inc. It contains code patterns of potential XSS in an application. Instead of scrutinizing code for exploitable vulnerabilities, the recommendations in this cheat sheet pave a safe road for developers that mitigate the possibility of XSS in your code. By following these recommendations, you can be reasonably sure your code is free of XSS.
Mitigation summary
In general, always let Rails render ERB template files rather than constructing them in code. If HTML escaping is needed, use html_safe()
in Ruby code and review each individual usage carefully. Once reviewed, mark the line with # nosem
. Beware of putting data in dangerous locations in templates. And as always, run a security checker continuously on your code.
Check your project using Semgrep
The following command runs an optimized set of rules for your project:
semgrep --config p/default
1. Unescaped variable enters template engine in Ruby code
1.A. Using html_safe()
html_safe()
marks the supplied string as "safe for HTML rendering." This bypasses HTML escaping and potentially creates XSS vulnerabilities.
Example:
html = "<div>#{name}</div>".html_safe
References
Mitigation
Ban html_safe()
. Alternatively, If needed, review each usage and exempt with # nosem
.
Semgrep rule
ruby.rails.security.audit.xss.avoid-html-safe.avoid-html-safe1.B. Using content_tag()
content_tag()
's escaping behavior has changed between Rails 2 and 3. In Rails 2, no supplied content is escaped. In Rails 2 and 3, attribute names are not escaped. Further, the returned value is marked as "safe," the same as if html_safe()
had been used. This confusing behavior makes it difficult to use content_tag()
properly; improper use can create XSS vulnerabilities in your application.
Example:
content_tag :p, "Hello, #{name}"
References
Mitigation
Ban content_tag()
. Alternatively, If necessary, prefer html_safe()
due to its straightforward behavior.
Semgrep rule
ruby.rails.security.audit.xss.avoid-content-tag.avoid-content-tag1.C. Using raw()
raw()
disables HTML escaping for the returned content. This permits raw HTML to be rendered in a template, which could create a XSS vulnerability.
Example:
raw @user.name
References
Mitigation
Ban raw()
. Alternatively, Prefer html_safe()
if necessary.
Semgrep rule
ruby.rails.security.audit.xss.avoid-raw.avoid-raw1.D. Disabling of ActiveSupport#escape_html_entities_in_json
ActiveSupport#escape_html_entities_in_json
is a setting which determines whether Hash#to_json()
will escape HTML characters. Disabling this could create XSS vulnerabilities.
Example:
config.active_support.escape_html_entities_in_json = false
References
escape_html_entities_in_json
documentation- Brakeman scanner - Cross-site scripting (JSON)
- How to disable HTML escaping for JSON, but keep enabled for views?
Mitigation
Ban disabling of ActiveSupport#escape_html_entities_in_json
. Alternatively, If HTML is needed in JSON, use JSON.generate()
and review each usage carefully. Exempt each case with # nosem
.
Semgrep rule
ruby.lang.security.json-entity-escape.json-entity-escape2. Bypassing the template engine
2.A. Manually creating an ERB template
Manually creating an ERB template could create a server-side template injection (SSTI) vulnerability if it is created with user input. (This could also result in XSS.) Due to the severity of this type of vulnerability, it is better to use a template file instead of creating templates in code.
Example:
ERB.new("<div>#{@user.name}</div>").result
References
Mitigation
Ban template creation in code. Alternatively, Use ERB template files.
Semgrep rule
ruby.rails.security.audit.xss.manual-template-creation.manual-template-creation2.B. Rendering an inline template with render inline
render inline:
is the same as creating a template manually and is therefore susceptible to the same vulnerabilities as manually creating an ERB template. This can result in a SSTI or XSS vulnerability.
Example:
render inline: "<div>#{@user.name}</div>"
References
Mitigation
Ban render inline:
. Alternatively, Use ERB template files.
Semgrep rule
ruby.rails.security.audit.xss.avoid-render-inline.avoid-render-inline2.C. Using render text:
render text:
unintuitively sets the Content-Type to text/html. This means anything rendered through render text:
will be interpreted as HTML. Templates rendered in this manner could create a XSS vulnerability.
Example:
render text: "<div>#{@user.name}</div>"
References
Mitigation
Ban render text:
. Alternatively, Use ERB template files.
Semgrep rule
ruby.rails.security.audit.xss.avoid-render-text.avoid-render-text3. Templates: Variable explicitly unescaped
3.A. Using html_safe()
html_safe()
marks the supplied string as "safe for HTML rendering." This bypasses HTML escaping and potentially creates XSS vulnerabilities.
Example:
<%= name.html_safe %>
References
Mitigation
Ban html_safe()
. Alternatively, Prefer using html_safe()
in Ruby code instead of templates.