In Rails Action Cable, what do allowed_request_origins and disable_request_forgery_protection check, and why would production WebSocket upgrades fail with 'Request origin not allowed'?
answer
- checked before the upgrade
- allow_same_origin_as_host defaults to true
- strings exact, regexes via ===
- development: localhost on any port
- failure: 404 plus a log line
basics
~20 sBefore accepting a WebSocket, Action Cable compares the Origin header with the request's own scheme and host, then with config.action_cable.allowed_request_origins (strings or regexes). A mismatch gets a 404 and a 'Request origin not allowed' log line; disable_request_forgery_protection skips the check entirely.
solid answer
~40 sIn `ActionCable::Connection::Base#process`, before the 101 upgrade, Rails calls its origin check. If `config.action_cable.disable_request_forgery_protection` is true, everything passes. Otherwise, with `allow_same_origin_as_host` (default true), an `Origin` equal to the request's own `scheme://Host` passes; failing that, the origin must match an entry in `allowed_request_origins` with `===`, so strings match exactly and regexes match anywhere unless anchored. Anything else is logged as `Request origin not allowed: <origin>` and answered with a 404 'Page not found', never a socket. Development defaults the list to `/https?:\/\/localhost:\d+/`; production has no list. Typical production failures: pages on `https://league.example` connecting to a cable host such as `wss://cable.league.example`, or a proxy that changes `Host` or drops the forwarded scheme. Fix with an explicit, anchored list; `disable_request_forgery_protection` belongs only in development.
code
ruby · 6 lines# config/environments/production.rb
config.action_cable.url = "wss://cable.league.example/cable"
config.action_cable.allowed_request_origins = [
"https://league.example",
%r{\Ahttps://[a-z0-9-]+\.league\.example\z}
]go deeper
Know that Action Cable rejects WebSocket requests from unexpected origins, and that allowed_request_origins lists the accepted ones.
Explain the order: forgery protection switch, same-origin-as-host, then the list matched with ===, and the 404 on failure.
Diagnose production refusals from the log line: separate cable hosts, multiple domains, proxies losing the scheme, and write anchored regexes.
Set the policy for which origins may hold sockets with user cookies, how it is reviewed when domains change, and how non-browser clients are handled.
## Why Action Cable checks the origin A WebSocket upgrade from a browser carries the site's cookies, and Action Cable connections usually authenticate with those cookies. Browsers also send an `Origin` header naming the page that opened the socket. Action Cable's request-forgery protection uses that header to refuse sockets opened by pages from other sites. The attack itself is covered with WebSockets in general; this answer is about the Rails settings. ## The exact check `ActionCable::Connection::Base#process` runs once per upgrade request and accepts it only if the request is a WebSocket upgrade **and** the origin check passes. The check, in order: 1. If `config.action_cable.disable_request_forgery_protection` is true, allow. 2. If `config.action_cable.allow_same_origin_as_host` is true (the default) and `Origin` equals `"#{proto}://#{HTTP_HOST}"`, where `proto` is `https` when Rack considers the request SSL and `http` otherwise, allow. 3. If any entry of `Array(config.action_cable.allowed_request_origins)` matches with `allowed === origin`, allow. A `String` matches only an identical string; a `Regexp` matches if it finds a match anywhere in the header. 4. Otherwise log `Request origin not allowed: <origin>` and respond `404` with the body "Page not found". Because this runs before the handshake, a refused browser never gets a socket, never reaches `connect`, and sees only a failed WebSocket in its console. ## Defaults by environment | Setting | Default | Where it comes from | |---|---|---| | `allow_same_origin_as_host` | `true` | `ActionCable::Server::Configuration` | | `allowed_request_origins` | `/https?:\/\/localhost:\d+/` in development, unset elsewhere | Action Cable's engine initializer | | `disable_request_forgery_protection` | `false` | `ActionCable::Server::Configuration`; a commented-out line in the generated `development.rb` | The Action Cable guide says development allows "localhost:3000"; the source regex allows localhost on any port. ## Why production upgrades fail Same-origin passes most single-host apps without any setting. Failures come from the cases where `Origin` and the request's own scheme and host differ: - **A separate cable host.** The league's pages are served from `https://league.example`, and `config.action_cable.url` points browsers at `wss://cable.league.example/cable`. The Origin is the page's host, the request's host is the cable host, so step 2 fails and step 3 needs an entry. - **Several front-end domains** (`www.` and the bare domain, or a partner subdomain) talking to one app. - **A proxy in front of Rails** that rewrites `Host` or terminates TLS without passing the forwarded scheme, so Rails compares `https://league.example` with `http://league.example`. Fixing the forwarded headers is better than widening the list. The fix is an explicit list: ```ruby config.action_cable.allowed_request_origins = [ "https://league.example", %r{\Ahttps://[a-z0-9-]+\.league\.example\z} ] ``` ## Mistakes with the list - **Unanchored regexes.** `%r{https://league.example}` also matches `https://league.example.attacker.test` and `https://leagueXexample.com`, because `===` searches the string and `.` matches any character. Anchor with `\A` and `\z` and escape the dots. - **Bare hostnames.** `"league.example"` never equals an Origin, which always includes the scheme. - **Glob strings.** `"https://*.league.example"` is compared literally; use a regex. - **Turning protection off.** `disable_request_forgery_protection = true` in production lets any website open a socket that carries the visitor's cookies. It exists for local experiments. - **Confusing it with `config.hosts`.** Host authorization checks the `Host` header for every request; it does not compare `Origin` and does not replace this setting. ## Debugging a refused upgrade 1. Find `Request origin not allowed:` in the production log and copy the exact Origin value it prints. 2. Compare it with the scheme and host the request reached Rails with; if only the scheme differs, the proxy is not forwarding it. 3. If the page and the cable endpoint are on different hosts by design, add the page's origin to `allowed_request_origins`, as a string or an anchored regex. 4. Deploy and confirm the log now shows the upgrade succeeding, before touching any other setting. ## Clients without an Origin The check reads `HTTP_ORIGIN`. A non-browser client that sends none fails both comparisons and gets the 404, unless forgery protection is disabled. The header is not proof of anything, though: a script can send any Origin it likes. The check protects browser users from other sites; authentication still belongs in `connect`.
- What does setting allow_same_origin_as_host to false change?Rails stops accepting an Origin just because it equals the request's own scheme and host, so even the app's own pages must be listed in `allowed_request_origins`. It is a way to make the list the single source of truth, for example when the cable server shares a hostname with origins you do not want to trust.
- A refused page shows a failed WebSocket but the server log shows no rejection from connect. Where should you look?The origin check runs in `process`, before `connect`, so search the log for `Request origin not allowed:` and compare the logged Origin with the request's scheme and host and with `allowed_request_origins`. A scheme mismatch (`http` vs `https`) usually means the proxy is not forwarding the original scheme.
- A rejected origin gets a socket first and a 403 disconnect afterwards.
- allowed_request_origins = [%r{https://league.example}] only matches the league's own site.
- Setting disable_request_forgery_protection in production is harmless because connect checks the cookie.
- In development only http://localhost:3000 is accepted.
- Action Cable compares the Origin header against config.hosts.