How to best prevent CSRF attacks in a GAE app?

前端 未结 3 1015
-上瘾入骨i
-上瘾入骨i 2021-02-04 12:01

So, what is the best way to prevent an XSRF attack for a GAE application? Imagine the following:

  1. Anyone can see a user\'s public object, and the db.Model id is us
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 12:24

    When you generate the page that lets the user delete an object, generate a random token and include it in a hidden form field. Also set a HTTP-only cookie with that value. When you receive a delete request, check that the random token from the form and the value from the cookie match.

    Your random token shouldn't just be a random number. You should encrypt the combination of a random number and the user's identity, to make it difficult for attackers to forge their own tokens. You should also use different encryption keys for the value stored in the form and the value stored in the cookie, so if one of the tokens does leak, it is still difficult for an attacker to forge the other token.

    This approach verifies that the delete request originates from your form, by the presence of the security token in the form; and doesn't require writing to the datastore.

    This approach is still vulnerable to cross-site scripting attacks, where an attacker could retrieve the hidden value from the form or submit the form, so thoroughly test your site for cross-site scripting vulnerabilities. This approach is also vulnerable to "clickjacking" attacks.

提交回复
热议问题