django-intercoolerjs icon indicating copy to clipboard operation
django-intercoolerjs copied to clipboard

CSRF token

Open brejoc opened this issue 8 years ago • 8 comments

Django is expecting a CSRF token with POST requests to prevent Cross Site Request Forgeries. This also includes AJAX POST requests.

With a normal form this looks like this:

<form action="" method="post">{% csrf_token %}

Looks like there are two ways to implement this.

brejoc avatar Feb 16 '17 13:02 brejoc

There's a third way, I believe: ic-on-beforeSend="xhr.setRequestHeader('X-CSRFToken', '{{ csrf_token }}');"

kezabelle avatar Mar 22 '17 08:03 kezabelle

You are right @kezabelle. Thanks a lot! I'm thinking about introducing a template tag to make it more convenient.

brejoc avatar Mar 22 '17 09:03 brejoc

I wonder if intercooler should support the rails-style CSRF meta tags out of the box.

Does django have something similar?

Cheers, Carson

On March 22, 2017 at 2:07:41 AM, Jochen Breuer ([email protected]) wrote:

You are right @kezabelle https://github.com/kezabelle. Thanks a lot! I'm thinking about introducing a template tag to make it more convenient.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/brejoc/django-intercoolerjs/issues/5#issuecomment-288338409, or mute the thread https://github.com/notifications/unsubscribe-auth/AAcov2Yx1NvixG5LCeJ72gSlbdgGHhi8ks5roOTdgaJpZM4MDBLE .

1cg avatar Mar 22 '17 17:03 1cg

@carsongross That would be awesome. Django is very frontend agnostic, so this is our job. I see no reason not to introduce a CSRF meta tag. I would indeed prefer this over anything else. It is easy to implement and solves the problem. Now we only have to convince all the frameworks to use the same header field name. Django uses X-CSRFToken and Rails X-CSRF-Token. You can change that in the Django settings. But I've not tried that yet.

brejoc avatar Mar 22 '17 19:03 brejoc

+1 for an out of the box support on CSRF tokens

ammsa avatar Mar 26 '17 21:03 ammsa

@chg20 Any news on this? I'm just asking, because I was not following the latest changes and would like to catch up. Thanks!

brejoc avatar Mar 31 '19 18:03 brejoc

How does django encode the CSRF token?

1cg avatar Mar 31 '19 19:03 1cg

@chg20 Seems like the best way to get it is from the cookies. Hm… maybe intercooler could have a hook that gets called when implemented and then each framework would be able to ship a tiny snippet that would set the csrf tokens or not?

brejoc avatar Apr 01 '19 14:04 brejoc