Django CSRF cookie accessible by javascript?

前端 未结 2 1755
遥遥无期
遥遥无期 2021-01-13 15:55

On django website, https://docs.djangoproject.com/en/dev/ref/contrib/csrf/ it states:

The CSRF protection is based on the following things:

1. A CSRF cookie         


        
相关标签:
2条回答
  • 2021-01-13 15:59

    From the name CSRF (Cross Site Request Forgery), you can already guess the attacker must perform the request from "cross site" (other site).

    "The key to understanding CSRF attacks is to recognize that websites typically don't verify that a request came from an authorized user. Instead they verify only that the request came from the browser of an authorized user." - quoted here

    So for sites that don't prevent CSRF attacks, the attacker can send the malicious request from anywhere: browsers, emails, terminal... Since the website doesn't check the origin of the request, it believes that the authorized user made the request.

    In this case, in every Django form, you have a hidden input called "CSRF token". This value is randomly and uniquely generated at the time the form rendered, and will be compared after the request has been made. So the request can only be sent from the authorized user's browser. There is no way (which I know of) an attacker can get this token and perform the malicious request that can be accepted by Django backend.

    Clear enough?

    0 讨论(0)
  • 2021-01-13 16:04

    I believe that this post answers your updated question:

    Because of the same-origin policy, the attacker cannot access the cookie indeed. But the browser will add the cookie to the POST request anyway, as you mentioned. For this reason, one must post the CSRF token from the code as well (e.g. in a hidden field). In this case, the attacker must know the value of the CSRF token as stored in the victim's cookie at the time she creates the malicious form. Since she cannot access the cookie, then she cannot replicate the token in her malicious code, and the attack fails.

    Now, one might imagine other ways of storing the token than in the cookie. The point is that the attacker must not be able to get it. And the server must have a way to verify it. You could imagine saving the token together with the session on the server-side, and storing the token in some "safe" way on the client side ("safe" meaning that the attacker cannot access it).

    Here is a quote from OWASP:

    In general, developers need only generate this token once for the current session. After initial generation of this token, the value is stored in the session and is utilized for each subsequent request until the session expires. When a request is issued by the end-user, the server-side component must verify the existence and validity of the token in the request as compared to the token found in the session. If the token was not found within the request or the value provided does not match the value within the session, then the request should be aborted, token should be reset and the event logged as a potential CSRF attack in progress.

    In the end, the security needs two things:

    • The CSRF token must be sent from the code, which means that the malicious code must know it.
    • The CSRF token must be stored in some "safe" place for comparison (the cookie is convenient for this).

    I am not a specialist, but this is my understanding of the problem. Hope it helps.

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