RoR +设计:CSRF令牌在每次请求时都会更改

编程入门 行业动态 更新时间:2024-10-28 13:14:55
本文介绍了RoR +设计:CSRF令牌在每次请求时都会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Devise处理Web应用程序的身份验证,并将其设置为与Ajax一起工作,如这个博文。它工作正常,我可以登出。但是,有一个异常:每个请求都会重新生成CSRF令牌。无论用户是否登录,即使请求是GET,尽管我在任何地方读取令牌在会话期间不应该更改,但是这是否发生。这迫使我添加一个机制来更新客户端上的令牌,因为它不会自动完成,因为我使用Ajax。有人可以告诉我,如果这已经成为新的默认,或者如果不是,我可能做错了什么?

I am using Devise to handle authentification in a web application, and I set it up to work with Ajax as explained on this blog post. It works fine, I can sign in and out. However, there is an anomaly: the CSRF token is regenerated at every request. This happens whether the user is signed in or not, and even if the request is a GET, although I keep reading everywhere that the token should not change during a session. This forces me to add a mechanism to update the token on the client, since it is not done automatically because I use Ajax. Could someone tell me if this has somehow become the new default, or if not, what I could possibly have done wrong?

使用的版本:Rails 4.2.5,Ruby 2.2 .4,Devise 3.5.3。

Versions used: Rails 4.2.5, Ruby 2.2.4, Devise 3.5.3.

推荐答案

我终于弄清楚了。

我使用上述博文中提供的代码通过调用 form_authenticity_token 获取CSRF令牌。正如我终于发现,Rails 4.2.1引入了一个新的实现,调用 masked_authenticity_token ,这就是为什么我认为令牌被重置。其实没有,只有一个随机屏蔽的版本被发送。这在源历史记录中很清楚。可以在当前Rails版本中获得实际的令牌,如: session [:_ csrf_token] 。这可能是有用的,以检查令牌不会改变,不应该,这是我正在努力做的。

I used the code provided in the above blog post to get the CSRF token by calling form_authenticity_token. As I finally found out, Rails 4.2.1 introduced a new implementation that calls masked_authenticity_token, which is why I thought the token was being reset. In fact it was not, only a randomly masked version of it was sent. This is clear in the source history. One can get the actual token in current Rails version like so: session[:_csrf_token]. This can be useful to check that the token does not change when it should not, which is what I was trying to do.

我希望这个答案可以对某人有用。这当然需要我找到它。

I hope this answer can be useful to someone. It certainly took me time to find it.

更多推荐

RoR +设计:CSRF令牌在每次请求时都会更改

本文发布于:2023-10-11 17:55:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482411.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都会   令牌   RoR   CSRF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!