django: csrftoken COOKIE vs. csrfmiddlewaretoken HTML Form value

前端 未结 3 1227
臣服心动
臣服心动 2020-12-07 19:41

Trying to learn about security. Curious about why in django when submitting a form (a POST), there are 2 separate \"elements\" that contain the same csrf token value:

3条回答
  •  自闭症患者
    2020-12-07 20:08

    From Jeff Atwood's blog entry:

    Preventing CSRF and XSRF Attacks (Oct 14, 2008)

    The original post

    The Felten and Zeller paper (pdf) recommends the "double-submitted cookie" method to prevent XSRF:

    When a user visits a site, the site should generate a (cryptographically strong) pseudorandom value and set it as a cookie on the user's machine. The site should require every form submission to include this pseudorandom value as a form value and also as a cookie value. When a POST request is sent to the site, the request should only be considered valid if the form value and the cookie value are the same. When an attacker submits a form on behalf of a user, he can only modify the values of the form. An attacker cannot read any data sent from the server or modify cookie values, per the same-origin policy. This means that while an attacker can send any value he wants with the form, he will be unable to modify or read the value stored in the cookie. Since the cookie value and the form value must be the same, the attacker will be unable to successfully submit a form unless he is able to guess the pseudorandom value.

    The advantage of this approach is that it requires no server state; you simply set the cookie value once, then every HTTP POST checks to ensure that one of the submitted values contains the exact same cookie value. Any difference between the two means a possible XSRF attack.

提交回复
热议问题