Behind a TLS-terminating proxy, a Rails 8.1 budgeting app with force_ssl enabled loops on redirects; why, and what does config.assume_ssl change?
answer
- proxy speaks plain HTTP to Puma
- request.ssl? reads the forwarded scheme
- AssumeSSL rewrites the Rack env
- redirect never fires afterwards
- only safe if the proxy is the sole path
basics
~10 sThe proxy forwards decrypted traffic over HTTP without a forwarded scheme, so request.ssl? is false and ActionDispatch::SSL redirects back to HTTPS forever; config.assume_ssl makes every request look like HTTPS, so the loop stops.
solid answer
~40 sWith TLS ending at the proxy, Puma receives plain HTTP. Rack decides the scheme from `HTTPS=on`, `X-Forwarded-Ssl` or a forwarded proto; if the proxy sends none of them, `request.ssl?` is false, so `ActionDispatch::SSL` redirects to `https://`, the browser comes back through the proxy, and the cycle repeats. One fix is a proxy that sends `X-Forwarded-Proto: https`, which Rack honours. The other is `config.assume_ssl = true`, which inserts `ActionDispatch::AssumeSSL` ahead of the SSL middleware; it sets `HTTPS=on`, `rack.url_scheme=https` and the forwarded proto and port on **every** request. Then `request.ssl?` is true, `_url` helpers and `request.base_url` use `https`, cookies are marked secure and HSTS is sent, but the HTTP-to-HTTPS redirect never fires, so the proxy must do it. That is only safe when nothing reaches Puma except through the proxy.
code
ruby · 6 lines# config/environments/production.rb
# Puma is reachable only from the TLS-terminating proxy, which redirects http:// itself.
Rails.application.configure do
config.assume_ssl = true # every request is treated as HTTPS
config.force_ssl = true # secure cookies + HSTS; its redirect no longer fires
endgo deeper
Know that behind a proxy that terminates TLS, Rails sees plain HTTP unless the proxy forwards the scheme or config.assume_ssl is set.
Explain how Rack derives request.ssl? from HTTPS, X-Forwarded-Ssl and X-Forwarded-Proto, and what AssumeSSL writes into the env.
Diagnose the loop and the origin-check failures from logs, choose between a forwarded header and assume_ssl, and make sure the proxy owns the HTTP redirect when assume_ssl silences Rails' own.
Decide which layer owns transport security across environments, and make the network guarantee assume_ssl relies on an explicit, tested part of the deployment.
## The setup that loops A common production shape: browsers speak HTTPS to a reverse proxy or load balancer, which **terminates TLS** and forwards the decrypted request to Puma over plain HTTP on a private network. Rails never sees the TLS connection, so it has to be told the original scheme. Rack's `Request#scheme` answers `https` when any of these is present, in this order: 1. `HTTPS=on` in the env; 2. an `X-Forwarded-Ssl: on` header; 3. a forwarded scheme, from `Forwarded: proto=https` or `X-Forwarded-Proto` / `X-Forwarded-Scheme`; 4. otherwise `rack.url_scheme`, which for a plain connection is `http`. `request.ssl?` is just `scheme == "https"`. If the proxy sends none of those headers, every request looks like HTTP. With `config.force_ssl = true`, `ActionDispatch::SSL` then answers each one with a redirect to `https://`, the browser follows it to the proxy, the proxy forwards it as HTTP again, and the browser eventually reports too many redirects. ## Symptoms without force_ssl With `force_ssl` off the app works but is subtly wrong: - `_url` helpers in controllers copy `request.protocol`, so links and redirects come out as `http://`. - `request.base_url` is `http://…`, so a form post whose `Origin` is `https://…` fails Rails' forgery-protection origin check with `InvalidAuthenticityToken`. - Cookies are not marked secure and no HSTS header is sent. ## Two ways to tell Rails the truth | Approach | How | Trade-off | |---|---|---| | Forwarded scheme | proxy adds `X-Forwarded-Proto: https` | per-request truth; force_ssl still redirects real HTTP | | `config.assume_ssl = true` | `ActionDispatch::AssumeSSL` rewrites the env | unconditional; the app can no longer tell HTTP from HTTPS | `ActionDispatch::AssumeSSL` is tiny. On every request it sets `HTTPS=on`, `HTTP_X_FORWARDED_PORT=443`, `HTTP_X_FORWARDED_PROTO=https` and `rack.url_scheme=https`. Rails places it after host authorization and before `ActionDispatch::SSL`, so the SSL middleware already sees a secure request. Note that Rails does not consult `config.action_dispatch.trusted_proxies` when reading the forwarded scheme; that setting serves `ActionDispatch::RemoteIp`, the client-IP calculation. ## What assume_ssl changes about force_ssl With both flags on, `ActionDispatch::SSL` still marks cookies secure and still sends `Strict-Transport-Security`, because every request now passes `request.ssl?`. Its **redirect never fires**, for the same reason. A browser that types `http://` must therefore be redirected by the proxy itself. In practice the pair means: the proxy owns the HTTP-to-HTTPS hop, Rails owns cookies, HSTS and correct URLs. ## When assume_ssl is unsafe `assume_ssl` lies on behalf of every request. That is fine when Puma is reachable **only** through the TLS proxy. It is wrong when: - the app port is exposed directly, so a plain-HTTP visitor would get secure-flagged cookies on an insecure connection and no redirect; - some traffic legitimately arrives over HTTP, such as an internal service calling the app without TLS; - the same build runs locally without a proxy and you rely on the redirect there. ## Diagnosing it When the browser reports too many redirects after `force_ssl` goes on: 1. Read the Rails log: if the redirected requests never appear as `Started GET` lines, they are being answered by `ActionDispatch::SSL`, which sits ahead of Rails' request logger, and the scheme is the suspect. 2. Check what the proxy sends upstream. If neither `X-Forwarded-Proto` nor `X-Forwarded-Ssl` reaches Puma, Rack can only see `http`. 3. Decide between a forwarded header and `assume_ssl`, then confirm with a HEAD request through the proxy that the response carries HSTS and no `Location`. 4. Re-check form posts and `_url` output, since both depended on the same wrong scheme. ## What a new 8.1 app ships The generated `production.rb` comment says "Assume all access to the app is happening through a SSL-terminating reverse proxy." With `--skip-kamal` both `assume_ssl` and `force_ssl` are on; since 8.1.1, a Kamal-generated app leaves both commented so the first deploy works before a certificate is issued. Turning them on is a deliberate step once TLS is in place. For the budgeting app, this is why the scheme is settled before the strict CSP is tightened: an origin-check failure or an `http://` redirect can look like a policy problem when it is a scheme problem.
- The proxy already sends X-Forwarded-Proto: https. Do you still need assume_ssl?No. Rack's `scheme` honours `X-Forwarded-Proto`, so `request.ssl?` is already true for proxied HTTPS traffic and `force_ssl` behaves normally, including redirecting genuine plain-HTTP requests. `assume_ssl` is for proxies that send no forwarded scheme, or when you prefer one setting over trusting headers.
- Why does a misread scheme break form submissions even with force_ssl off?Rails' forgery protection compares the browser's `Origin` header with `request.base_url`. The browser sends `https://…`, while Rails, seeing plain HTTP, computes `http://…`, so the check fails and the post raises `InvalidAuthenticityToken`. Fixing the scheme, by forwarded header or `assume_ssl`, fixes the mismatch.
- Does config.action_dispatch.trusted_proxies control whether Rails believes X-Forwarded-Proto?No. `trusted_proxies` feeds `ActionDispatch::RemoteIp`, which decides `request.remote_ip` from `X-Forwarded-For`. Rack reads the forwarded scheme without a trusted-proxy check, which is one more reason the app port must not be reachable except through the proxy.
- assume_ssl redirects plain-HTTP requests to HTTPS
- Rails trusts X-Forwarded-Proto only from config.action_dispatch.trusted_proxies
- With assume_ssl, force_ssl still redirects users who type http://
- assume_ssl is harmless even when the app port is publicly reachable
- The redirect loop means the TLS certificate is invalid
Assumes Rails 8.1: since 8.1.1 a Kamal-generated production.rb leaves assume_ssl and force_ssl commented out, so they must be turned on deliberately.