预防CSRF? [英] Preventing CSRF?

查看:73
本文介绍了预防CSRF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里看到了一些问题(stackoverflow)和 这是 帖子,但我仍然有一些疑问...


  1. 使用隐藏值




    • 隐藏值可以很容易地复制并像真实值一样发送,难以猜测(例如md5)将无济于事。 (对吗?)


  2. 在到达表单时设置cookie并将cookie值作为隐藏值发送。

  3. p>


    • 您可以轻松地更改cookie值或使用相同的真实隐藏值发送与真实的cookie完全相同的自定义cookie。 (对吗?)


  4. 使用超时,POST值不能太晚。




    • 因此,如果您操作缓慢,则尝试使用隐藏值设置所有内容时都会失败。如果速度很快,它会起作用。 (对吗?)


我想保护CSRF ...但是我该怎么做?

解决方案

我发现防止CSRF问题的最简单方法是:


  1. 在服务器端,分配一个使用随机(不可使用的)令牌向客户端发送的HttpOnly cookie


  2. 在具有该cookie值的表单上放置一个隐藏字段


  3. 提交表单后,请确保隐藏字段值等于cookie值(在服务器端)



I already seen some question from here (stackoverflow) and THIS post, but I still have some questions...

  1. Using hidden value in the post form and check it when post reach the server.

    • The hidden value can easy be copied and send exactly like the real one, "hard to guess" (like md5) will not help. (right?)
  2. Setting a cookie when you reach the form and send the cookie value as a hidden value.

    • You can easily change a cookie value or send a custom cookie exactly like the real one using the same real hidden value. (right?)
  3. Using 'timeout', the POST values cannot reach too late.

    • So, if you're slow you will fail when you try to set everything up with the hidden value. If you're fast it gonna work. (right?)

I want to be protected about CSRF...but how exactly I do it?

解决方案

The easiest way I found to prevent CSRF issues is:

  1. On the server side, assign an HttpOnly cookie to the client with a random (unguessable) token

  2. Place a hidden field on the form with that cookie value

  3. Upon form submit, ensure the hidden field value equals the cookie value (on the server side of things)

这篇关于预防CSRF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