skip to content

After a Rails app upgrades to OmniAuth 2, its GitHub sign-in link 404s and the replacement POST button fails until omniauth-rails_csrf_protection is added — why?

level: seniorimportance: should knowfreq 40%

answer

  1. CVE-2015-9284, login CSRF
  2. allowed_request_methods defaults to [:post]
  3. rack-protection checks session[:csrf]
  4. Rails keeps session[:_csrf_token]
  5. request_validation_phase = TokenVerifier

basics

~20 s

OmniAuth 2 handles /auth/:provider on POST only, so a GET link falls through to Rails and 404s. Its default CSRF check is rack-protection's, which cannot read Rails' token; omniauth-rails_csrf_protection swaps in a verifier built on Rails' own forgery protection.

solid answer

~40 s

OmniAuth 1 started the request phase on GET, so any page could trigger `/auth/github` with a link or an image and, for a user already authorised at the provider, complete a sign-in or account connection the user never asked for (CVE-2015-9284). OmniAuth 2 sets `allowed_request_methods` to `[:post]`; a GET to `/auth/github` is no longer intercepted, reaches the Rails router, finds no route and 404s. The POST must also pass `request_validation_phase`, which by default is `OmniAuth::AuthenticityTokenProtection`, a rack-protection check against `session[:csrf]`. Rails stores its token in `session[:_csrf_token]`, so a Rails form's token fails and the strategy fails with `authenticity_error`, landing on `/auth/failure`. The `omniauth-rails_csrf_protection` gem's railtie replaces that phase with a `TokenVerifier` that calls Rails' `verified_request?` and raises `ActionController::InvalidAuthenticityToken` on a bad token. Re-enabling GET is the wrong fix.

code

ruby · 10 lines
ruby
# Gemfile
gem "omniauth"
gem "omniauth-rails_csrf_protection"

# What the gem's railtie does at boot:
OmniAuth.config.request_validation_phase =
  OmniAuth::RailsCsrfProtection::TokenVerifier.new

# Do NOT do this to make an old link work again:
# OmniAuth.config.allowed_request_methods = [:get, :post]

go deeper

for a junior

Remember that OmniAuth 2 needs a POST to /auth/:provider, so the sign-in control is button_to, and that Rails apps add omniauth-rails_csrf_protection.

for a middle

Explain the two separate checks: the allowed request methods, and the request validation phase with its mismatched session key.

for a senior

Diagnose the 404 and the authenticity_error as two distinct failures, explain the login-CSRF attack the change closes, and reject the fixes that reopen it.

for a principal

Treat third-party sign-in entry points as state-changing endpoints in security review, and make the CSRF posture of every middleware explicit rather than inherited.

## The vulnerability behind the change In OmniAuth 1, visiting `/auth/:provider` with a **GET** started the **request phase**: the middleware redirected the browser to the provider. A GET can be fired by any page the victim visits, through a link, an image tag or a redirect. For a user who had already authorised the app at the provider, the provider often redirected straight back to the callback without asking again. An attacker could therefore drive a signed-in user through a sign-in or "connect your account" flow they never started. That class of bug is tracked as **CVE-2015-9284**, and the OmniAuth source still points to it in the warning it logs when GET is allowed. ## What OmniAuth 2 changed `OmniAuth.config.allowed_request_methods` now defaults to **`[:post]`**. The strategy starts the request phase only when the method is in that list. Anything else on `/auth/github` is **not intercepted**: the middleware passes it on, Rails' router finds no route for `GET /auth/github`, and the user sees a 404. Devise apps see the same thing: `devise_for` declares the authorize route with the verbs in `allowed_request_methods`, so a GET matches no route there either. Either way the request never reached OmniAuth. The fix on the page is a form, not a link: ```erb <%= button_to "Sign in with GitHub", "/auth/github", data: { turbo: false } %> ``` `button_to` renders a POST form with Rails' `authenticity_token` field. `data: { turbo: false }` keeps Turbo from submitting it with `fetch`, which cannot carry a cross-origin redirect as a page visit. ## Why the POST still fails without the gem Before calling the strategy's request phase, OmniAuth runs `OmniAuth.config.request_validation_phase`. Its default is **`OmniAuth::AuthenticityTokenProtection`**, a subclass of rack-protection's `Rack::Protection::AuthenticityToken`. That check keeps its own secret in **`session[:csrf]`**. Rails keeps its token in **`session[:_csrf_token]`** and masks it into each form. The two never match, so the check raises `OmniAuth::AuthenticityError`, the strategy calls `fail!(:authenticity_error)`, and the default failure endpoint redirects to `/auth/failure?message=authenticity_error&strategy=github`. ## What omniauth-rails_csrf_protection does The gem is tiny. Its railtie runs one line at boot: ```ruby OmniAuth.config.request_validation_phase = OmniAuth::RailsCsrfProtection::TokenVerifier.new ``` `TokenVerifier` includes `ActionController::RequestForgeryProtection`, reads its settings from `ActionController::Base.config`, and calls Rails' own `verified_request?` on the incoming request. A bad or missing token raises `ActionController::InvalidAuthenticityToken`, which OmniAuth turns into a failure. Because it shares Rails' configuration, it also honours `allow_forgery_protection = false`, which the generated `config/environments/test.rb` sets, so integration tests can post to `/auth/github` without a token. | Setup | GET /auth/github | POST with Rails token | |---|---|---| | OmniAuth 1 | starts sign-in (vulnerable) | starts sign-in | | OmniAuth 2 alone | 404 from the router | fails with `authenticity_error` | | OmniAuth 2 + the gem | 404 from the router | starts sign-in | The gem's README also names an alternative without it: `OmniAuth.config.request_validation_phase = OmniAuth::AuthenticityTokenProtection.new(key: :_csrf_token)`, pointing rack-protection at Rails' session key. ## Fixes that reopen the hole - **`OmniAuth.config.allowed_request_methods = [:get, :post]`** brings GET back. OmniAuth logs a warning about it (silenced only with `silence_get_warning`), and Rails' `verified_request?` returns true for every GET, so the token check does not protect that path. - **`OmniAuth.config.request_validation_phase = nil`** removes the check entirely, so any site can post the form. - Skipping Rails' forgery protection on a controller does nothing here, because the request phase runs in middleware before any controller.

  • Why does adding skip_forgery_protection to your callback controller not affect the request phase?
    The request phase runs inside the OmniAuth middleware, before the router or any controller. A controller-level `skip_forgery_protection` only changes Rails' check in that controller's actions, such as a callback that a provider POSTs to. The request phase's check is whatever `OmniAuth.config.request_validation_phase` holds.
  • Does the CSRF token in the request phase protect the callback too?
    No. The callback is a redirect from the provider and carries no Rails token. Binding the callback to the browser that started the flow is the strategy's job, typically through a state value it parks in the session before the redirect; that relying-party mechanism belongs to the OAuth layer, not to this gem.

  • Set allowed_request_methods to [:get, :post] so the old links keep working.
  • omniauth-rails_csrf_protection is what makes OmniAuth reject GET requests.
  • A 404 on /auth/github means the strategy gem is missing.
  • skip_forgery_protection on the sessions controller fixes the authenticity_error.
  • OmniAuth 2 reads Rails' authenticity token out of the box.

Assumes OmniAuth 2.1 with omniauth-rails_csrf_protection 2.0; OmniAuth 2.0 made POST the only default request method.