Skip to content

Proposal to update wiki documentation on Rails CSRF protection #1156

Closed

Description

@goldsmithb

The wiki page about mitigating CVE-2025-9284 is out of date. We should update the recommendation to be the one @jkowens originally came up with (source).

Currently, the wiki suggests Rails developers use the omniauth-rails_csrf_protection gem to mitigate the vulnerability. However, that gem's README now states:

OmniAuth has provided a built-in solution to mitigate against CVE-2015-9284 since version 2.0.0. You should be able to mitigate against this vulnerability by adding this configuration to your application:

OmniAuth.config.request_validation_phase = OmniAuth::AuthenticityTokenProtection.new(key: :_csrf_token)

This gem will continued to be maintained as an alternative to the solution above.

This is confusing for Rails developers trying to set up OmniAuth, as it is not clear whether the gem is the best solution or not. This open PR in the OmniAuth repo reflects these changes in the project README, but I think updating this wiki page is also important.

I didn't want to make the edit without allowing for discussion of some kind, so I created this issue. If every thing looks good, I will update the wiki page sometime this week.


The proposed new wiki page is below:


These instructions are up-to-date as of February 21, 2025, and OmniAuth version v2.1.4. They are written to address CVE-2015-9284,
and are consolidated from these discussions (here, here, and here) and account for the changes introduced in OmniAuth v2.0.0.

The Issue

This vulnerability is mitigated by disallowing GET requests during OmniAuth's request_phase and including a CSRF token with requests. OmniAuth by default accomplishes the first mitigation criteria. Although OmniAuth provides most of the functionality needed to mitigate the vulnerability, special action must be taken to ensure CSRF tokens are included and handled properly. This depends on the implementation of your app.

Using GET requests

A key part of resolving this vulnerability is refusing GET requests to /auth/:provider endpoints.

As of OmniAuth v2.0.0, GET requests are disabled by default, so no further action is required. If you are using an earlier version of omniauth, you must configure your application to only accept get requests:

OmniAuth.config.allowed_request_methods = [:post]
# And to prevent OmniAuth from logging a warning with each GET:
OmniAuth.config.silence_get_warning = true

If the use of GET requests to /auth/:provider is essential for your application (for example, if you are ever redirecting to /auth/:provider as part of an authentication process), then you will need to put together a more involved solution for your specific needs. This may mean redirecting to a standard log-in screen which includes link_to 'Log in', '/auth/:provider', method: :post or another POST/form-based approach (like button_to).

Mitigating in Rails applications

Rails has built-in handling for CSRF tokens, but this is not compatible with OmniAuth as is because Rails' security tokens use a custom key.

The recommendation is to configure OmniAuth to use Rails' CSRF token key. Previously the omniauth-rails_csrf_protection gem was recommended to accomplish this, but that is no longer necessary. Simply adding the following config and only using POST requests is sufficient:

  1. Include the following configuration in your application:
# config/omniauth.rb or similar
OmniAuth.config.request_validation_phase = OmniAuth::AuthenticityTokenProtection.new(key: :_csrf_token)
  1. Update all links to /auth/:provider to use POST requests. For example:
link_to 'Sign in via X', '/auth/:provider', method: :post
# or
button_to 'Sign in via X', '/auth/:provider'

Because GET is disabled by default, not doing this will cause the passthru to third-parties to fail, giving this Devise error message: "Not found. Authentication passthru."

  1. If you are continuing to use GET requests to access /auth/:provider then you will need to explicitly enable it. Do so by including the configuration from the Using GET Requests section in your app. (But remember that using GET requests for /auth/:provider is not recommended and you should try to stick to POST requests if at all possible)

Mitigating in non-Rails applications

By default, OmniAuth uses rack-protection's AuthenticityToken class to validate authenticity tokens.

If you are using a rack based framework like sinatra, you can find an example of how to add authenticity tokens to your view here.

Your main responsibility is to include CSRF tokens in your forms:

  1. Ensure your Rack application has rack-protection configured and sessions enabled.

  2. Update all links to /auth/:provider to use POST requests with a CSRF token.
    You'll need forms that POST to /auth/:provider with a hidden field containing
    the session's CSRF token. See the official Sinatra example and this gist for reference.

  3. If you need GET access to /auth/:provider (not recommended), do so by including the configuration from the Using GET Requests section in your app.

OmniAuth.config.allowed_request_methods = [:post, :get]
# And to prevent OmniAuth from logging a warning with each GET:
OmniAuth.config.silence_get_warning = true

Regression Testing

Should you wish to add regression testing to validate that this vulnerability is mitigated then you can consult the following examples:


Activity

  1. xeron commented on Aug 3, 2026

    @xeron

    I tried removing omniauth-rails_csrf_protection gem and adding the suggested line which worked with manual test but broke my system test which does the following:

      test 'user can login with OAuth' do
        login_with_oauth(@user)
    
        assert_current_path user_path
        assert_text I18n.t('users.create.success')
        assert_text @user.name
      end
      OmniAuth.config.mock_auth[:default] = OmniAuth::AuthHash.new(
        info: OmniAuth::AuthHash::InfoHash.new(
          name: 'FirstN123 LastN123',
          email: '[email protected]',
          image: 'https://lh5.googleusercontent.com/-u123/AAAAAAAAAAU/AAAAAAAAABc/gA-Ux123/photo.jpg'
        ),
        provider: 'google',
        uid: '100000000000000000123'
      )
    
      def login_with_oauth(user)
        add_google_mock(user)
    
        visit root_path
        click_button 'login-button'
      end
    
      def add_google_mock(user)
        OmniAuth.config.add_mock(
          :google, info: {
            name: user.name,
            email: user.email
          }
        )
      end
    Error:
    UsersTest#test_user_can_login_with_OAuth:
    OmniAuth::AuthenticityError: Forbidden
        vendor/bundle/ruby/4.0.0/gems/omniauth-2.1.4/lib/omniauth/authenticity_token_protection.rb:27:in 'OmniAuth::AuthenticityTokenProtection#deny'
    

    Reverting back to omniauth-rails_csrf_protection gem fixed system tests.

  2. added theissue type on Oct 17, 2026
  3. sferik commented on Oct 17, 2026

    @sferik
    Contributor

    Thanks for proposing this and for pushing the discussion forward! The README now shows the Rails setup with OmniAuth.config.request_validation_phase = OmniAuth::AuthenticityTokenProtection.new(key: :_csrf_token) instead of the extra gem (#1151), and has a new section explaining OmniAuth's CSRF protection (#1075). Both will ship in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions