【问题标题】:"CSRF detected" with Omniauth and Google使用 Omniauth 和 Google “检测到 CSRF”
【发布时间】:2014-04-18 15:09:28
【问题描述】:

我收到了

OmniAuth::Strategies::OAuth2::CallbackError 在 /auth/google/callback csrf_detected |检测到 CSRF

我的代码:

require 'sinatra'
require "sinatra/json"
require "sinatra/config_file"
require 'omniauth-oauth2'
require 'omniauth-google-oauth2'

use Rack::Logger

config_file "config/app_config.yml"
use Rack::Session::Cookie, secret: '5fb7w345y3489f523y4h'

configure do
  enable :sessions
end

use OmniAuth::Builder do
  provider :google_oauth2, settings.google[:client_id], settings.google[:secret],
    {
      :scope => "userinfo.profile",
      :access_type => "offline",
      :prompt => "select_account consent",
      :name => "google"
    }
end

get '/list' do
  json get_list
end

get '/' do
  %Q|<a href='/auth/google'>Sign in with Google</a>|
end

get '/auth/:name/callback' do
  @auth = request.env['omniauth.auth']
  @auth.inspect
end

我的回调同时返回代码和状态。

【问题讨论】:

  • 我刚刚重启了服务器,一切都很好:P

标签: ruby oauth-2.0 sinatra omniauth


【解决方案1】:

/config/initializer/session_store.rb 中定义的域与google api 控制台中定义的origin/redirect_uri 不同时,rails 会出现此问题。

MyApp::Application.config.session_store :cookie_store, key: '_app_session', domain: 'my_app.com'

删除域参数或在双方使用相同的域解决了问题。

【讨论】:

  • 还要检查浏览器中是否启用了 cookie,这修复了我的 CSRF 检测错误,我的 /config/initializer/session_store.rb Rails.application.config.session_store :cookie_store, key: '_XXX-XXXXX_session'
【解决方案2】:

遇到同样的问题

(google_oauth2) 回调阶段已启动。
(google_oauth2) 验证失败! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF 检测到

上次 Omniauth-oauth2 更新引入的“状态”参数有一个必填字段。

有些人建议使用 provider_ignores_state: true 但这是个坏主意,因为它引入了 csrf 缺陷

猜想我们必须降级到以前的版本才能保持 google_oauth2 正常工作。

发到https://github.com/intridea/omniauth-oauth2/issues/58

【讨论】:

  • 一个网站对我做了这个。作为用户,我有什么解决方法吗?
  • 那么是什么版本使这个工作?还要在哪里设置“provider_ignores_state: true”?
  • 正如他们的 GitHub 存储库所建议的那样,provider_ignores_state 仅适用于 AJAX 流。 github.com/zquestz/omniauth-google-oauth2/blob/…
【解决方案3】:

如果您使用带 OmniAuth 的 Devise,您需要跳过额外的 omniauth.rb 初始化文件,只需在您的初始化程序/devise.rb 中添加 config.provider "KEY"、"SECRET",然后继续您的实现。

【讨论】:

  • 这为我解决了问题,而不是其他问题——我已经在会话存储配置中拥有 domain: :all。
  • 也为我们修复了...修复它的原因是因为如果您在两个位置都有代码,它会两次从omniauth gem 中触发此代码,第二次是omniauth。 state 为 nil,因为它在第一次迭代中被删除。 elsif !options.provider_ignores_state && (request.params["state"].to_s.empty?|| request.params["state"] != session.delete("omniauth.state")) 失败!(:csrf_detected, CallbackError .new(:csrf_detected, "检测到 CSRF"))
【解决方案4】:

您是否回击并重新尝试登录?我遇到了这个问题,这真的让我很困惑,但这是因为我要回去重试。如果我再次输入地址,我不会得到问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 2011-01-13
    • 1970-01-01
    相关资源
    最近更新 更多