# Why You Need Strong Parameters in Rails

*Published on March 19, 2025*

Tags: Controllers

---


_This is the fifth post in my series on_ [_Rails Controllers_](/tag/controllers)_._&nbsp;_Originally published last year, I have revised it and have included the new_ _`expect`_ _method introduced in Rails 8._

As Rails developers, we are used to using&nbsp;`post_params`&nbsp;or&nbsp;`user_params`&nbsp;methods in our controller classes. Until recently, I had no idea why we were doing this, instead of using the&nbsp;`params`&nbsp;hash directly, and, as&nbsp;[a Reddit commenter correctly pointed out](https://www.reddit.com/r/rails/comments/1jevq6x/comment/minho53/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button), they often felt like a magic ritual that purged out the evil spirits from the parameters.

Turns out, strong parameters are there for a good reason, as a solution to a common security vulnerability. This post will cover the why, what, and how of strong parameters in Rails and how they protect against this vulnerability.

## Mass Assignment Vulnerability

**What is it?**&nbsp;Mass Assignment means unintentionally assigning values to multiple variable or object properties at a time.

**Why is it bad?**&nbsp;Consider the following object:

```ruby
user = {
  name: "Jason",
  location: "Chicago",
  admin: false
}
```

Now let’s say we want to update the user's location to "San Diego". The standard way to do this would be to receive some data, preferably from a form submission from the user, and use that data to update our user’s properties.

```ruby
params = {
  # some data
  location: "San Diego",
}

user.update!(params)
```

Boom! You are done. But wait, the next day, you realize that the user now has 'admin' access - which you didn't want.

**How did that happen?**

The answer is the other properties in the&nbsp;`params`&nbsp;object above that I didn’t list. Here’s the complete form data submitted by the user:

```ruby
params = {
  name: "Jason",
  location: "San Diego",
  admin: true
}
```

Because the software updated all the properties of the&nbsp;`user`&nbsp;using the&nbsp;`params`&nbsp;hash in its entirety, the&nbsp;`admin`&nbsp;property was updated to&nbsp;`true`. As a result, Jason is now an admin.

Now, this is obviously a dumbed-down example to show the problem. Of course, the web application is not foolish enough to ask any random user if they are an admin, on a form that anyone can submit.

But I hope you got the idea.&nbsp;**It’s terrible to update all the properties of an object from a source that we don’t trust!**

💡 Mass assignment vulnerability occurs when your application allows user input to be assigned to multiple object attributes or database fields at once without proper restrictions, potentially leading to unauthorized data modification.

The above example showed an update operation, but the same principle applies when creating new records. You should never blindly use user-submitted data to create objects. Instead, ensure that only the permitted attributes are used, preventing mass assignment vulnerabilities and unauthorized changes.

```ruby
# don't do this
user = User.create(params[:user])
```

### Github is Hacked

If you want to see a&nbsp;[real-world example](https://www.zdnet.com/home-and-office/networking/github-suspends-member-over-mass-assignment-hack/), in 2012 GitHub was compromised by this vulnerability. A GitHub user used mass assignment that gave him administrator privileges to none other than the Ruby on Rails project.

As GitHub co-founder, Tom Preston-Werner later said,

> “The root cause of the vulnerability was a failure to properly check incoming form parameters, a problem known as the mass-assignment vulnerability,”

## How to Protect Against Mass Assignment?

The solution is simple. To start with, you should at least define which model attributes you want to make mass assignable. Before you update any object, filter out only the properties you want and nothing else!

Here's a simple and manual way of doing this, before Rails had strong parameters.

```ruby
class UsersController < ActionController::Base
  def create
    User.create(user_params)
  end
  
  def update
    User.find(params[:id]).update(user_params)
  end

  private
    def user_params
      params[:user].slice(:name, :location)
    end
end
```

This pattern was so common that Rails released a plugin as well as a gem for it (see&nbsp;[strong\_parameters](https://github.com/rails/strong_parameters)), and later versions of Rails (v4 onwards) provide it out of the box. Here’s how it works.

## How Strong Parameters Work in Rails

The philosophy behind strong parameters is&nbsp;_“__**assume unsafe until proven otherwise**__“_. In simple terms, Rails marks the parameters forbidden to be used in mass assignment until you explicitly mark them as safe.

**How do you mark parameters as safe?**

Using the&nbsp;`require`&nbsp;and&nbsp;`permit`&nbsp;methods on the&nbsp;`params`&nbsp;hash, which is an instance of&nbsp;[`ActionController::Parameters`](https://api.rubyonrails.org/classes/ActionController/Parameters.html). As the documentation says, this class allows you to choose which attributes should be permitted for mass updating and thus prevent accidentally exposing that which shouldn’t be exposed.

```ruby
params.require(:user).permit(:name, :location)
```

In the above code, we explicitly mark the&nbsp;`user`&nbsp;parameter as required using the&nbsp;`require`&nbsp;method, and only permit the&nbsp;`name`&nbsp;and&nbsp;`location`&nbsp;parameters inside the&nbsp;`user`. If the&nbsp;`user`&nbsp;has an&nbsp;`admin`&nbsp;parameter, Rails won’t allow you to use it in a mass assignment operation.

Let’s open the Rails console by running&nbsp;`bin/rails console`&nbsp;and run some experiments:

```ruby
params = ActionController::Parameters.new({
  user: {
    name: "Jason",
    location: "Chicago",
    admin: false
  },
  client: {
    name: "David"
  }
})

=> #<ActionController::Parameters {"user"=>{"name"=>"Jason", ..}, "client"=>...} permitted: false>
```

_Yes, you can simply create a_&nbsp;_`params`_&nbsp;_object on fly. You don’t need to make a request from the browser to access it. Pretty cool, right?_

Notice that the output of the above code was an object with the&nbsp;`permitted`&nbsp;property set to&nbsp;`false`. Now, let’s mark only the&nbsp;`user`&nbsp;parameter as required, only permitting the&nbsp;`name`&nbsp;and&nbsp;`location`&nbsp;parameters.

```ruby
user_params = params.require(:user).permit(:name, :location)
=> #<ActionController::Parameters {"name"=>"Jason", "location"=>"Chicago"} permitted: true>
```

Note that after validating the parameters and allowing only the attributes we want (**name**&nbsp;and&nbsp;**location**, and not&nbsp;**admin**), the result&nbsp;`client_params`&nbsp;has the&nbsp;`permitted`&nbsp;property set to&nbsp;`true`. The&nbsp;`client_params`&nbsp;parameter is now safe to use in a mass-assignment operation.

To permit an entire hash of parameters, use the&nbsp;`permit!`&nbsp;method.

```ruby
params.require(:user).permit!
```

💡 Remember that the strong parameters are only validated when you try to use them to update the active model’s properties. It won’t prevent you from accessing the data.

```ruby
> params
=> #<ActionController::Parameters {"user"=>#<ActionController::Parameters {"name"=>"Jason", "location"=>"Chicago", "admin"=>false} permitted: false>, "client"=>{"name"=>"David"}} permitted: false>

# This works
params[:client]
=> #<ActionController::Parameters {"name"=>"David"} permitted: false>
params[:client][:name]
=> "David"

# This doesn't
User.update!(params[:client])
```

So far so good. Now let's explore a new method Rails added very recently to simplify the strong parameters API.

## Rails 8 Introduces expect

Until now, the standard approach to using strong parameters in Rails was through the&nbsp;`require`&nbsp;and&nbsp;`permit`&nbsp;methods. However, with the release of Rails 8,&nbsp;[a new&nbsp;`expect`&nbsp;method was added](https://github.com/rails/rails/pull/51674)&nbsp;on the&nbsp;`ActionController::Parameters`&nbsp;object.

The&nbsp;`expect`&nbsp;method lets you safely permit and require parameters in one step, and it reads well, too!

```javascript
user = { name: "Jason", location: "Chicago", admin: false }

params = ActionController::Parameters.new({
  user: {
    name: "Jason",
    location: "San Diego",
    admin: true
  }
})

user_params = params.expect(user: [:name, :location])
user_params # => #<ActionController::Parameters {"name"=>"Jason", "location"=>"San Diego"} permitted: true>

user.update!(user_params)
# => #<Person id: 1, name: "Jason", location: "San Diego", admin: false>
```

Based on the latest&nbsp;[Rails guides](https://guides.rubyonrails.org/action_controller_overview.html#strong-parameters), it seems that&nbsp;`expect`&nbsp;is now the preferred way to handle strong parameters moving forward. I recommend reading the&nbsp;[pull request](https://github.com/rails/rails/pull/51674)&nbsp;that introduced this method to understand the reasoning behind the&nbsp;`expect`&nbsp;method.

## 

Nested Parameters

You can use strong parameters on nested&nbsp;`params`, as shown below:

```ruby
params = ActionController::Parameters.new({
  product: {
    name: "iPhone",
    price: 500,
    accessories: [{
      name: "headphone",
      price: 35
    }, {
      name: "adapter",
      price: 90
    }]
  }
})

permitted = params.permit(product: [ :name, { accessories: :price } ])
permitted.permitted?                            # => true
permitted[:product][:name]                      # => "iPhone"
permitted[:product][:price]                     # => nil
permitted[:product][:accessories][0][:price]    # => 35
permitted[:product][:accessories][0][:category] # => nil
```

💡 Note: If you&nbsp;`permit`&nbsp;a key pointing to a hash, Rails doesn't allow the entire hash. You must specify the permitted hash attributes.

## Permit All Parameters

If you want to permit all parameters by default, Rails provides the&nbsp;`permit_all_parameters`&nbsp;option, which is&nbsp;`false`&nbsp;by default. If set to&nbsp;`true`, it will permit all the parameters.

```ruby
ActionController::Parameters.new
=> #<ActionController::Parameters {} permitted: false>

ActionController::Parameters.permit_all_parameters = true
=> true

ActionController::Parameters.new
=> #<ActionController::Parameters {} permitted: true>
```

This is, of course, not recommended.

## Take Specific Action for Unpermitted Parameters

You can also control the behavior when Rails finds the unpermitted parameters. For this, set the&nbsp;`action_on_unpermitted_parameters`&nbsp;property, which can take one of three values:

- `false`&nbsp;to do nothing and continue as if nothing happened.
- `:log`&nbsp;to log an event at the&nbsp;`DEBUG`&nbsp;level.
- `:raise`&nbsp;to raise an&nbsp;`ActionController::UnpermittedParameters`&nbsp;exception.

```ruby
params = ActionController::Parameters.new(name: "DHH")

### false ###
ActionController::Parameters.action_on_unpermitted_parameters = false
params.permit(:rails)
=> #<ActionController::Parameters {} permitted: true>

### log ###
ActionController::Parameters.action_on_unpermitted_parameters = :log
params.permit(:rails)
2022-05-17 18:39:23.099411 D [56269:4120 subscriber.rb:149] Rails -- Unpermitted parameter: name
=> #<ActionController::Parameters {} permitted: true>

### raise ###
ActionController::Parameters.action_on_unpermitted_parameters = :raise
params.permit(:rails)
=> #../metal/strong_parameters.rb:1002:in `unpermitted_parameters!': found unpermitted parameter: :name (ActionController::UnpermittedParameters)
```

