Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Could not authorize you from Facebook because "Csrf detected".' #73

Closed
tomsoderlund opened this issue Jul 23, 2012 · 70 comments
Closed

Comments

@tomsoderlund
Copy link

Seems like v1.4.1 introduced an issue with CSRF:

http://stackoverflow.com/questions/11548539/rails-3-2-facebook-auth-csrf-failure
http://stackoverflow.com/questions/11597130/omniauth-facebook-keeps-reporting-invalid-credentials

@amyth
Copy link

amyth commented Jul 27, 2012

@joelvh
Copy link

joelvh commented Jul 27, 2012

Omniauth-oauth2 causes the CSRF detected error because something is redirecting to the callback URL twice in a row. First time the "state" value is there and your callback processes. Then immediately after, something redirects to the callback again, but the second time, the "state" value has been deleted from session and causes the error.

Here is where the state is verified and removed from session.

https://github.com/intridea/omniauth-oauth2/blob/master/lib/omniauth/strategies/oauth2.rb#L71

The question is, what is redirecting to the callback twice in a row? It only happens once in a while for me. My guess is Devise is accidentally storing the callback URL as the redirect URL? But not sure.....

@afeld
Copy link

afeld commented Aug 8, 2012

I'm seeing this twice in my logs each time I authorize:

(facebook) Callback phase initiated.

I'm trying to get post-auth redirects working with omniauth+devise, but it seems that the double redirects are clearing session['omniauth.origin'] as well. As a workaround, I created a new controller action that essentially does the following:

# app/controllers/omniauth_controller.rb

# GET /auth/fb_sso_proxy?return_to=...
def fb_sso_proxy
  session[:user_return_to] = params[:return_to] if params[:return_to]
  redirect_to user_omniauth_authorize_path(:facebook)
end

and do my authorization via Facebook through there - the redirect is then handled by Devise instead of OmniAuth. Would love to be able to remove this, though.

@gaffo
Copy link

gaffo commented Aug 8, 2012

I'm getting the same issue but my error message unhelpful:

Started GET "/auth/facebook/callback?signed_request=REDACTED" for 140.239.148.226 at 2012-08-08 23:18:32 +0000
(facebook) Callback phase initiated.
(facebook) Authentication failure! invalid_credentials: OmniAuth::Strategies::OAuth2::CallbackError, OmniAuth::Strategies::OAuth2::CallbackError

Started GET "/auth/failure?message=invalid_credentials&strategy=facebook" for 140.239.148.226 at 2012-08-08 23:18:32 +0000

ActionController::RoutingError (No route matches [GET] "/auth/failure"):

If I take the csrf check out things work fine

@nogeek
Copy link

nogeek commented Aug 31, 2012

+1 on this however I only get it when using iphone and not through browser

@amichal
Copy link

amichal commented Sep 10, 2012

in my initial tests it seems to be related browser caching. Shift-reload in Chrome works always. If i let the browser request caching it fails (most of the time)

@ryanclark2
Copy link

I am getting this error with or without sandbox enabled. Can't seem to find a real solution...

@quidproquo
Copy link

Got the same problem, seems to only happen when my app gets embedded in the FB Canvas. I just switch back to the 1.4.0 omniauth-facebook gem and it works. I hope someone can fix this issue.

@cybercent
Copy link

+1, same problem, only when using Canvas. Downgraded to 1.4.0 and it seems to work.

@ajbraus
Copy link

ajbraus commented Oct 16, 2012

+1 getting this problem when I try to upgrade.

@idrysdale
Copy link

+1, Downgraded to 1.4.0 and all seems good again?

@jalcine
Copy link

jalcine commented Oct 16, 2012

We shouldn't have to downgrade to get this working.

