Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Fix CSRF protection to work with non-standard CSRF cookie names
Browse files Browse the repository at this point in the history
Fixes disqus#19 with an updated version of disqus#18 with review changes. Thanks @karech and @graingert.
  • Loading branch information
Adam Chainz committed Dec 31, 2015
1 parent 6da655a commit d3131d4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Pending Release
* Removed the login/logout pages, which were copied and adapted from an old version of Django Admin, and likely no
longer secure. If you are not logged in Nexus will now redirect you to Django Admin - thus Django Admin is now
required by Nexus.
* Fixed Nexus CSRF protection to work if you have changed the CSRF cookie name,
thanks to a PR on the original Nexus from Github users @karech and
@graingert.

1.0.0 (2015-12-09)
------------------
Expand Down
5 changes: 3 additions & 2 deletions nexus/media/js/nexus.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ jQuery.ajaxSetup({
}

if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
var cookieName = $('#nexus-constants').data('csrfCookieName');
xhr.setRequestHeader("X-CSRFToken", getCookie(cookieName));
}
}
});
});
5 changes: 4 additions & 1 deletion nexus/templates/nexus/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
<script src="{% nexus_media_prefix %}/nexus/js/lib/jquery.js"></script>
<script src="{% nexus_media_prefix %}/nexus/js/lib/jquery.tmpl.js"></script>
<script src="{% nexus_media_prefix %}/nexus/js/lib/facebox/facebox.js"></script>
<script src="{% nexus_media_prefix %}/nexus/js/nexus.js"></script>
<script src="{% nexus_media_prefix %}/nexus/js/nexus.js"
id="nexus-constants"
data-csrf-cookie-name="{% nexus_csrf_cookie_name %}"
></script>

{% block head %}
{% endblock %}
Expand Down
6 changes: 6 additions & 0 deletions nexus/templatetags/nexus_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict

from django import template
from django.conf import settings
from django.utils import six

import nexus
Expand All @@ -20,6 +21,11 @@ def nexus_version():
register.simple_tag(nexus_version)


def nexus_csrf_cookie_name():
return settings.CSRF_COOKIE_NAME
register.simple_tag(nexus_csrf_cookie_name)


def show_navigation(context):
site = context.get('nexus_site', NexusModule.get_global('site'))
request = NexusModule.get_request()
Expand Down

0 comments on commit d3131d4

Please sign in to comment.