skip to content

In a Rails controller, why does Listing.update(params[:listing].permit!) create a mass-assignment hole, and how does Brakeman report it?

level: middleimportance: must knowfreq 55%

answer

  1. ActiveModel::ForbiddenAttributesError by default
  2. permit! marks every key, nested too
  3. to_unsafe_h skips the check entirely
  4. Brakeman: Mass Assignment, permit!
  5. admin and account_id keys flagged High

basics

~20 s

permit! marks every key, nested ones included, as allowed, so a request can set any column such as dealer_id or featured; Brakeman reports it as a Mass Assignment warning, High confidence when the params then reach a mass-assignment call.

solid answer

~50 s

Active Model refuses unpermitted `ActionController::Parameters` in `new`, `update`, `assign_attributes`, `where` and `create_with`, raising `ActiveModel::ForbiddenAttributesError`. `permit!` sets the permitted flag on the object and recursively on nested hashes, so whatever keys the request sends are assigned, including columns the form never shows, like `dealer_id`, `featured` or `price_override`. `to_unsafe_h` is worse in review because it returns a plain hash with no `permitted?` method, so the check never runs. Brakeman's mass-assignment check warns "Specify exact keys allowed for mass assignment instead of using permit!" under the warning type **Mass Assignment**, High confidence when the params reach a mass-assignment call later in the same method, Medium otherwise. A separate check flags explicit `permit` of risky keys: `admin` and `account_id` High, `role` and `banned` Medium. The fix is an explicit key list, which in Rails 8 is usually written with `params.expect`.

code

ruby · 14 lines
ruby
class ListingsController < ApplicationController
  def update
    @listing = current_dealer.listings.find(params[:id])

    # Hole: any key in the request, nested too, is assigned
    # @listing.update(params[:listing].permit!)

    if @listing.update(params.expect(listing: [:title, :price, :description]))
      redirect_to @listing, status: :see_other
    else
      render :edit, status: :unprocessable_content
    end
  end
end

go deeper

for a junior

Know that Rails refuses unpermitted params with ForbiddenAttributesError and that permit! switches that protection off for every key.

for a middle

Explain the permitted flag, why to_unsafe_h bypasses it, nested attributes widening the hole, and how Brakeman's Mass Assignment confidence is chosen.

for a senior

Review admin and ownership fields specifically, scope records to the current user, and treat Brakeman's suspicious-key warnings as authorization questions.

for a principal

Make privileged attributes structurally unreachable from general update actions, so safety does not depend on every permit list being right.

## What mass assignment is in Rails **Mass assignment** means setting many attributes from one hash: `Listing.new(attrs)`, `listing.update(attrs)`, `assign_attributes`. It is convenient and dangerous, because the hash usually comes from the request, and the request can contain any key the attacker likes, not only the fields your form renders. Rails' defence is built into `ActionController::Parameters`. Each instance carries a **permitted** flag, false by default. Active Model's `ForbiddenAttributesProtection` checks it: if the object responds to `permitted?` and returns false, the call raises **`ActiveModel::ForbiddenAttributesError`**. Active Record applies the same check in `where` and `create_with`, so `Listing.where(params[:filters])` also raises. ## What permit! does `permit!` walks the parameters, calls `permit!` on every nested hash and array element, then sets the flag to true. After that the object passes every check. In the dealership app: ```ruby def update @listing.update(params[:listing].permit!) end ``` A salesperson's browser normally sends `title`, `price` and `description`. A crafted request adds `listing[dealer_id]=42` and moves the car to another dealership, or `listing[featured]=1` and buys free front-page placement. Nested attributes make it wider: with `accepts_nested_attributes_for :photos`, `permit!` lets the request create, update or destroy associated records too. ## Other ways to switch the check off | Code | Effect | |---|---| | `params.permit!` | permits every key, recursively | | `params.to_unsafe_h` | returns a plain hash; no `permitted?`, so no check | | `ActionController::Parameters.permit_all_parameters = true` | every params object starts permitted, app-wide | | `params.permit(:title, :admin)` | explicit, but lets a privileged key through | `to_unsafe_h` has legitimate uses, such as logging or building a query string, but passing its result to a model is the same hole as `permit!` with a less obvious name. ## How Brakeman reports it Brakeman's mass-assignment check looks for `permit!` called on params: 1. Warning type **Mass Assignment**, warning code `mass_assign_permit!`, message "Specify exact keys allowed for mass assignment instead of using permit! which allows any keys". 2. Confidence **High** when the same method later passes params to a mass-assignment call, **Medium** otherwise. 3. It skips `permit!` inside a `*_path` helper call and chains that use `slice`, which already restrict the keys. A separate check inspects explicit `permit` lists for suspicious keys: `admin` and `account_id` at High confidence, `role` and `banned` at Medium. A warning there means "confirm only privileged users reach this action", not necessarily a bug. ## When permit! is not a bug `permit!` is legitimate when the parameters never reach a model. Brakeman itself skips `permit!` inside a `*_path` helper, such as `cars_path(params.permit!)` used to rebuild a filter URL, and chains that `slice` known keys first. Even then, prefer `params.permit(:make, :model, :max_price)`: a URL helper fed arbitrary keys can still produce surprising query strings, and an explicit list documents which filters the page supports. The risk also grows with the model. A `Listing` with `belongs_to :dealer`, a `featured` flag and `accepts_nested_attributes_for :photos` turns one `permit!` into control over ownership, placement and child records. Reviewers should read the model's attribute list alongside the controller, because the permit list is only safe relative to what the model will accept. ## The fix - List the keys the form actually edits. Rails 8 code usually writes `params.expect(listing: [:title, :price, :description])`; older code uses `require(...).permit(...)`. - Set ownership from the server, never the request: `@listing = current_dealer.listings.find(params[:id])`. - Give privileged fields their own action and authorization check rather than a conditional permit list. - Unlisted keys are dropped silently in production; Rails logs them in development and test by default, and `action_on_unpermitted_parameters = :raise` there makes a forgotten form field fail loudly.

  • Why is Listing.new(params[:listing].to_unsafe_h) as dangerous as permit!?
    `to_unsafe_h` returns a plain `HashWithIndifferentAccess`. Active Model's protection only raises for objects that respond to `permitted?` and return false; a plain hash does not respond, so every key is assigned. It is the same hole, harder to spot because the name sounds like a conversion.
  • Brakeman flags params.permit(:title, :role) at Medium confidence. What do you check?
    Whether the action can be reached by someone who must not change roles. Brakeman flags `role` as a suspicious key regardless of context. If only administrators reach this action and authorization is enforced, note that and ignore the warning; otherwise move role changes into a separate, authorized action.
  • Does a Rails app raise when a request carries keys that permit did not list?
    Not by default: unlisted keys are dropped. When `config.action_controller.action_on_unpermitted_parameters` is unset, Rails logs them in development and test and ignores them elsewhere; setting it to `:raise` makes `permit` raise `ActionController::UnpermittedParameters`, which catches a form field the list forgot.

  • permit! only affects top-level keys, not nested hashes
  • Passing to_unsafe_h to a model still raises ForbiddenAttributesError
  • Unpermitted params are assigned with a warning by default
  • Brakeman ignores permit! when attr_accessible is absent
  • Hiding a field in the form stops users from setting it