禁用 cookie 时处理 CSRF 保护
如果未启用 cookie,表单发布将抛出 ActionController::InvalidAuthenticityToken
。大多数情况下,我只是用一条消息来处理异常,指出登录我的应用程序需要 cookie。
但是,我的应用程序的用户可以做的一件事是创建与其他人共享的内容,这些人可能已登录,也可能未登录(这不是必需的)。此外,如果创建者选择这样做,该内容可以受到密码保护。这就是我遇到问题的地方。如果重要的话,我将使用 active_record_store
作为我的 session_store
,并且还设置了 config.action_controller.session
。
如果访问者在未启用 cookie 的情况下尝试解锁受密码保护的内容,应用程序将引发异常,并且从访问者的角度来看,会崩溃。我希望能够处理这种情况,但我不知道如何处理。我可以处理异常并允许在不检查 cookie 的情况下解锁内容,但如果我这样做,我可能也不会针对 CSRF 保护该操作,因为无论如何我都会打开一个漏洞,对吗?或者,我可以要求启用 cookie,以便可以对表单进行身份验证,但在这种情况下确实没有理由要求 cookie。
如果我理解正确的话,如果我禁用该操作的保护,我只会将解锁受保护的内容暴露给 CSRF 漏洞,但如果无论如何都没有启用 cookie,这会重要吗?有没有办法在处理缺少 cookie 时进行保护?
Without cookies enabled, a form post will throw ActionController::InvalidAuthenticityToken
. For the most part, I just handle the exception with a message stating that cookies are required to login to my application.
However, one thing users of my application can do is create content to be shared with other people, who may or may not be logged in (it is not required). Furthermore, this content can be password protected if the creator chooses to do so. This is where I am running into problems. If it matters, I am using the active_record_store
as my session_store
, and also have config.action_controller.session
set.
If a visitor tries to unlock password protected content without having cookies enabled, the application will throw the exception, and from the visitor's perspective, crash. I would like to be able to handle this situation, but I am not sure how to. I could handle the exception and allow the content to be unlocked without checking the cookie, but if I am doing so I might as well not be protecting the action against CSRF because I'd be opening up a vulnerability anyways, correct? Alternately, I could require cookies to be enabled so that the form can be authenticated, but there really is no reason to otherwise require cookies in this situation.
If I understand correctly, if I disable protection on that action, I will only be exposing unlocking protected content to a CSRF vulnerability, but if cookies are not enabled anyways, would it matter? Is there a way to protect while handling a lack of cookies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为禁用该操作不会有问题。伪造保护仅对对应用程序数据有一定影响的操作真正有用。
I don't think it will be a problem to disable it for that action. Forgery protection is only really useful for actions that have some effect on your app's data.