skip to content

Behind a TLS-terminating proxy, every Rails 8.1 form POST fails with "HTTP Origin header didn't match request.base_url" - which check fires, and how do you fix it?

level: seniorimportance: should knowfreq 32%

answer

  1. on by default since load_defaults 5.0
  2. forgery_protection_origin_check
  3. request.origin vs request.base_url
  4. http scheme because TLS ended upstream
  5. X-Forwarded-Proto or config.assume_ssl

basics

~20 s

forgery_protection_origin_check, on since load_defaults 5.0, compares the browser's Origin with request.base_url. Behind a proxy that ends TLS and sends no X-Forwarded-Proto, base_url starts with http://, so the comparison fails. Forward the scheme or set config.assume_ssl.

solid answer

~40 s

Rails' forgery check has a second half: when `config.action_controller.forgery_protection_origin_check` is true, which `load_defaults` has set since 5.0, `valid_request_origin?` requires `request.origin` to equal `request.base_url` before any token is accepted. `base_url` is built from the scheme Rails sees plus the host and port. A proxy that terminates TLS and forwards plain HTTP without `X-Forwarded-Proto: https` makes Rails see `http://`, while the browser sent `Origin: https://...`, so every POST is unverified even with a perfect token. Fix the inputs, not the check: have the proxy send `X-Forwarded-Proto` (and keep the original `Host`), or set `config.assume_ssl = true` when every request really arrives through TLS. Turning the origin check off or skipping forgery protection hides the symptom and removes a defence. An `Origin: null` header is different: Rails raises `InvalidAuthenticityToken` outright.

code

ruby · 9 lines
ruby
# config/environments/production.rb
Rails.application.configure do
  # Every request reaches Puma through a proxy that terminates TLS
  # and does not send X-Forwarded-Proto.
  config.assume_ssl = true

  # Not a fix: drops the Origin defence app-wide.
  # config.action_controller.forgery_protection_origin_check = false
end

go deeper

for a junior

Recall that Rails can compare the Origin header with the app's own URL as part of forgery protection, and that it is on by default.

for a middle

Explain how request.base_url is built from scheme, host and port, and why both the origin and the token must pass.

for a senior

Diagnose the proxy case from the log line, fix it with forwarded headers or assume_ssl, and reject the shortcuts that disable defences.

for a principal

Discuss who owns scheme and host correctness across proxy, platform and app teams, and how that is tested before deploys.

## The check behind the message Rails' forgery protection has two parts. Besides the token, it can compare the request's **Origin** header with the URL the app believes it is serving. The switch is `config.action_controller.forgery_protection_origin_check`. The module's own default is false, but the 5.0 step of `load_defaults`, included in every later version, sets it to true, so a Rails 8.1 app has it on. `verified_request?` reads, in effect: ```ruby request.get? || request.head? || !protect_against_forgery? || (valid_request_origin? && any_authenticity_token_valid?) ``` Both halves must pass. `valid_request_origin?` returns true when the check is off, when `request.origin` is nil (some clients omit the header), or when `request.origin == request.base_url`. When the origin fails, the logged warning is not the usual `Can't verify CSRF token authenticity.` but: ``` HTTP Origin header (https://give.example.org) didn't match request.base_url (http://give.example.org) ``` That message names the cause directly. ## Why a proxy breaks it `request.base_url` is Rack's `"#{scheme}://#{host_with_port}"`. Rack decides the scheme from, in order, `HTTPS=on`, `X-Forwarded-Ssl: on`, a forwarded proto (`Forwarded` or `X-Forwarded-Proto`), and finally the connection's own `rack.url_scheme`. When a load balancer or reverse proxy terminates TLS and talks plain HTTP to Puma: 1. the browser posts to `https://give.example.org` and sends `Origin: https://give.example.org`; 2. the proxy forwards over HTTP, and if it adds no forwarded-proto header, Rails sees scheme `http`; 3. `base_url` becomes `http://give.example.org`, the strings differ, every non-GET request is unverified, and with the default `:exception` strategy every form answers 422. The same failure appears when the proxy rewrites `Host` to an internal name or adds a port, because `host_with_port` is part of `base_url` too. ## Fixes, from best to worst | Fix | When it fits | |---|---| | Proxy sends `X-Forwarded-Proto: https` and preserves `Host` | the proxy is configurable; Rails then reports the real scheme everywhere, including generated URLs | | `config.assume_ssl = true` | every request reaches the app through a TLS-terminating proxy; Rails treats them all as HTTPS | | `forgery_protection_origin_check = false` | only as a stopgap; it drops a defence for the whole app | | `skip_forgery_protection` | never for this; it removes the token check as well | Two near misses often get tried first: - **`config.action_dispatch.trusted_proxies`** tells `request.remote_ip` which proxy addresses to skip when reading `X-Forwarded-For`. It does not change the scheme. - **`config.hosts`** is host authorization: it rejects requests whose `Host` is not allowed. It has nothing to do with the Origin comparison. `config.force_ssl = true` alone does not help: it redirects any request Rails sees as HTTP to HTTPS, and behind a plain-HTTP proxy that produces a redirect loop rather than a correct scheme. Since 8.1.1 a newly generated app that deploys with Kamal leaves `assume_ssl` and `force_ssl` commented out in `production.rb`, while an app generated with `--skip-kamal` has both enabled; check which one you are running before reasoning about the scheme. ## The `null` origin Some requests arrive with the literal header `Origin: null`; Rails' own error message names the usual cause, a `no-referrer` Referrer-Policy. Rails refuses to guess: with the origin check on, `valid_request_origin?` **raises** `ActionController::InvalidAuthenticityToken` with a message suggesting a less strict `Referrer-Policy` such as `same-origin` or `strict-origin`. Because it raises inside the check, the configured strategy is bypassed: even a controller on `:null_session` gets the exception. ## How to diagnose quickly - Read the warning line: an Origin message means the scheme or host, not the token. - Compare `request.base_url` with the browser's address bar, for example by logging it in a temporary `before_action`. - Inspect the headers the proxy adds before touching Rails configuration. ## What interviewers check - That you know the origin check exists, is on by default, and must pass **together with** the token. - That you trace `base_url` back to scheme detection and fix the proxy or `assume_ssl` rather than disabling protection.

  • A form POST arrives with no Origin header at all; does Rails' origin check reject it?
    No. `valid_request_origin?` accepts a nil `request.origin`, because some user agents do not send the header. The request still has to carry a valid authenticity token, so a missing Origin weakens nothing the token does not already cover.
  • Why does a controller using protect_from_forgery with: :null_session still raise on an Origin: null POST?
    The `null` case is handled inside `valid_request_origin?`, which raises `ActionController::InvalidAuthenticityToken` directly while Rails is still deciding whether the request is verified. The strategy's `handle_unverified_request` is never reached, so `:null_session` cannot soften it.

  • Adding the proxy's address to trusted_proxies fixes the Origin mismatch.
  • A valid authenticity token is enough even when the origin check fails.
  • Turning on force_ssl alone fixes the scheme behind a plain-HTTP proxy.
  • Rails falls back to the Referer header when Origin is missing.
  • The fix is skip_forgery_protection on the affected controllers.

Assumes Rails 8.1 with load_defaults 8.1; forgery_protection_origin_check has defaulted to true since load_defaults 5.0, and since 8.1.1 Kamal-deployed new apps leave assume_ssl commented out.