Reputation: 675
I have a concern about the safety of using Django's {{ csrf_token }}
in an ajax call stated in a template. Consider the case below:
function set_sensitive_data() {
$.ajax({
url: "{% url 'some_sensitive_view' %}",
method: "POST",
data:{
'csrfmiddlewaretoken': "{{ csrf_token }}",
'sensitive_data': "{{ some_data }}"
},
});
}
It works perfectly fine but is there any particular reason why I shouldn't do it this way? I've read Django docs and know that preferred way is to use cookies but that's not my case and I'm not asking about other solutions - I just want to know if this way is unsafe and if so then why?
Upvotes: 0
Views: 172
Reputation: 1394
This is fine. The CSRF token is not reusable and the token is still encrypted in transit as long as you're using HTTPS.
Upvotes: 2