Prevent XSS when passing a parameter directly into link_to
A Rails view helper, link_to, does not sanitise a URL taken directly from a request parameter, so a crafted link with a javascript: scheme can run script in the victim's browser. The reporter frames it as a feature request rather than a confirmed vulnerability.
- Triaged in under an hour
- Disclosed after 5 months
Note: I would say this is perhaps more of a feature request than an actual vulnerability, but Rafael França deleted this from GitHub and asked to submit it here instead
In a rails views it's easy to accidentally create an XSS vulnerability by using the following in a template:
<%= link_to 'Back', params[:back] %>
Doing this exposes the app to an attack that can easily be demonstrated by simply adding this to URL of that view:
?back=javascript%3Aalert%28boom%29%3B
I think it would be good if rails detects this situation and filters the link_to parameter if it's from an untrusted source. The attached two-line patch does this by only allowing the HTTP(S) protocol in that case.
Impact
If a programmer inadvertently passes a parameter directly into link_to then this would leave his site open to an XSS attack. Since rails filters untrusted parameters in many other situations it may not be apparent to the casual observer that link_to does not filter javascript.
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 link_to helper accepts any scheme in a URL passed to it, including javascript:, without checking that the source is trusted.
Impact
If a developer makes this mistake, the site is open to an XSS attack that runs script when the victim clicks the link.
What the attacker needed
Developer passes a parameter into link_to; victim clicks a crafted link
The fix
The reporter's patch would restrict link_to to HTTP(S) when the URL comes from an untrusted source.
How it was found
- A developer passes a request parameter straight into link_to in a template.
- An attacker sends a link whose parameter value uses the javascript scheme.
- The victim opens the crafted link, and the javascript scheme value is used in the generated link.
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 web page can include a link whose address is taken from the URL the visitor arrived with. If that address starts with 'javascript:' instead of 'http', clicking the link runs code in the visitor's browser, which is called cross-site scripting (XSS). The reporter says the framework should block such addresses automatically.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.