Skip to content

Commit

Permalink
Merge pull request #186 from wildfish/bugfix/issue_158_csrf_cookie_ht…
Browse files Browse the repository at this point in the history
…tponly

Added fallback to DOM based CSRF token to handle CSRF_COOKIE_HTTPONLY…
  • Loading branch information
OmegaDroid authored Apr 3, 2020
2 parents 209e6cf + 0bfbb99 commit 584a674
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
21 changes: 17 additions & 4 deletions star_ratings/static/star-ratings/js/dist/star-ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ var djangoRemarkRest = {
return cookieValue;
},

setCSRFToken: function (req) {
var token = this.getCookie('csrftoken');

// attempt to get token from DOM if it's not accessible from the cookie.
// https://docs.djangoproject.com/en/dev/ref/csrf/#acquiring-csrf-token-from-html
if (token == null){
token = document.querySelector('[name=csrfmiddlewaretoken]').value;
}

req.setRequestHeader("X-CSRFToken", token);
return req
},

makeRequest: function (url, method, success, fail) {
var req = new XMLHttpRequest();
if (req.overrideMimeType !== undefined) {
Expand Down Expand Up @@ -261,25 +274,25 @@ var djangoRemarkRest = {

post: function (url, data, success, fail) {
var req = this.makeRequest(url, 'POST', success, fail);
req.setRequestHeader("X-CSRFToken", this.getCookie('csrftoken'));
req = this.setCSRFToken(req)
req.send(JSON.stringify(data));
},

put: function (url, data, success, fail) {
var req = this.makeRequest(url, 'PUT', success, fail);
req.setRequestHeader("X-CSRFToken", this.getCookie('csrftoken'));
req = this.setCSRFToken(req)
req.send(JSON.stringify(data));
},

patch: function (url, data, success, fail) {
var req = this.makeRequest(url, 'PATCH', success, fail);
req.setRequestHeader("X-CSRFToken", this.getCookie('csrftoken'));
req = this.setCSRFToken(req)
req.send(JSON.stringify(data));
},

"delete": function (url, data, success, fail) {
var req = this.makeRequest(url, 'DELETE', success, fail);
req.setRequestHeader("X-CSRFToken", this.getCookie('csrftoken'));
req = this.setCSRFToken(req)
req.send(JSON.stringify(data));
}
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions star_ratings/static/star-ratings/js/src/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ var djangoRemarkRest = {
return cookieValue;
},

setCSRFToken: function (req) {
var token = this.getCookie('csrftoken');

// attempt to get token from DOM if it's not accessible from the cookie.
// https://docs.djangoproject.com/en/dev/ref/csrf/#acquiring-csrf-token-from-html
if (token == null){
token = document.querySelector('[name=csrfmiddlewaretoken]').value;
}

req.setRequestHeader("X-CSRFToken", token);
return req
},

makeRequest: function (url, method, success, fail) {
var req = new XMLHttpRequest();
if (req.overrideMimeType !== undefined) {
Expand Down Expand Up @@ -58,25 +71,25 @@ var djangoRemarkRest = {

post: function (url, data, success, fail) {
var req = this.makeRequest(url, 'POST', success, fail);
req.setRequestHeader("X-CSRFToken", this.getCookie('csrftoken'));
req = this.setCSRFToken(req)
req.send(JSON.stringify(data));
},

put: function (url, data, success, fail) {
var req = this.makeRequest(url, 'PUT', success, fail);
req.setRequestHeader("X-CSRFToken", this.getCookie('csrftoken'));
req = this.setCSRFToken(req)
req.send(JSON.stringify(data));
},

patch: function (url, data, success, fail) {
var req = this.makeRequest(url, 'PATCH', success, fail);
req.setRequestHeader("X-CSRFToken", this.getCookie('csrftoken'));
req = this.setCSRFToken(req)
req.send(JSON.stringify(data));
},

"delete": function (url, data, success, fail) {
var req = this.makeRequest(url, 'DELETE', success, fail);
req.setRequestHeader("X-CSRFToken", this.getCookie('csrftoken'));
req = this.setCSRFToken(req)
req.send(JSON.stringify(data));
}
};
Expand Down

0 comments on commit 584a674

Please sign in to comment.