-
-
Notifications
You must be signed in to change notification settings - Fork 303
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
google_oauth2 Authentication failure! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF detected #58
Comments
OmniAuth already creates the state for you, why are you trying to override it? Also, when you run |
I don't. it was to be sure the state token was the same (through loggers).
|
This should be working out of the box. In what specific omniauth gem is this happening? How are you configuring it? |
Just omniauth with google_oatuh2 provider. Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, Settings.google_oauth2.app_id, Settings.google_oauth2.app_secret, {
prompt: 'select_account',
image_aspect_ratio: 'square',
image_size: 200
}
end |
I think you made a copy paste mistake, Either way, make sure that |
lol yeah. fixed ;) Well everything works fine when I use the ugly
option. For the record I also use other options, but quite sure it has nothing to do with the present problem :
|
Yeah, this issue shouldn't have anything to do with your configuration. However, given OmniAuth's history of catching client errors and throwing random errors, I wouldn't dismiss this just yet. When you run bundle show, what is the version of the |
/usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/omniauth-google-oauth2-0.2.3
|
After some tests, its appear that session['omniauth.state'] is nil during the callback phase! |
This problem occurs with rails when the domain defined in MyApp::Application.config.session_store :cookie_store, key: '_app_session', domain: 'my_app.com' Removing the domain params or using the same domain on both sides (plus activating contacts and google+ APIs in console...) fixed the problem for me. I think we can close this issue! 😸 |
I had this exact same problem and adding |
I'm running into this in a Sinatra app using |
@gdurelle why close?
|
See comments above, seems it's just a domain problem; besides the I'm closing because it's no longer an issue. At most a configuration need, badly documented. But if you think there's a real technical problem, i'll reopen it right away. |
@gdurelle Thanks for clarifying. I ran into the same problem with omniauth-github, and |
I have been wrestling with this all day, but none of the above helped. I finally realized that this is happening to me on staging because staging is at staging.example.com and production is at example.com, so both production and staging cookies are being sent to staging. Staging will work most of the time, but then if I go use production and come back to staging, I get one more valid request and then need to re-authenticate. After returning from authenticating with Google, it threw csrf_detected until I cleared cookies. It would then work fine until some seemingly random time in the future that I now attribute to my having interacted with production. I'm leaving this note here because Google kept leading me back to this issue, so perhaps it will help someone else. |
I was getting this error in a rails app and the way to reproduce it was to initiate two oauth2 logins at the same time. This caused one of the logins to fail with CSRF error, because its random string in the state params get invalidated by the second login request. Just something to keep in mind if you get this error from time to time and you can't figure out why. |
Hey all, I'd just like to point out that this is still broken under some conditions: namely, if you start making multiple requests from you're app, and during that time initiate an auth request, there's a race condition that causes the session cookie that holds the state to be clobbered. Specifically, Just wanted to add to the thread, as I wound up here after some quick googling as well. I solved this by doing something specific within my app space (message if you'd like to hear what, but its more of a case-by-case thing) - if theres something I'm missing though that would have solved this would love to hear it! |
Strangely, I'm seeing this on iOS safari, but not iOS chrome or mac/pc |
@gdurelle even with the session domain set this does not work. |
For whatever its worth , I had all these issues because I had my clientId and secret in both my omniauth.rb file as well as my devise.rb config file. I mistakenly did this following the omniauth-google-oauth2 gem instructions. Now I only have the line below in devise.rb. No omniauth.rb file needed. |
@austinwang wow! i gave up and moved to using the gplus provider. |
@austinwang +1 |
@daniel-nelson - nice catch |
@daniel-nelson - thanks! |
Is this issue related to google issue https://code.google.com/p/gdata-issues/issues/detail?id=6628 wherein occasionally two requests are sent back from google account login? |
It's weird that it redirects me to the correct GitHub application, and asks me to authorize, hence the credentials are correct. Weird that this error is in the callback phase. Something to mention, I don't have this error locally. Update: I managed to fix my issue, it wasn't related to this GitHub issue. |
I had a really bad time with this error and wanted to leave a comment to help out anyone in the same situation. I was specifically using an omniauth-github strategy. In the controller action that renders the view with your authentication link on it, include the line:
And in the link itself, include that session key as a query parameter called 'state'. I put this method in application helper so I could call it from the view:
|
I also had a problem with this and by chance I found out that visitors using naked domain (non-www) got this error. After some digging I found the source to my problem:
There are a few different solutions to this:
I went with redirecting the traffic to www, since it was better suited for my given situation. There are multiple ways to redirect the traffic, e.g using Rails routes:
|
i've been banging my head against this issue for far too long to not leave a comment here letting others know what worked for me. for context: i'm using oauth within a mounted rails engine. the issue was that the main rails app the engine was mounted in was using https via if you're reading this, i hope it helps you to not lose two and a half days to debugging like i did. godspeed! |
FWIW, if anyone else comes here with the same issue - I had the same error happen with Hybrid Auth; adding provider_ignores_state in omniauth setup fixed it. Then another separate bug appeared with invalid_client; which was way more easier to handle - PEBKAC :) |
With the hybrid auth approach, it seems that the state is never set locally (usually done at https://github.com/intridea/omniauth-oauth2/blob/master/lib/omniauth/strategies/oauth2.rb#L58). Anyone know if its safe to set |
If you got this error with Safari, then try Firefox and Chrome. If only Safari has this issue, then my answer may help, because I spend hours to figure it out. The problem is, like this URL http://example.org/auth/my-strategy, Safari will send two requests. The first request to onmiauth, will generate a The second request is going after the first request, it makes Omniauth generate another See, the callback To fix it, I add a timestamp to login URL, http://example.org/auth/my-strategy?t=12345678, Safari won't request this URL twice when I click it. |
I found this issue myself today, and thanks to @N0hbdy's suggestion. I had a regular AJAX ping going on in the browser which was firing at a similar time as the navigation to the Ended up aborting and unbinding the AJAX request on page unload to prevent the race. |
@marcusmalmberg , your post solved it for me! If working with multiple domains, you will have different session cookies. So if you use |
here is my way |
Changing |
Not sure if this will help anyone, but I had multiple unicorn workers and I believe each one had a different session secret because of me using I am using Sinatra, and unicorn currently. |
I just had the same issue. Not sure what the state parameter has to do with csrf but that is what caused the issue. I had tried creating the link like so Instead creating the link like this works for me |
Unrelated to the above, but, I just ran into this issue and googling brought me here. My issue was that on The weird thing which I don't understand yet is that if a user went directly to the url which then issued a redirect to the If however the user first went to any other page to get past the I don't quite understand why the |
Things were working fine for google & facebook but I couldn't get twitter/github or linkedin oauth working. I found lots of ways people tried to fix this but in the end it was because I had upgraded an old app to rails 5 and the cookie session store that was a default initializer was removed in rails 5 and built into the app. You can still override it which I think was causing a conflict in the domain used. If you are running into this problem with an app that was upgraded from 4->5 try commenting out Rails.application.config.session_store in the session_store.rb initializer and see if it helps. |
That was a saviour, thank you :-) so people who just do google auth with google-omniauth and then change their mind putting devise in mix, will end up in this situation. |
This resolved it for me! |
For me the reason this happened was non of the above so I thought I'd share what solved it for me. Maybe it can save someone else a few hours of headache in the future. Following this change introduced in Google Chrome behavior, we started adding So as a result, my session cookie wouldn't get created on the browser because Chrome would ignore its So if your app is setting
Or
Hope this helps. Update (Nov 25th 2021):
|
Clear coockies helped me |
Having a similar issue after upgrading to Rails 7, and only with Safari. Firefox/Chrome work fine. Really bizarre. Any tips on how to troubleshoot / fix this? |
Even when creating the state myself it got the same error.
Even though the returned state is the same, meaning it should match !
Isn't there a double redirect or something ?
Because if so the :
in https://github.com/intridea/omniauth-oauth2/blob/master/lib/omniauth/strategies/oauth2.rb#L72 would explain the problem.
=> First passes, second fail.
https://stackoverflow.com/questions/22386149/why-am-i-getting-csrf-detected-with-omniauth-and-google/23739564#23739564
The text was updated successfully, but these errors were encountered: