What does allow_browser versions: :modern in a generated Rails 8.1 ApplicationController do, and which requests does it still let through?
answer
- a before_action under the hood
- 406 Not Acceptable, static page
- Safari 17.2, Chrome 120, Firefox 121
- no user agent or bots: allowed
- block: option overrides the response
basics
~20 sallow_browser adds a before_action that renders public/406-unsupported-browser.html with 406 to browsers older than Safari 17.2, Chrome 120, Firefox 121 or Opera 106, and to Internet Explorer; unknown agents, bots and requests without a version pass.
solid answer
~40 sSince Rails 7.2, `rails new` puts `allow_browser versions: :modern` in a non-API `ApplicationController`. The class method registers a `before_action` that parses the User-Agent with the `useragent` gem. A request is blocked only when the agent reports a version, the browser is named in the version set, the version is below the minimum, and it is not a bot. `:modern` means Safari 17.2, Chrome 120, Firefox 121, Opera 106, and no Internet Explorer at all. A blocked request gets `public/406-unsupported-browser.html`, without a layout, with **406 Not Acceptable**, which halts the chain. Pass a hash for your own minimums, `only:`/`except:` to scope it, and `block:` with a method name or lambda to respond differently. Each block is instrumented as `browser_block.action_controller`.
code
ruby · 10 linesclass ApplicationController < ActionController::Base
allow_browser versions: :modern, block: :handle_outdated_browser
private
def handle_outdated_browser
Rails.logger.info("Blocked #{request.user_agent}")
render file: Rails.root.join("public/406-unsupported-browser.html"),
layout: false, status: :not_acceptable
end
endgo deeper
Recognise the generated allow_browser line, know that it shows old browsers a 406 page, and know it only affects controllers that inherit it.
Explain that the macro installs a before_action, list the :modern minimums, and describe which agents pass: no version, unlisted browsers and bots.
Instrument browser_block.action_controller before changing the set, handle web views and kiosks deliberately, and know that subclasses can only add checks, not remove the parent's lambda.
Set the browser support policy as a product decision, trading front-end simplicity on modern features against the share of real users turned away.
## What the generated line does A new full-stack Rails 8.1 app ships an `ApplicationController` whose first declaration is `allow_browser versions: :modern`, with a comment explaining that it admits only browsers supporting webp images, web push, badges, import maps, CSS nesting and CSS `:has`. It exists because the default front end — import maps, modern CSS, Turbo — assumes those features, and a clear "please upgrade" page beats a silently broken one. Both the macro and the generated line arrived in Rails 7.2. ## How the check works `allow_browser` is a class method in `ActionController::AllowBrowser`. It does not run anything itself; it registers a **`before_action`** (a lambda) that calls a private `allow_browser` on each request. That private method builds a `BrowserBlocker` and asks whether the request is blocked: 1. **Does the agent report a version?** If the User-Agent is empty, or the `useragent` gem cannot extract a version, the request is allowed. 2. **Is the browser named in the set?** Only browsers listed in the version hash are guarded. A browser the hash does not mention is allowed. 3. **Is it below the minimum?** The parsed version is compared with the minimum; `ie: false` means every version is blocked. 4. **Is it a bot?** Crawlers recognised by the gem are allowed even with an old engine. Only when every condition says "block" does the callback run the block action, inside an `ActiveSupport::Notifications` event named `browser_block.action_controller`. ## The `:modern` set and the default response | Key | Minimum version | |---|---| | `safari` | 17.2 | | `chrome` | 120 | | `firefox` | 121 | | `opera` | 106 | | `ie` | `false` (always blocked) | The default block renders `public/406-unsupported-browser.html` with `layout: false` and `status: :not_acceptable`. Because that is a render inside a `before_action`, the filter chain halts and the action never runs. The file is generated into `public/` alongside 400, 404, 422 and 500 pages, so the message can be edited like any static page. ## Options - `versions:` — `:modern` or your own hash, for example `{ safari: 16.4, firefox: 121, ie: false }`. - `only:` and `except:` — passed straight to the `before_action`, to scope the check to some actions. - `block:` — a method name as a symbol, or a lambda run with `instance_exec`, to log, redirect or render something else instead of the static page. ## What still gets through - Requests with **no User-Agent**, or one without a parsable version, such as many server-to-server webhooks. - Agents **not named** in the set, which covers command-line HTTP clients that identify themselves by their own name. - **Bots**, so search crawlers are not shown the upgrade page. - Controllers that do **not inherit** from the controller declaring it, for example the built-in `/up` health check controller. ## Testing with allow_browser in place System tests drive a current headless browser, so they pass the `:modern` check without any changes. Integration tests that send no User-Agent are allowed through as well, since an empty agent never counts as blocked. That also means a test suite will not warn you when the version set changes, so the policy needs its own explicit test. To test the block itself, send an old browser's User-Agent string in a request test and assert a 406 status and the unsupported-browser page body; that test documents the support policy as well as checking it. ## Production judgement - **Scoping is additive.** A subclass calling `allow_browser` again adds a second check alongside the parent's, so it can tighten the rules but not loosen them. The callback is an anonymous lambda, so there is no method name to hand to `skip_before_action`; loosening means changing the parent or moving the declaration to an intermediate base controller. - **Watch who you turn away.** Embedded web views, kiosk devices and managed corporate machines often lag behind the `:modern` minimums. Subscribe to `browser_block.action_controller` and count blocks before tightening further. - **APIs and webhooks** in the same controller tree are usually unaffected because their clients send no browser user agent, but a JSON request from an old browser still gets the HTML 406 page unless a custom `block:` responds differently.
- Why can't a Rails subclass controller remove the allow_browser check with skip_before_action?`allow_browser` registers an anonymous lambda as the `before_action`, so there is no callback name to pass to `skip_before_action`. A second `allow_browser` call in the subclass only adds another check. To relax the rule you change the parent's declaration, scope it with `only:`/`except:`, or move it to an intermediate base controller.
- How would you measure how many visitors Rails' allow_browser blocks before tightening the version set?Subscribe to the `browser_block.action_controller` notification, which fires for every blocked request with the request and the versions in its payload, and count or log those events. A custom `block:` method can also log before rendering the 406 page.
- allow_browser is Rack middleware, so it runs before routing for every request.
- A blocked browser is redirected with a 302 to an upgrade page.
- Requests without a User-Agent header are blocked by allow_browser versions: :modern.
- Any browser missing from the version set is blocked, because :modern is an allow-list.
- skip_before_action :allow_browser in a subclass turns the check off.
Assumes Rails 8.1.4; allow_browser and the generated allow_browser versions: :modern line arrived in Rails 7.2, so older apps have neither.