In Rails 8.1, what happens when a controller calls redirect_to params[:return_to], and how do allow_other_host: and action_on_open_redirect change it?
answer
- same host or leading slash passes
- OpenRedirectError under load_defaults 7.0+
- :log, :notify or :raise
- allow_other_host: true opts out
- url_from returns nil off-host
basics
~10 sredirect_to allows a path or a URL on request.host; another host raises ActionController::Redirecting::OpenRedirectError when action_on_open_redirect is :raise, the load_defaults 7.0+ value, while allow_other_host: true allows it and false always refuses it.
solid answer
~40 s`redirect_to` checks the computed location. A path starting with `/` (but not `//`), a URL whose host equals `request.host`, or a host in `config.action_controller.allowed_redirect_hosts` (added in 8.1) passes. Anything else depends on `allow_other_host:` and `config.action_controller.action_on_open_redirect`. `allow_other_host: true` redirects anywhere; `false` always raises. Otherwise `:raise`, which `load_defaults` 7.0 and later set, raises `ActionController::Redirecting::OpenRedirectError`; the framework default `:log` only logs "Open redirect to ... detected" and still redirects, and `:notify` publishes `open_redirect.action_controller` and redirects. That setting replaces the deprecated `raise_on_open_redirects`. For a return-to link, `redirect_to url_from(params[:return_to]) || root_path` falls back instead of raising, and the generated authentication concern avoids the param entirely by storing `request.url` in the session.
code
ruby · 11 linesclass SessionsController < ApplicationController
def create
if user = User.authenticate_by(params.permit(:email_address, :password))
start_new_session_for user
# Internal URL or nil: never an off-site host from the query string
redirect_to url_from(params[:return_to]) || root_path
else
redirect_to new_session_path, alert: "Try another email address or password."
end
end
endgo deeper
Know that redirecting to a URL taken from params can send users to another site, and that Rails raises for other hosts in a new app.
Explain how redirect_to decides internal versus external, the three action_on_open_redirect values, allow_other_host: in both directions, and url_from as a fallback.
Audit upgraded apps whose load_defaults leave redirects at :log, keep return-to targets server-side, and know where Brakeman's redirect check can be blind.
Decide which external hosts the product may hand users to, and keep that list in one reviewed setting rather than scattered allow_other_host: flags.
## The attack in one line An **open redirect** is an endpoint that sends the browser wherever a parameter says. In the dealership app, the sign-in page accepts `?return_to=` so users land back on the car they were viewing. If `return_to` can be `https://dealer-login.example.evil`, a phishing email can use the dealership's real domain as the first hop, and the victim ends up on a convincing copy of the sign-in form. ## What redirect_to checks in Rails 8.1 `redirect_to params[:return_to]` turns its argument into a location and then decides whether it is internal: 1. A string starting with `/`, but not `//`, is internal. 2. A URL whose host equals `request.host` is internal; subdomains must match exactly. 3. A host matching `config.action_controller.allowed_redirect_hosts` is treated as internal. The list is new in 8.1 and uses the same matcher as `config.hosts`. 4. Everything else, including `//evil.example` and `javascript:` URLs, is external. For an external location the outcome depends on the call and the configuration: | Situation | Result | |---|---| | `allow_other_host: true` | redirects | | `allow_other_host: false` | raises `OpenRedirectError` | | `action_on_open_redirect = :raise` | raises `OpenRedirectError` | | `action_on_open_redirect = :log` | logs a warning and **redirects** | | `action_on_open_redirect = :notify` | instruments `open_redirect.action_controller` and **redirects** | `OpenRedirectError` is a subclass of `ActionController::Redirecting::UnsafeRedirectError`; its message suggests passing `allow_other_host: true`. Treat that hint as a last resort. ## Which value an app actually has The framework default is `:log`, but `load_defaults "7.0"` and every later target set `:raise`, so a new 8.1 app raises. An app upgraded from 6.x that never advanced `load_defaults` past 6.1 only logs, so its open redirects work. `config.action_controller.raise_on_open_redirects` is the older boolean; Rails 8.1 deprecates it in favour of `action_on_open_redirect`, and a `true` value still forces raising. Rails 8.1's own defaults add a neighbouring guard: `action_on_path_relative_redirect = :raise`, so `redirect_to "evil.example"`, a string with no leading slash that would be glued onto the current host, raises `PathRelativeRedirectError`. ## Fixing the return-to flow - **Fall back instead of raising**: `redirect_to url_from(params[:return_to]) || root_path`. `url_from` returns the location only when it is internal by the same rules, otherwise `nil`. - **Keep the target out of the request**: the Rails 8 authentication generator's concern stores `request.url` in `session[:return_to_after_authenticating]` before sending the user to sign in, then reads it back with `after_authentication_url`. A value the server wrote cannot be forged in a link. - **Name partner hosts explicitly**: if financing really lives on `credit.partner.example`, add it to `allowed_redirect_hosts` rather than passing `allow_other_host: true` on a user-controlled value. - **Never combine `allow_other_host: true` with params.** With that pair Rails' open-redirect protection is switched off for the call by design. ## Testing the behaviour A request test documents the contract for the sign-in page: 1. Sign in with `return_to=/cars/42` and expect a redirect to `/cars/42`. 2. Sign in with `return_to=https://evil.example/login` and expect the fallback, the root path, when the code uses `url_from`. 3. Sign in with `return_to=//evil.example` and expect the same fallback, since a protocol-relative URL names another host. Without `url_from`, the second case raises `OpenRedirectError` under `:raise`, which surfaces as a 500 for the user; a fallback is the friendlier design for a value that arrives in a link. ## What Brakeman reports Brakeman's redirect check warns "Possible unprotected redirect" under the warning type **Redirect** when request input reaches `redirect_to`, `redirect_back` or `redirect_back_or_to`. It treats an app with `load_defaults` 7.0 or later, or `raise_on_open_redirects = true`, as protected and then warns only when `allow_other_host: true` is passed. Brakeman 8.1 reads `raise_on_open_redirects` but not `action_on_open_redirect`, so an app that sets the newer key to `:log` loses its redirect warnings while being exposed. Check the configuration as well as the report.
- An upgraded app's logs show "Open redirect to ... detected" but users are still redirected. Why?Its `action_on_open_redirect` is `:log`, the framework default, because `load_defaults` was never raised to 7.0 or later. `:log` records the event and allows the redirect. Set it to `:raise`, by advancing `load_defaults` or explicitly, after fixing the call sites that legitimately leave the host.
- Why might Brakeman show no Redirect warnings for an app that is exposed?Brakeman treats `load_defaults` 7.0+ or `raise_on_open_redirects = true` as protection and then warns only on `allow_other_host: true`. Brakeman 8.1 does not read `action_on_open_redirect`, so an app that sets it to `:log` looks protected to the scanner while redirecting anywhere.
- redirect_to only accepts paths, so params cannot leave the site
- Under :log, Rails blocks the redirect and logs it
- Pass allow_other_host: true to fix OpenRedirectError on return_to
- A //evil.example location counts as an internal path
- url_from raises when the URL is on another host
Assumes Rails 8.1: action_on_open_redirect replaces the deprecated raise_on_open_redirects, allowed_redirect_hosts is new in 8.1.0, and load_defaults 8.1 raises on path-relative redirects.