Rails 4中多个子域名下发生ActionController::InvalidAuthenticityToken错误

4

在此之前,我要说一下我已经在谷歌和Stackoverflow上搜索了很多帖子,但是没有一个能够解决我的问题,因此我创建了一个新的帖子。

我正在创建一个具有两个子域的应用程序:account.psantos.dev和app.psantos.dev。

config/initializers/session_store.rb中,我有:

Rails.application.config.session_store :cookie_store, key: '_psantos.co.ao_session', domain: 'psantos.dev'

我有以下文件夹结构:

- app
-- controllers
--- app1
---- welcome_controller.rb
--- account
---- welcome_controller.rb
--views
--- layout
---- app1.html.erb
---- account.html.erb
--- app1
---- welcome
----- index.html.erb
--- account
---- welcome
----- index.html.erb

app/views/layout/app1.html.erbhttp://app1.psantos.dev)中,我有以下一行:

<li><%= "Logout", account_sign_out_url, method: :delete %></li>

当我点击这个链接(将跳转到:http://account.psantos.dev),我收到了以下错误:
ActionController::InvalidAuthenticityToken at /sign_out ActionController::InvalidAuthenticityToken 在两个布局文件(app1.html.erb和account.html.erb)的<\head >标签之前,我有以下行:
<%= csrf_meta_tags %>

我该如何解决这个问题?

更新:相关日志

Started GET "/" for 127.0.0.1 at 2015-08-08 12:37:03 +0100 Processing
by APP1::WelcomeController#index as HTML   Parameters:
{"subdomain"=>"app1"}   Rendered app1/welcome/index.html.erb within
layouts/app1 (0.4ms)   [1m[35mEntity Load (0.3ms)[0m  SELECT 
"entities".* FROM "entities" WHERE "entities"."user_token" = $1 LIMIT
1  [["user_token", "xxxxxxxxxxxxxxxxxxxx"]] Completed 200 OK in 43ms
(Views: 42.0ms | ActiveRecord: 0.3ms)


Started DELETE "/sign_out" for 127.0.0.1 at 2015-08-08 12:37:05 +0100
Processing by Account::SessionsController#destroy as HTML  
Parameters: {"subdomain"=>"account"} Can't verify CSRF token
authenticity Completed 422 Unprocessable Entity in 1ms (ActiveRecord:
0.0ms)

ActionController::InvalidAuthenticityToken -
ActionController::InvalidAuthenticityToken:   actionpack (4.2.3)
lib/action_controller/metal/request_forgery_protection.rb:181:in
`handle_unverified_request'   actionpack (4.2.3)
lib/action_controller/metal/request_forgery_protection.rb:209:in
`handle_unverified_request'   actionpack (4.2.3)
lib/action_controller/metal/request_forgery_protection.rb:204:in
`verify_authenticity_token'   activesupport (4.2.3)
lib/active_support/callbacks.rb:430:in `block in make_lambda'  
activesupport (4.2.3) lib/active_support/callbacks.rb:143:in `block in
halting_and_conditional'   activesupport (4.2.3)
lib/active_support/callbacks.rb:502:in `block in call'   activesupport
(4.2.3) lib/active_support/callbacks.rb:502:in `call'

请展示视图代码和相关的日志文件片段。 - Roman Kiselenko
嗨@Зелёный,看一下日志..我更新了帖子。你需要展示哪个视图的代码? - WeezHard
尝试在 app/controllers/application_controller.rb 中切换到 protect_from_forgery with: :null_session 并阅读有关 Rails 中 CSRF 的内容。 - Roman Kiselenko
@Зелёный 但这不会打开安全漏洞吗? - WeezHard
为了防止所有其他伪造的请求,我们引入了一个必需的安全令牌,我们的网站知道但其他网站不知道。我们在请求中包含安全令牌,并在服务器上进行验证。 - Roman Kiselenko
一种方法是使用rescue_from创建自己的处理程序。 - Roman Kiselenko
1个回答

1
现在无法对子域名进行 :delete 请求。

jquery-ujs 和 jquery-rails 中存在漏洞,可用于绕过 CSP 保护,允许攻击者向攻击者域发送 CSRF 令牌。此漏洞已被分配 CVE 标识符 CVE-2015-1840。受影响版本:所有版本。不受影响的应用程序:不使用 jquery-ujs 或 jquery-rails 的应用程序。修复版本:jquery-rails 版本 4.0.4 和 3.1.3,以及 jquery-ujs 1.0.4。影响 ------ 在攻击者可能能够控制锚标签的 href 属性或触发 POST 操作的表单标签的 action 属性的情况下,攻击者可以将 href 或 action 设置为 " https://attacker.com"(注意前导空格),这将传递给 JQuery,JQuery 将将其视为同一源请求,并将用户的 CSRF 令牌发送到攻击者域。

这是链接到jquery-ujs的提交在jquery-ugs中具有差异代码的图像 你可以在这里阅读更多。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接