【问题标题】:Rails 4 ActionController::InvalidAuthenticityToken with multiple subdomains具有多个子域的 Rails 4 ActionController::InvalidAuthenticityToken
【发布时间】:2015-10-31 18:40:16
【问题描述】:

在我说之前我在 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'

我有以下结构文件夹:

- 应用程序
-- 控制器
--- 应用程序1
---- Welcome_controller.rb
--- 帐户
---- Welcome_controller.rb
--views
--- 布局
---- app1.html.erb
---- account.html.erb
--- 应用程序1
---- 欢迎
----- index.html.erb
--- 帐户
---- 欢迎
----- index.html.erb

在:app/views/layout/app1.html.erb (http://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)上,我在 标签之前有这一行:

<%= 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'

【问题讨论】:

  • 请向我们展示查看代码和相关的日志文件。
  • 嗨@Зелёный,查看日志。我更新了帖子。您需要在哪个视图中显示代码?
  • 尝试将app/controllers/application_controller.rb 切换到protect_from_forgery with: :null_session 并阅读Rails 中的CSRF
  • @Зелёный 但这不会打开安全漏洞?
  • 为了防止所有其他伪造请求,我们引入了我们的站点知道但其他站点不知道的必需安全令牌。我们在请求中包含安全令牌并在服务器上对其进行验证。

标签: ruby-on-rails session ruby-on-rails-4 subdomain


【解决方案1】:

现在不可能:删除对子域的请求。

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 动作的表单标签的动作属性的情况下,攻击者可以将 href 或动作设置为“ https://attacker.com"(注意前导空格)将传递给 JQuery,JQuery 会将其视为同源请求,并将用户的 CSRF 令牌发送到攻击者域。

这是 jquery-ujs 的 commit

您可以阅读更多here

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 2018-11-02
    • 2018-11-27
    • 2015-11-23
    • 2017-11-13
    • 1970-01-01
    • 2020-05-15
    • 2016-11-14
    • 2019-12-25
    相关资源
    最近更新 更多