Help with CSRF on Rodauth + Omniauth - API Only (Secure cookie) #23
-
Greetings! I've set up a new application using the latest versions of everything. In the context of my question, I've configured Rails, Rodauth-rails, and Rodauth-omniauth. (I once implemented Argon2 integration in Rodauth and migration mechanisms and was pleasantly surprised by the integration in Rodauth-rails—thanks to the author!) In our application, we don’t plan to use multiple domains, so we intend to use cookies for user identification. As I understand, this is well-supported by Omniauth itself and, seemingly, also by Rodauth and Rodauth-rails, though I might be mistaken. Ideally, I would like to enable CSRF protection to generally reduce the potential risks associated with using cookies. I’ve seen there is a guide for using Omniauth in JSON (i.e., API) mode and saw mentions of built-in CSRF protection without using an external gem. However, I couldn’t find any information in the documentation or discussions on how to use CSRF protection in API mode. In a discussion similar to this one link, I encountered the following error: Started GET "/auth/github/callback?code=...&state=..." for 127.0.0.1 at 2024-10-31 23:28:28 +0500
ActiveRecord::SchemaMigration Load (1.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC /*application='RailsBackend'*/
D, [2024-10-31T23:28:28.653328 #3049637] DEBUG -- omniauth: (github) Setup endpoint detected, running now.
D, [2024-10-31T23:28:28.653372 #3049637] DEBUG -- omniauth: (github) Callback phase initiated.
E, [2024-10-31T23:28:28.656543 #3049637] ERROR -- omniauth: (github) Authentication failure! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF detected
Started GET "/favicon.ico" for 127.0.0.1 at 2024-10-31 23:28:28 +0500 At this stage, I don’t understand how to correctly enable CSRF protection and assume it should be implemented in the Rails way through X-CSRF-Token. Moreover, I believe there are two distinct phases for CSRF tokens:
I will elaborate on the second phase. When the backend is a public API, as I understand it, it’s not enough to simply issue a CSRF token since it could easily be obtained, rendering it ineffective. Therefore, it would likely be correct to issue the CSRF token only during authentication. Currently, I'm not entirely sure how this should work, and I couldn't immediately find any guidance or recommendations in the Rodauth/Rodauth-rails documentation—on how to issue a CSRF token in API mode or in similar edge cases. I believe this question likely relates directly to Rodauth itself, stemming from its initial design for server-side rendering, with API mode support provided by the community. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
Strangely, this solution #17 (comment) doesn't allow me to get rid of the error, I was planning to use this at least as a temporary measure. Started POST "/auth/github" for 127.0.0.1 at 2024-11-01 01:07:58 +0500
D, [2024-11-01T01:07:58.348066 #3085463] DEBUG -- omniauth: (github) Setup endpoint detected, running now.
D, [2024-11-01T01:07:58.348131 #3085463] DEBUG -- omniauth: (github) Request phase initiated.
Started GET "/auth/github/callback?code=...&state=..." for 127.0.0.1 at 2024-11-01 01:08:05 +0500
D, [2024-11-01T01:08:05.620654 #3085463] DEBUG -- omniauth: (github) Setup endpoint detected, running now.
D, [2024-11-01T01:08:05.620679 #3085463] DEBUG -- omniauth: (github) Callback phase initiated.
E, [2024-11-01T01:08:05.622222 #3085463] ERROR -- omniauth: (github) Authentication failure! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF detected
Started GET "/favicon.ico" for 127.0.0.1 at 2024-11-01 01:08:05 +0500 |
Beta Was this translation helpful? Give feedback.
-
What method of handling unverified requests are you using in
In that case, you have to figure out why the CSRF check failed for the request phase. Rails should read it from |
Beta Was this translation helpful? Give feedback.
-
Lack of a cookie received at the |
Beta Was this translation helpful? Give feedback.
Lack of a cookie received at the
POST /auth/:provider_name
step on the client results inAuthentication Error! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF detected
at thecallback
step when your strategy is based onomniauth-oauth2