Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
236: Strong Parameters and Mass Assignment Protection
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
requireto ensure theprojectkey is present. - Use
permitto allow only the three user-facing fields. - Update the
updateaction to use your new method instead of the raw params hash.
There are no comments for now.