I propose (since I'm kind of busy but I might try to do this) to do a diff of 1.4.0 and 1.4.1 and see what on Earth changed that made this happen.

@ryanclark2
Copy link

I fixed this in my app and it now works with 1.4.1. Unfortunately, I fixed it many commits ago and am not entirely sure of the solution, but I definitely found that I had the FB credentials in both my devise and my omniauth initializers. This could have been making FB think I was trying to login more than once. Let me know if this helps.. I can take a closer look and see what else I did if this isn't the fix you are all looking for.

@jalcine
Copy link

jalcine commented Oct 20, 2012

@iamryo that makes sense, but if that's the case, when Devise and Omniauth are used in collaboration and a strategy is defined in both libraries, one should take precedence over the other. If they're the same, then just use Devise's configuration.

@cybercent
Copy link

@iamryo , That might be a different problem. I'm not using omniauth-facebook with devise.

@jalcine
Copy link

jalcine commented Oct 22, 2012

I'm not sure if it does, but if Omniauth can communicate with its parent authorization scheme, then it shouldn't be breaking DRY.

@emkman
Copy link

emkman commented Nov 14, 2012

@iamryo Thank you! I had the same problem, where I first defined an omniauth initializer and then added the strategy to the devise config. Removing the omniauth initializer fixed this for me.

@incogneato
Copy link

+1 -- works on 1.4.0, but not 1.4.1. FWIW, I am not using Devise ... just straight-up OAuth2 + omniauth-facebook

@miraclecoder
Copy link

+1 had a devise initializer with an omniauth section and separate omniauth initializer. both defined fb credentials. removing one and restarting worked.

@gmccue
Copy link

gmccue commented Dec 7, 2012

@miraclecoder -- +1 This worked for me too. I'm using Devise and Omniauth-facebook, removing the omniauth Facebook initialization removed the duplicate callback phase.

@mgogov
Copy link

mgogov commented Dec 8, 2012

@iamryo , +1
Removing the devise config from a devise + omniauth app worked fine (omniauth-facebook 1.4.1)

@mariusbutuc
Copy link

  1. Ensured that the FB credentials are initialized only once
  2. Downgraded to omniauth-facebook v1.4.0, that relies on pre-1.1+ omniauth-oauth2 (1.0.3)

Same issue:

Could not authenticate you from Facebook because "Csrf detected".

It happens on heroku, yet I cannot reproduce on dev.

@jeremyhaile
Copy link

Happening for me too with devise and omniauth-facebook. Just spent about 5 hours trying to get it working before finding this!

@deevis
Copy link

deevis commented Jan 27, 2013

FYI - this same 'Csrf detected' problem can also be caused by having a domain name mismatch between your live site and the site url configured within your facebook application.

@pawel2105
Copy link

+1 in case there was any doubt. Removed my omniauth initializer and it started working again (1.4.1)

@betjaminrichards
Copy link

Running into the same issue here with Rails 3.2.13 and Omniauth_facebook 1.4.1 . Downgrading the 1.4.0 didn't fix for me and I'm only calling it in the devise initializer.

@betjaminrichards
Copy link

And the mystery deepens...

Using the exact same account I can authenticate with Facebook correctly using Google Chrome, but using Safari I encounter the afforementioned error.

Safari version: Version 6.0.4 (7536.29.13)
Google Chrome version: Version 27.0.1453.93

@hanchang
Copy link

Since this is open, plus I found this via Google more readily than #107, I'll leave this here in hopes it will help future generations:

I am on omniauth-facebook 1.6.0 and still needed to add provider_ignores_state: true in my Omniauth config like so:

config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], {
  strategy_class: OmniAuth::Strategies::Facebook,
  provider_ignores_state: true,
}

After adding that config option, the CSRF error goes away.

@guilhermesimoes
Copy link

provider_ignores_state: true had already been mentioned. However, be aware that you are making your app susceptible to CSRF attacks by using that option.

provider_ignores_state: true is NOT a permanent solution.

@skull-squadron
Copy link

@guilhermesimoes Is there a non chmod 777 option? As much as I'd like to engage Igor H., I'd prefer that we not have a security fail too soon.

@guilhermesimoes
Copy link

True, you wouldn't want to mess with Homakov.

This is probably not the answer you're looking for but omniauth-facebook should work out of the box. If you use it solo (without any other gem) and set it up properly it should work. I'm using it with devise at this moment and it's working fine as well.

However, it seems to me like any deviation from the normal omniauth-facebook flow seems to trigger CSRF detection. So far, here are some of the things that seem to do it:

  • Defining your app credentials in two different initializers, usually omniauth.rb and devise.rb. source
  • Having a domain name mismatch between your live site and the site url configured within your OAuth application. source
  • Having a cookie domain that doesn't match the redirect_uri. source
  • Leaving the facebook application in sandbox mode, so the domain name doesn't match the production one. source
  • Adding a before_filter :authenticate to the OmniauthCallbacksController or ApplicationController (since OmniauthCallbacksController inherits from ApplicationController). source
  • Using omniauth-facebook in conjunction with Facebook's client-side flow. source
  • Messing with the state param.

@skull-squadron
Copy link

