Skip to content
This repository has been archived by the owner on Feb 12, 2024. 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.

Conflicts:
	HISTORY.rst
	nexus/templatetags/nexus_helpers.py
  • Loading branch information
Adam Chainz authored and delinhabit committed Mar 17, 2016
1 parent 926161f commit 586b3d7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
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 @@ -25,7 +25,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,5 +1,6 @@
from django import template
from django.utils.datastructures import SortedDict
from django.conf import settings

import nexus
from nexus import conf
Expand All @@ -18,6 +19,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 586b3d7

Please sign in to comment.