skip to content

In Rails 8.1, which header does config.permissions_policy actually emit, and how does a controller override it for one action?

level: middleimportance: nice to knowfreq 15%

answer

  1. the name and the header differ
  2. Feature-Policy, legacy syntax
  3. no initializer in a new app
  4. middleware only when a policy exists
  5. controller override needs a global policy

basics

~10 s

Despite its name, Rails 8.1's permissions_policy DSL writes the legacy Feature-Policy header; a controller's permissions_policy only: block edits a clone of the global policy, which must exist because the override cannot start from nothing.

solid answer

~50 s

`Rails.application.config.permissions_policy do |policy| ... end` declares browser-feature rules with methods such as `camera`, `microphone`, `geolocation`, `payment` and `fullscreen`, taking `:self`, `:none`, origins or lambdas. Rails 8.1 still sends them as **`Feature-Policy`** with the old `camera 'none'; fullscreen 'self'` syntax; the source comment says the middleware got the new name while the header kept the old name and implementation. The middleware is installed only when a global policy is configured, and a new 8.1 app generates no `permissions_policy.rb` initializer. In a controller, `permissions_policy only: :new do |policy| ... end` runs as a `before_action`, clones the global policy and lets you change directives for those actions; unlike the CSP macro there is no `false` form, and without a global policy the override fails. If you need the standard `Permissions-Policy` header, set it yourself, for example in `config.action_dispatch.default_headers`.

go deeper

for a junior

Know that Rails has a DSL for browser-feature restrictions and that it lives in an initializer you write yourself.

for a middle

Explain that the DSL emits Feature-Policy despite its name, which sources it accepts, and how the controller macro clones the global policy for chosen actions.

for a senior

Check what header actually reaches browsers, add Permissions-Policy yourself where it matters, and avoid assuming feature restrictions are enforced because the DSL is configured.

for a principal

Decide whether framework-generated security headers or headers set at the edge are the source of truth, so one layer does not silently contradict the other.

## Two names for one idea Browsers let a page restrict powerful features (camera, microphone, geolocation, payment, fullscreen) for itself and its iframes. The first header for that was **`Feature-Policy`**; its successor is **`Permissions-Policy`**, with a different syntax. Rails' DSL is named `permissions_policy`, but in 8.1 it still writes **`Feature-Policy`**. The comment on `ActionDispatch::PermissionsPolicy` explains that Rails took the new name for the middleware to avoid renaming it later and kept the old header name and implementation for now. Knowing that mismatch is most of what this question tests. ## Declaring the global policy A new 8.1 app has no `config/initializers/permissions_policy.rb`; you create one: ```ruby Rails.application.config.permissions_policy do |policy| policy.camera :none policy.microphone :none policy.geolocation :none policy.payment :self, "https://pay.budget.example" policy.fullscreen :self end ``` Rules of the DSL: - Each feature is a method: `accelerometer`, `autoplay`, `camera`, `display_capture`, `fullscreen`, `geolocation`, `gyroscope`, `microphone`, `payment`, `usb`, `web_share` and others. - `:self` and `:none` map to `'self'` and `'none'`; strings pass through; lambdas are evaluated in the controller. - Calling a method with no arguments deletes that feature from the policy. - `speaker`, `vibrate` and `vr` were deprecated in 7.1 and removed in 7.2. The example builds `Feature-Policy: camera 'none'; microphone 'none'; geolocation 'none'; payment 'self' https://pay.budget.example; fullscreen 'self'`. ## When the middleware runs `ActionDispatch::PermissionsPolicy::Middleware` is added to the stack only when `config.permissions_policy` has been set, and never in API-only apps. On each response it: 1. leaves the response alone if a `Feature-Policy` header is already present; 2. builds the header from the request's policy; 3. deletes the header again if the policy has no directives. ## Overriding in a controller `ActionController::Base` includes a `permissions_policy` class macro: ```ruby class ReceiptsController < ApplicationController permissions_policy only: :scan do |policy| policy.camera :self end end ``` It registers a `before_action` (so `only:` and `except:` work), clones the request's policy, runs the block on the clone and stores it back. Two differences from `content_security_policy` matter: | | `content_security_policy` | `permissions_policy` | |---|---|---| | No global policy | starts from an empty policy | fails: there is nothing to clone and no middleware | | Remove the header for an action | `content_security_policy false` | no `false` form | | Report-only variant | yes | no | ## Sending the standard header Browsers that dropped `Feature-Policy` read `Permissions-Policy`, whose syntax is `camera=(), geolocation=(self)`. Rails' DSL cannot produce it. If you need it, add it yourself, for instance through `config.action_dispatch.default_headers`, which Rails merges into every response a controller builds, or with `response.headers` in a controller. Which header a given browser honours is browser territory; the Rails fact is which header the DSL writes. ## Checking what is sent Because the name and the header disagree, verify rather than assume. An integration test that reads `response.headers["Feature-Policy"]` documents the actual behaviour, and a second assertion on `Permissions-Policy` documents whether you add the standard header yourself. In a budgeting app where receipt scanning needs the camera on one page only, the global policy says `camera :none` and the receipts controller allows `:self` for the scan action; a test on both pages keeps that exception from spreading. ## In an interview - Say the DSL exists and what it does. - Point out the header name it really sends. - Mention that a new app has no initializer for it. - Note the controller override's dependency on a global policy.

  • Why does a permissions_policy block in a controller fail in an app without a global policy?
    The macro clones `request.permissions_policy` and edits the clone. Without `config.permissions_policy`, that value is `nil`, so the block has no policy to call directives on and raises. The middleware that writes the header is also missing from the stack in that case. Define a global policy, even an empty block, first.
  • How would you send a standard Permissions-Policy header from a Rails 8.1 app?
    Set it yourself, since the DSL only writes `Feature-Policy`. `config.action_dispatch.default_headers` merges fixed headers into every response, so adding a `"Permissions-Policy" => "camera=(), microphone=()"` entry works app-wide; a controller can also set `response.headers` for one action.

  • Rails' permissions_policy DSL sends the Permissions-Policy header
  • A new 8.1 app generates config/initializers/permissions_policy.rb
  • permissions_policy false, only: :show removes the header like CSP
  • The permissions policy is a directive inside the CSP header
  • A controller override works without any global policy

Assumes Rails 8.1.4: the DSL still writes the legacy Feature-Policy header, and a new 8.1 app generates no permissions_policy initializer.