Pass Django CSRF token to Angular with CSRF_COOKIE_HTTPONLY

前端 未结 1 859
南笙
南笙 2021-01-02 10:23

In Django, when the CSRF_COOKIE_HTTPONLY setting is set to True, the CSRF cookie gains the httponly flag, which is desirable from a security perspective, but br

1条回答
  •  时光说笑
    2021-01-02 10:55

    I think this question was answered well in this discussion.

    https://groups.google.com/forum/#!topic/django-developers/nXjfLd8ba5k

    https://code.djangoproject.com/ticket/27534

    CSRF_COOKIE_HTTPONLY does not provide any additional security for single page apps. Some people reccomended this solution

    var csrftoken = getCookie('csrftoken');
    if (csrftoken === null) {
        csrftoken = $('input[name="csrfmiddlewaretoken"]').val();
        if (csrftoken === null) {
            console.log('No csrf token');
        }
    }
    

    however either or if you are exposing csrftoken for your app restfully nothing stops the malcious user from taking it and using it as well. If you are running a single page app you might as well set CSRF_COOKIE_HTTPONLY=False, As per comment bellow:

    Gavin Wahl 5/4/15

    How so? You cannot just ajax-fetch stuff from different domains.

    I'm talking about a single domain. Injected javascript on a page that doesn't contain the CSRF token can fetch a different page on the same domain to get it.

    If you already injected javascript onto the victims page (XSS) there is no need to fetch the CSRF token, you already got greater control.

    Well, the HttpOnly flag is intended to reduce the damage an attacker can do once the already can inject javascript. But I agree -- hiding the CSRF token from javascript doesn't increase security in any way, but the documentation implies it does. I want to make it clear in the documentation that this setting has no meaningful effect.

    If you still think you have a valid attack vector there, please send it to secu...@djangoproject.com and add a bit more explanation.

    There is no attack vector. This is just about misleading documentation.

    0 讨论(0)
提交回复
热议问题