【问题标题】:protect_from_forgery in Rails 6?Rails 6中的protect_from_forgery?
【发布时间】:2019-09-15 12:41:20
【问题描述】:

protect_from_forgery 方法不包含在我的应用程序控制器中,默认为 Rails 6 应用程序,但在主应用程序布局中有嵌入的 ruby​​ <%= csrf_meta_tags %>。这是否意味着protect_from_forgery 方法已被抽象并且不再在应用程序控制器中显式需要?

我买了 Pragmatic Programmer's Rails 6 书,我唯一能找到的是“csrf_meta_tags() 方法设置了防止跨站点请求伪造攻击所需的所有幕后数据”。

【问题讨论】:

    标签: ruby-on-rails ruby csrf csrf-token


    【解决方案1】:

    对于 Rails 5.2 及更高版本,ActionController::Base 默认启用。查看此提交: https://github.com/rails/rails/commit/ec4a836919c021c0a5cf9ebeebb4db5e02104a55

    
    *   Protect from forgery by default
    
        Rather than protecting from forgery in the generated ApplicationController,
        add it to ActionController::Base depending on
        `config.action_controller.default_protect_from_forgery`. This configuration
        defaults to false to support older versions which have removed it from their
        ApplicationController, but is set to true for Rails 5.2.
    

    在官方文档中:https://edgeguides.rubyonrails.org/configuring.html

    config.action_controller.default_protect_from_forgery determines whether
    forgery protection is added on ActionController:Base. This is false by default.
    

    【讨论】:

    • 那么我们现在在哪里修改设置,所以我可以添加prepend: true
    • 我读到的方式是,如果您使用的是 Rails 5.2+,则不需要添加它,默认情况下它是打开的。 @Tallboy
    • @Tallboy 可能您必须像过去一样将其显式添加到控制器中。
    猜你喜欢
    • 2015-03-19
    • 2010-12-20
    • 2011-07-26
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2014-07-09
    • 2014-09-20
    • 2011-08-30
    相关资源
    最近更新 更多