XSS due to incomplete JS escaping
Rails' JavaScript escaping helper does not escape backticks or template-literal placeholder syntax, so values placed inside a template literal in a view can break out of the string. An attacker who controls such a value could inject and run arbitrary script in a victim's browser.
Originally disclosed on HackerOne
- Triaged in 13 hours
- Fixed in 16 months
- Paid in 16 months
- Disclosed after 16 months
ActionView::Helpers::JavaScriptHelper inside rails/actionview/lib/action_view/helpers/javascript_helper.rb provides JS escaping in Rails, but fails to protect template literal strings. As such, there are two ways XSS can occur:
###XSS via template literal break out:
- Create a view with the following code:
<script>let a = `<%= j '`+alert`' %>`</script>
- The alert will execute because backticks aren't escaped.
###XSS via template literal placeholder evaluation:
- Create a view with the following code:
<script>let a = `<%= j '${alert()}' %>`</script>
- The alert will execute because
${expression}isn't escaped
(escaping $ with \$ seems sufficient)
Impact
Attackers can leverage this weakness to steal private information, hijack accounts and distribute malware by injecting malicious code instead of an alert.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the escaping helper only handles quote characters and does not escape backticks or the dollar-brace sequence used by template literals.
Impact
An attacker could use this to steal private information, hijack accounts and distribute malware by injecting malicious code in place of a simple alert.
What the attacker needed
Developer places escaped value in a template literal; attacker controls the value being escaped
The fix
The escaping helper should also escape backticks and the dollar-brace placeholder sequence, for example by escaping the dollar sign.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A template literal is a JavaScript string written with backticks instead of quotes, and it can contain ${...} expressions that JavaScript runs. Rails has a helper meant to make user-supplied text safe to drop into JavaScript, but it does not handle backticks or that ${...} syntax. So if a developer puts untrusted text inside a backtick string, the text can close the string or add its own expression and run code.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.