Skip to Content
Course content

236: Strong Parameters and Mass Assignment Protection

Click on the "Edit" button in the top corner of the screen to edit your slide content.

You might be thinking that as long as you don't put a specific input field in your HTML form, there's no way for a user to update that attribute in your database. It feels intuitive: if the user can't see the "Admin" checkbox on the registration page, they can't check it, right? This is a dangerous assumption that leads to one of the most classic security vulnerabilities in web development: mass assignment.

The "Hidden Field" Fallacy

Here is why that logic fails. Your Rails controller doesn't just receive data from your HTML forms; it receives data from HTTP requests. A malicious user doesn't have to use your browser-rendered form. They can use a tool like curl, Postman, or even just the browser's developer console to send whatever parameters they want.

Imagine you have a User model with name, email, and is_admin. In your controller, you're doing something like this:

def create
  @user = User.new(params[:user]) # This is mass assignment
  if @user.save
    redirect_to @user
  end
end

Even if your new.html.erb only contains fields for name and email, I can open my terminal and run this:

curl -X POST http://localhost:3000/users \
     -d "user[name]=Hacker" \
     -d "user[email]=hacker@example.com" \
     -d "user[is_admin]=true"

Because you passed the entire params[:user] hash directly into User.new, Rails will happily set is_admin to true. I've just granted myself administrative access to your application without ever touching your UI. I've seen this happen in production environments more times than I'd like to admit; it's a silent killer because the app "works" perfectly during your manual testing.

Whitelisting with Strong Parameters

To stop this, Rails introduced Strong Parameters. The goal is to move the responsibility of "which fields are allowed" out of the view and into the controller. Instead of trusting the params hash, we create a private method that explicitly whitelists the attributes we want to permit.

Here is how we fix the example above:

def create
  @user = User.new(user_params)
  if @user.save
    redirect_to @user
  end
end

private

def user_params
  params.require(:user).permit(:name, :email)
end

Let's break down what's happening in user_params. First, require(:user) ensures that the user key is present in the params hash; if it's missing, Rails will throw a ActionController::ParameterMissing error (which results in a 400 Bad Request). Then, permit(:name, :email) tells Rails: "I only care about these two keys. If anything else is in there—like is_admin—just ignore it."

Now, if I try that same curl attack, Rails will simply filter out the is_admin parameter before it ever reaches the User.new method. The user will be created, but they will not be an admin. It's a simple pattern, but it's the primary line of defense for your data integrity.

One quick tip: you'll often find yourself wanting different permitted parameters for different roles. For instance, a regular user can update their email, but only an admin should be able to update a user's account_status. In those cases, don't be afraid to write two different private methods—like user_params and admin_user_params—and call the one that fits the current user's authorization level.




📋 Practical Task

Securing the Profile Update Action

You are working on a Project Management app. The Project model has the following attributes: title, description, deadline, and internal_budget_code.

Currently, the ProjectsController is vulnerable to mass assignment. Your task is to implement Strong Parameters to ensure that users can update the project's title, description, and deadline, but cannot modify the internal_budget_code via the web request.

Starter Code:

class ProjectsController < ApplicationController
  def update
    @project = Project.find(params[:id])
    if @project.update(params[:project]) # VULNERABLE LINE
      redirect_to @project, notice: 'Project updated!'
    else
      render :edit
    end
  end
end

Requirements:

  • Create a private method called project_params.
  • Use require to ensure the project key is present.
  • Use permit to allow only the three user-facing fields.
  • Update the update action to use your new method instead of the raw params hash.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.