什么时候不需要/不需要使用 AntiForgeryToken?

发布于 2024-10-12 21:01:27 字数 778 浏览 2 评论 0原文

UPD:security.stackexchange.com 和我得到的答案是不同的。请关注这里,以获得正确答案!

我正在运行一个相当大的网站,每天有数千次访问,并且有一个相当大的用户群。

自从我开始迁移到 MVC 3 以来,我一直将 AntiForgeryToken 放入多种表单中,以修改受保护的数据等。

其他一些表单(例如登录/注册)现在也使用 AntiForgeryToken,但我开始怀疑它们的用途首先需要那里,出于几个原因......

  1. 登录表单要求发布者知道正确的凭据。我真的想不出 csrf 攻击会在这里受益。特别是如果我检查请求是否来自同一主机(检查 Referrer 标头)
  2. AntiForgeryToken 令牌每次加载页面时都会生成不同的值。如果我在登录页面上打开两个选项卡,然后尝试发布它们,第一个将成功加载。第二个将失败并出现 AntiForgeryTokenException(首先加载两个页面,然后尝试发布它们)。对于更安全的页面 - 这显然是一种必要的罪恶,对于登录页面 - 似乎有点矫枉过正,只是自找麻烦:S

可能还有其他原因导致人们在表单中使用/不使用令牌..我是否正确假设在每个帖子表单中使用令牌是不好的/过度杀伤力,如果是这样 - 什么样的表单会从中受益,哪些表单肯定不会受益?

UPD: Same question asked on security.stackexchange.com and the answer I got is different. Please follow there, to get the correct answer!

I'm running a rather large site with thousands of visits every day, and a rather large userbase.

Since I started migrating to MVC 3, I've been putting the AntiForgeryToken in a number of forms, that modify protected data etc.

Some other forms, like the login / registration also use the AntiForgeryToken now, but I'm becoming dubious about their need there in the first place, for a couple reasons...

  1. The login form requires the poster to know the correct credentials. I can't really think of any way an csrf attack would benefit here. Especially if I check that the request came from the same host (checking the Referrer header)
  2. The AntiForgeryToken token generates different values every time the page is loaded.. If I have two tabs open with the login page, and then try to post them, the first one will successfully load. The second will fail with a AntiForgeryTokenException (first load both pages, then try to post them). With more secure pages - this is obviously a necessary evil, with the login pages - seems like overkill, and just asking for trouble :S

There are possibly other reasons why would one use/not use the token in their forms.. Am I correct in assuming that using the token in every post form is bad / overkill, and if so - what kind of forms would benefit from it, and which ones would definitely NOT benefit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-10-19 21:01:27

防伪令牌在用户尚未经过身份验证的网站公共部分(例如登录和注册表单)毫无用处。 CSRF 攻击的工作方式如下:

  1. 恶意用户在其网站上设置一个与您的网站相似的 HTML 表单。该表单也可以包含隐藏字段。
  2. 他欺骗您网站的一位用户访问他的恶意网址。
  3. 用户认为他在您的网站上,填写表格并将其提交到您的网站。
  4. 如果用户已经在您的网站上进行了身份验证,则表单提交成功,并且毫无戒心的用户已经删除了他的帐户(或您可以想象的任何内容)。

因此,您可以在站点的经过身份验证的部分上使用防伪令牌,其中包含可以以某种方式修改用户状态的操作。

备注:检查 Referer 标头来识别请求来自您的网站是不安全的。任何人都可以伪造请求并欺骗此标头。

Anti forgery tokens are useless in public parts of the site where users are not yet authenticated such as login and register forms. The way CSRF attack works is the following:

  1. A malicious user sets a HTML form on his site which resembles your site. This form could contain hidden fields as well.
  2. He tricks one of your site users to visit his malicious url.
  3. The user thinks that he is on your site, fills the form and submits it to your site.
  4. If the user was already authenticated on your site the form submission succeeds and the unsuspecting user have deleted his account (or whatever you can imagine).

So you could use anti forgery tokens on authenticated parts of your site containing actions that could modify somehow the user state.

Remark: checking the Referer header for identifying that a request came from your site is not secure. Anyone can forge a request and spoof this header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文