Ruby on rails Rails:如何在Rails API模式下实现保护_免受_伪造
我有一个Rails 5 API应用程序(Ruby on rails Rails:如何在Rails API模式下实现保护_免受_伪造,ruby-on-rails,security,csrf,ruby-on-rails-5,Ruby On Rails,Security,Csrf,Ruby On Rails 5,我有一个Rails 5 API应用程序(ApplicationControllerCookies中没有任何内容。我在工作应用程序的cookies中看到了一堆类似于\u my\u app-session的东西。如果您使用的是Rails 5 API模式,您不会在任何视图中使用保护\u免受伪造或包含,因为您的API是“无状态的”。如果您打算使用完整Rails(而不是API模式),同时将其用作其他应用程序/客户端的REST API,那么您可以执行以下操作: protect_from_forgery un
ApplicationController
)。需要为这个API的一个端点添加一个简单的GUI表单
最初,当我试图呈现表单时,我得到了ActionView::Template::Error undefined method protect\u from\u forgery?
。我向该端点添加了include-ActionController::RequestForgeryProtection
和protect\u from\u-forgery with:exception
。果然解决了这个问题
但是,当我尝试提交此表单时,我得到:422
不可处理的实体
ActionController::InvalidAuthenticationToken
。我已经添加了
并验证了meta:csrf-param
和meta:csrf-token
是否存在于我的标题中,以及authenticity\u-token
是否存在于我的表单中。(代币本身各不相同。)
我试过,防止伪造prepend:true,带有:exception
,没有效果。我可以通过以下注释来“修复”这个问题:保护您免受伪造:例外
。但我的理解是,这将关闭我表格上的CSRF保护。(我需要CSRF保护。)
我错过了什么
更新:
为了澄清这一点,该应用程序的99%是纯JSON RESTful API。需要在这个应用程序中添加一个HTML视图和表单。因此,对于一个控制器,我想启用完全CSRF保护。应用程序的其余部分不需要CSRF,可以保持不变
更新2:
我只是将这个应用程序的HTML表单和页眉的页面源代码与我编写的另一个传统Rails 5应用程序进行了比较。标题中的authenticity\u标记
与表单中的authenticity\u标记
相同。在我遇到问题的API应用程序中,它们是不同的。也许是什么
更新3:
好吧,我不认为错配是问题所在。然而,在进一步比较工作应用程序和非工作应用程序时,我注意到网络>Cookies中没有任何内容。我在工作应用程序的cookies中看到了一堆类似于
\u my\u app-session
的东西。如果您使用的是Rails 5 API模式,您不会在任何视图中使用保护\u免受伪造
或包含
,因为您的API是“无状态的”。如果您打算使用完整Rails(而不是API模式),同时将其用作其他应用程序/客户端的REST API,那么您可以执行以下操作:
protect_from_forgery unless: -> { request.format.json? }
module MyApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
config.middleware.use ActionDispatch::Flash
end
end
因此,在适当的时候将调用
protect\u from\u fackery
。但是我在您的代码中看到了ActionController::API
,因此您似乎正在使用API模式,在这种情况下,您可以从应用程序控制器中完全删除该方法无需保护AJAX调用和API不被伪造
如果您想为某些操作禁用它,那么
protect_from_forgery except: ['action_name']
问题是:Rails 5在API模式下,逻辑上不包括Cookie中间件。没有它,就不会有存储在Cookie中的会话
键
,在验证我随表单传递的令牌时使用
有些令人困惑的是,在config/initializers/session_store.rb
中更改内容没有任何效果
我最终在这里找到了这个问题的答案:,这让我来到了这里:它确切地提到了我需要添加到application.rb以恢复工作cookie的行:
config.session_store :cookie_store, key: "_YOUR_APP_session_#{Rails.env}"
config.middleware.use ActionDispatch::Cookies # Required for all session management
config.middleware.use ActionDispatch::Session::CookieStore, config.session_options
这三条线加上:
class FooController < ApplicationController
include ActionController::RequestForgeryProtection
protect_from_forgery with: :exception, unless: -> { request.format.json? }
...
在我的Rails API应用程序中得到了一个CSRF保护的窗体.< /P> <代码>类API:ApLogRoalp>应用程序控制器
class Api::ApiController < ApplicationController
skip_before_action :verify_authenticity_token
end
在\u操作之前跳过\u:验证\u真实性\u令牌
结束
如上所述使用rails 5在使用rails 6 API专用应用程序时,我遇到了这个挑战 以下是我解决问题的方法: 首先,将其包含在您的
app/controllers/application\u controller.rb
文件中:
class ApplicationController < ActionController::API
include ActionController::RequestForgeryProtection
end
或者,如果您希望根据请求格式有条件地保护\u免受\u伪造:
class ApplicationController < ActionController::API
include ActionController::RequestForgeryProtection
protect_from_forgery with: :exception if proc { |c| c.request.format != 'application/json' }
protect_from_forgery with: :null_session if proc { |c| c.request.format == 'application/json' }
end
所以它看起来是这样的:
protect_from_forgery unless: -> { request.format.json? }
module MyApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
config.middleware.use ActionDispatch::Flash
end
end
模块MyApp
类应用程序 #该应用程序是一个纯RESTful API,全部为json。现在需要向这个应用程序添加一个HTML视图和表单。因此,对于该控制器,我想切换出API模式并启用CSRF保护。我确实尝试了此解决方案,但我仍然无法验证CSRF令牌的真实性。
当我尝试发布表单时…供您查看,您使用的是erb
,表单使用的是
?ApplicationController扩展了ActionController::API,您的控制器也扩展了ApplicationController吗?如果是这样的话,试着把它改成MyController类。同样的错误InvalidAuthenticityToken
是的,我的问题是如何为一个控制器的非API调用激活它?我有一个HTML页面和表单需要CSRF保护,应用程序的其余部分是一个RESTful JSON API,不需要它。我假设将protect\u from\u forgery
与include ActionController::RequestForgeryProtection
一起添加到该控制器中即可,但是现在当我尝试提交表单时,我得到了无效身份验证。你们真的确定你们不需要API中的CSRF保护,只需要Ajax调用和cookie吗?你们如何保护你们的API?不清楚为什么你们首先需要在API模式下使用form_标记。API模式的思想是将您的后端作为纯数据端点,而不负责生成任何UIR
module MyApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
config.middleware.use ActionDispatch::Flash
end
end
NoMethodError (undefined method `flash=' for #<ActionDispatch::Request:0x0000558a06b619e0>):