We got a similar to this error by double configuring in an omniauth initializer and devise config.  Nuking the former fixed the csrf weirdness (we're also using twitters secure_headers gem)—
Sent from Mailbox for iPhone

On Tue, Mar 18, 2014 at 6:51 PM, Guilherme Simões
notifications@github.com wrote:

True, you wouldn't want to mess with Homakov.
This is probably not the answer you're looking for but omniauth-facebook should work out of the box. If you use it solo (without any other gem) and set it up properly it should work. I'm using it with devise at this moment and it's working fine as well.
However, it seems to me like any deviation from the normal omniauth-facebook flow seems to trigger CSRF detection. So far, here are some of the things that seem to do it:

@riffraff
Copy link

I just got this error for omniauth-facebook with rails 3.2 and ruby 2.1.1 (it also occurs with omniauth-google_oauth2).

omniauth-facebook 1.6.0.

In my case it appears to be related to having cross-domain client-side sessions set up with

 My::Application.config.session_store :cookie_store,
                                    :key => '_my_app_session',
                                    :domain => :all,
                                    :tld_length => 2

removing the last 2 lines fixed it.

@nneal
Copy link

nneal commented Jun 25, 2014

I just got this issue as well. Rails 4.1.1, omniauth 1.6.0, Devise 3.2.4. I'm configuring FB auth only in one place (Devise). is only happening for some users sometimes, and I couldn't find a link between the cases. I'll report back if I can find a fix.

PS: downgrading to 1.4.0 caused other, more substantial bugs

@zacksiri
Copy link

+1

1 similar comment
@kathgironpe
Copy link

+1

@simi
Copy link
Owner

simi commented Jul 6, 2014

Can anyone modify repo example to get this error?

@scomma
Copy link

scomma commented Jul 18, 2014

+1 Still getting this intermittently (quite randomly) with 1.6.0. Impossible to debug.

@mszyndel
Copy link
Contributor

Same here, problem randomly occurring on 1.6.0 but ONLY on a test host, maybe it has something to do with Facebook settings? Rails, Devise, etc versions the same between the instances.

@sydneyitguy
Copy link

+1 with omniauth-facebook (1.6.0) / omniauth-oauth2 (1.2.0) / omniauth (1.2.2)

@mszyndel
Copy link
Contributor

mszyndel commented Aug 5, 2014

Ok, me problem lied in the fact that I had HTTP basis auth in application controller which kicked in AFTER omniauth but before the actual request. Browser behaviour was to redo the request, but state param was already removed from session by the first request.

Probably not a case for most of you but may help someone.

@joshco
Copy link

joshco commented Oct 21, 2014

Ahh.I also have httpauth invovled

@ameyspatil
Copy link

+1

@nitsujri
Copy link

Removing config.omniauth :facebook, ... from initializers/devise.rb made it work for me.

I was double loading it within omniauth-facebook.rb from mixing two tutorials.

@meconlin
Copy link

Problem

Safari browser facebook login throws csrf_token error.
This only happened on development and staging environments.

Setup

I had this issue using:

  • rails 4
  • devise 3.4.0

Fix

I removed http_auth from my dev and staging environments controllers.

@AlvaroOlivencia
Copy link

Upgrading to omniauth-facebook 2.0.0 fixed the issue for me (using FB JS SDK).
From the changelog: fix CSRF exception when using FB JS SDK and parsing signed request (765ed9, @mkdynamic)

@simi
Copy link
Owner

simi commented Jan 3, 2015

Thanks for info @AlvaroOlivencia!

@hecbuma
Copy link

hecbuma commented Jan 30, 2015

For me was HTTP Auth the issue, I just get rid of it and that was all

@BenV
Copy link

BenV commented Feb 11, 2015

We ran into this issue after turning on encrypted cookies in Rails. Turns out we were making a simultaneous request when starting the auth flow which was overwriting the omniauth state. This didn't happen before encrypted cookies because they were only sent when changed. This post sums the issue up nicely: http://techbrahmana.blogspot.com/2013/10/invalid-credentails-omniauth-oauth2.html

@mirzoz
Copy link

mirzoz commented Jan 12, 2016

@emkman, your method really works! thanks bro!

@davidalejandroaguilar
Copy link

True, you wouldn't want to mess with Homakov.

This is probably not the answer you're looking for but omniauth-facebook should work out of the box. If you use it solo (without any other gem) and set it up properly it should work. I'm using it with devise at this moment and it's working fine as well.

However, it seems to me like any deviation from the normal omniauth-facebook flow seems to trigger CSRF detection. So far, here are some of the things that seem to do it:

  • Defining your app credentials in two different initializers, usually omniauth.rb and devise.rb. source
  • Having a domain name mismatch between your live site and the site url configured within your OAuth application. source
  • Having a cookie domain that doesn't match the redirect_uri. source
  • Leaving the facebook application in sandbox mode, so the domain name doesn't match the production one. source
  • Adding a before_filter :authenticate to the OmniauthCallbacksController or ApplicationController (since OmniauthCallbacksController inherits from ApplicationController). source
  • Using omniauth-facebook in conjunction with Facebook's client-side flow. source
  • Messing with the state param.

So omniauth-facebook is not compatible with Facebook's client-side flow?

@giandevsecops
Copy link

*SOLUTION that worked for me: clearing computer temps and cache, and clearing the browser. Reopening app. CSFR is a Cross Site Request Forgery. So, in this case, either it is a real attack happening or the site/app OKTA SSO verification system just thinks there is a Csfr going on. If it is not a Persistent attack, then clearing the cache/temps and browser and reopening might do. if not do that but also restart computer. Ultimately do clear cache but switch browser/app if possible for what is intended. Last but not least, if none of that work give your computer and network a thorough check up, might be actually infected. Of course, you could also just report the issue from beginning but might have to wait. I did the steps I stated here and it work for me without need of escalation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests