Skip to content

Commit

Permalink
Upgrade jquery-ujs to do proper checks for cross domain requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Mar 26, 2015
1 parent 135ba0f commit 92f2a9d
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions vendor/assets/javascripts/jquery_ujs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,14 @@

// Default way to get an element's href. May be overridden at $.rails.href.
href: function(element) {
return element.attr('href');
return element[0].href;
},

// Submits "remote" forms and links with ajax
handleRemote: function(element) {
var method, url, data, elCrossDomain, crossDomain, withCredentials, dataType, options;
var method, url, data, withCredentials, dataType, options;

if (rails.fire(element, 'ajax:before')) {
elCrossDomain = element.data('cross-domain');
crossDomain = elCrossDomain === undefined ? null : elCrossDomain;
withCredentials = element.data('with-credentials') || null;
dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);

Expand Down Expand Up @@ -147,7 +145,7 @@
error: function(xhr, status, error) {
element.trigger('ajax:error', [xhr, status, error]);
},
crossDomain: crossDomain
crossDomain: rails.isCrossDomain(url)
};

// There is no withCredentials for IE6-8 when
Expand All @@ -167,6 +165,27 @@
}
},

// Determines if the request is a cross domain request.
isCrossDomain: function(url) {
var originAnchor = document.createElement("a");
originAnchor.href = location.href;
var urlAnchor = document.createElement("a");

try {
urlAnchor.href = url;
// This is a workaround to a IE bug.
urlAnchor.href = urlAnchor.href;

// Make sure that the browser parses the URL and that the protocols and hosts match.
return !urlAnchor.protocol || !urlAnchor.host ||
(originAnchor.protocol + "//" + originAnchor.host !==
urlAnchor.protocol + "//" + urlAnchor.host);
} catch (e) {
// If there is an error parsing the URL, assume it is crossDomain.
return true;
}
},

// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
handleMethod: function(link) {
Expand All @@ -178,7 +197,7 @@
form = $('<form method="post" action="' + href + '"></form>'),
metadataInput = '<input name="_method" value="' + method + '" type="hidden" />';

if (csrfParam !== undefined && csrfToken !== undefined) {
if (csrfParam !== undefined && csrfToken !== undefined && !rails.isCrossDomain(href)) {
metadataInput += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
}

Expand Down

0 comments on commit 92f2a9d

Please sign in to comment.