gpt4 book ai didi

ruby-on-rails - Rails 请求伪造保护设置

转载 作者:行者123 更新时间:2023-12-04 07:39:11 25 4
gpt4 key购买 nike

请帮助 Rails 新手:) 我有 protect_from_forgery在我的 ApplicationController 中没有属性的调用(默认给出)类(class)。

基本上这里是代码:

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery
helper_method :current_user_session, :current_user
filter_parameter_logging :password, :password_confirmation

我认为它应该做的是:它应该阻止任何没有正确的 POST 请求 authenticity_token .但是当我像下面这样使用 jQuery 发送 post 请求时,它工作正常(在数据库中执行了更新语句)!
$.post($(this).attr("href"), { _method: "PUT", data: { test: true } });

我在控制台中看到没有 authenticity_token在发送的参数中,但请求仍然被认为是有效的。这是为什么?

UPD
在 config/environments/development.rb 中找到配置设置
config.action_controller.consider_all_requests_local = true

由于 DEV 环境和本地请求,这些 jQuery post 请求是可以的。

最佳答案

只要请求$.post($(this).attr("href"), { _method: "PUT", data: { test: true } });,你的代码就没有问题从应用程序本身内执行。如果您在其他地方运行了另一个应用程序,例如在 localhost:3001 上运行,并且您从那里发送了一个帖子,那么它将无法工作。事实上,如果您使用的是 Firefox > 3.0,它也有跨站点 xhr 的早期实现。例如,您可以从任何其他站点发送 POST(但前提是protect_from_forgery 已关闭!)。 xhr 不需要 auth token 的原因是跨站点 xhr 被禁用。因此,在不提供身份验证 token 的情况下使用 xhr 是安全的。如果您从应用程序以外的任何其他地方尝试,我相信它会引发异常,要求提供身份验证 token 。此外,您应该定义一个 crossdomain.xml 以防止来自外部来源的访问。

尝试这样做:curl -X -d url_endpoint_of_your_app .查看您是否收到 200 响应代码。如果你这样做了,那就有点可疑了。

关于ruby-on-rails - Rails 请求伪造保护设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2725118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com