FR: Setting for CSRF Header (pull-request included)

322 views
Skip to first unread message

Wesley Alvaro

unread,
Nov 22, 2013, 2:28:28 PM11/22/13
to django-d...@googlegroups.com
I've been using AngularJS with Django, but I have to override the default CSRF cookie/header values in AngularJS since only one of the values (the cookie name) can be overridden in Django.

This is a humble request to add a setting for the CSRF header name so that I can maintain it as my "AngularJS CSRF settings" and I'm sure others would like it too.  Changing the cookie and header to XSRF-TOKEN and X-XSRF-TOKEN respectively means that CSRF in Angular just works. Nice. With AngularJS's popularity on the sharp incline, I see this being quite a useful-to-many feature.

I acknowledge that adding a setting is not ideal, so I welcome any ideas you may have.

Thanks,
-Wes

Lee Trout

unread,
Nov 22, 2013, 5:25:14 PM11/22/13
to django-d...@googlegroups.com
Looking at it objectively I'm on the fence. Angular's $http is easily configurable at the provider level and I feel like the onus is on any front-end tool to be flexible enough to work with different servers. At the same time if I needed the same code to talk to Django, Flask, and Node then I would expect that I could get all of them on some common ground. Of course I would probably just roll my own middleware in that case...

I hold that opinion because maintaining a handful of projects, for me, is easier by having the backend as the constant and just remembering when I'm rolling djangular that I need to config a couple settings when I start off with Angular. (Another being setting X-Requested-With so request.is_ajax works as expected).


--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/65d2ce93-1d70-4972-b635-e65dcb896c61%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Wesley Alvaro

unread,
Dec 2, 2013, 8:05:51 PM12/2/13
to django-d...@googlegroups.com
I agree; that's exactly the issue I'm trying to rectify. I'd like the backend to be constant and just work. We have many applications that run off of the same Django setup, essentially differing only in their handlers. That's why I'd like to configure the backend once and my front-ends (not shared, doesn't even have to be Angular) can be good to go with Angular without having to configure each of them (as we are right now).

Conversation on the PR has changed it from adding a setting to removing 6 settings.

I objectively think that this is the best solution. It declutters the settings module, moving the settings to where they're actually (and only) used: the middleware class. This makes it stupid easy to subclass and change the settings to whatever you need. In my case:

middleware.py:

class AngularCsrfViewMiddleware(csrf.CsrfViewMiddleware):
  HEADER_NAME = 'HTTP_X_XSRF_TOKEN'

settings.py:

MIDDLEWARE_CLASSES = (
  'my.app.middleware.AngularCsrfViewMiddleware',
)

Do you agree that this looks like a better solution? I have created a new PR (#1995) to use this method and updated the tests.
Obviously, there will need to be a deprecation schedule setup for the settings that have been moved.


Thanks!
-Wes

Tim Graham

unread,
Jul 8, 2014, 1:48:43 PM7/8/14
to django-d...@googlegroups.com
The main problem I see with this approach is that it would no longer be straightforward for 3rd party code to access these settings. You'd need something akin to get_user_model() to retrieve the currently installed CSRF middleware so you could access its settings.

Take a look here for examples of 3rd party code that accesses a CSRF setting.

Wes Alvaro

unread,
Aug 4, 2014, 3:52:40 PM8/4/14
to django-d...@googlegroups.com
I don't see that as a drawback at all. Third party code should not be concerned with the CSRF cookie information. There's a separation of concerns that's being violated there. Are you speaking from knowledge of 3rd party code needing access to this data or hypothetically? If you have an example, I'd be interested to see why they are accessing it and why they aren't implemented as a CSRF middleware.

Donald Stufft

unread,
Aug 4, 2014, 3:57:04 PM8/4/14
to Wes Alvaro, django-d...@googlegroups.com


On August 4, 2014 at 3:52:56 PM, Wes Alvaro (he...@wesalvaro.com) wrote:
> I don't see that as a drawback at all. Third party code should not be
> concerned with the CSRF cookie information. There's a separation of
> concerns that's being violated there. Are you speaking from knowledge of
> 3rd party code needing access to this data or hypothetically? If you have
> an example, I'd be interested to see why they are accessing it and why they
> aren't implemented as a CSRF middleware.
>

Well any thing with hardcoded cookie names in javascript would break
with this setting although i’m inclined to say you shouldn’t change
the setting in that case.

--
Donald Stufft
PGP: 0x6E3CBCE93372DCFA // 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA

Wes Alvaro

unread,
Aug 4, 2014, 5:13:57 PM8/4/14
to Donald Stufft, django-d...@googlegroups.com

Yeah, I would agree with you. You should know what your csrf middleware is doing when you enable it so you should know what cookie name, etc is being used for your JS.

Tim Graham

unread,
Aug 4, 2014, 5:22:19 PM8/4/14
to django-d...@googlegroups.com
Did you look at the github code search link on my previous email? You may substitute the other CSRF setting names and determine if you think people are doing legitimate things or not.
Reply all
Reply to author
Forward
0 new messages