📖 Scenario: You are building a simple Rails web application that handles user sign-up and login. To keep users safe, you need to apply security best practices in your Rails code.
🎯 Goal: Learn how to implement basic security best practices in a Rails app, including strong parameters, password hashing, and protection against common attacks.
📋 What You'll Learn
Use strong parameters to whitelist user input
Use has_secure_password for password hashing
Add CSRF protection in the controller
Sanitize user input before displaying
💡 Why This Matters
🌍 Real World
Web applications must protect user data and prevent attacks like CSRF, injection, and password theft. These practices keep users safe and build trust.
💼 Career
Rails developers are expected to know how to secure apps by using built-in features like strong parameters, password hashing, and CSRF protection.
Progress0 / 4 steps
1
Set up User model with secure password
Create a Rails model called User with attributes email:string and password_digest:string. Add has_secure_password to enable password hashing.
Ruby on Rails
Hint
The has_secure_password method adds password hashing and authentication features.
2
Add strong parameters in UsersController
In UsersController, create a private method called user_params that uses params.require(:user).permit(:email, :password, :password_confirmation) to whitelist user input.
Ruby on Rails
Hint
Strong parameters help prevent unwanted data from being saved.
3
Enable CSRF protection in ApplicationController
In ApplicationController, add protect_from_forgery with: :exception to enable CSRF protection.
Ruby on Rails
Hint
This line helps protect your app from cross-site request forgery attacks.
4
Sanitize user input before displaying
In a view file, use the Rails helper sanitize() to safely display user input stored in @user.email.
Ruby on Rails
Hint
Sanitize removes harmful HTML or scripts from user input before showing it.
Practice
(1/5)
1. Which Rails feature helps prevent unwanted or dangerous user input from being saved to the database?
easy
A. Strong Parameters
B. Active Record Callbacks
C. Action Mailer
D. Asset Pipeline
Solution
Step 1: Understand user input control
Strong Parameters allow you to specify which attributes are allowed from user input, preventing mass assignment vulnerabilities.
Step 2: Identify the correct Rails feature
Active Record Callbacks and others do not control input filtering; only Strong Parameters do.
Final Answer:
Strong Parameters -> Option A
Quick Check:
User input filtering = Strong Parameters [OK]
Hint: Strong Parameters filter user input safely [OK]
Common Mistakes:
Confusing callbacks with input filtering
Thinking Asset Pipeline handles security
Assuming Action Mailer controls input
2. Which line correctly enables CSRF protection in a Rails controller?
easy
A. enable_csrf_protection!
B. protect_from_forgery with: :exception
C. csrf_protect true
D. use_csrf_protection = true
Solution
Step 1: Recall Rails CSRF protection syntax
The correct method to enable CSRF protection is protect_from_forgery with: :exception in controllers.
Step 2: Check other options for validity
Other options are not valid Rails syntax and will cause errors or have no effect.
Hint: Use protect_from_forgery with: :exception [OK]
Common Mistakes:
Using non-existent methods for CSRF
Forgetting to add CSRF protection
Misnaming the method or options
3. Given this Rails view code:
<%= raw @user.name %>
What security risk exists if @user.name contains HTML tags?
medium
A. Cross-Site Scripting (XSS) attack
B. SQL Injection
C. Mass Assignment
D. Denial of Service
Solution
Step 1: Understand output escaping in Rails views
Using <%= %> escapes HTML by default, but if raw HTML is inserted, it can cause XSS.
Step 2: Identify the risk of unescaped HTML in user input
If @user.name contains HTML tags and is not escaped, it can run malicious scripts in the browser, causing XSS.
Final Answer:
Cross-Site Scripting (XSS) attack -> Option A
Quick Check:
Unescaped HTML in views = XSS risk [OK]
Hint: Unescaped user input risks XSS attacks [OK]
Common Mistakes:
Confusing XSS with SQL Injection
Assuming Rails never escapes output
Ignoring HTML tags in user input
4. You see this code in a Rails controller:
params.require(:user).permit(:name, :email)
But users report they can update admin-only fields. What is the likely problem?
medium
A. CSRF protection is disabled
B. The controller action is missing
C. Strong Parameters are not used correctly
D. The database schema is incorrect
Solution
Step 1: Analyze the use of Strong Parameters
The code shows permitted params, but if the controller does not use this filtered params hash for updating, mass assignment can happen.
Step 2: Identify why admin-only fields update
If the controller uses unfiltered params or merges extra params, Strong Parameters are bypassed, causing security issues.
Final Answer:
Strong Parameters are not used correctly -> Option C
Quick Check:
Mass assignment issue = incorrect Strong Parameters use [OK]
Hint: Always use filtered params for updates [OK]
Common Mistakes:
Assuming CSRF causes mass assignment
Ignoring how params are passed to update
Blaming database instead of controller code
5. You want to ensure all Rails app traffic is secure and user data is protected. Which combination of practices is best?
hard
A. Only use HTTPS and ignore CSRF and output escaping
B. Disable CSRF protection, use HTTP, and sanitize database inputs
C. Use HTTP, skip escaping output, and rely on user validation
D. Force HTTPS, enable CSRF protection, and escape all output
Solution
Step 1: Identify key security practices for web apps
Forcing HTTPS encrypts data in transit, CSRF protection stops fake requests, and escaping output prevents XSS.
Step 2: Evaluate each option's security coverage
Force HTTPS, enable CSRF protection, and escape all output covers all three essential protections. Others disable or ignore important protections, increasing risk.
Final Answer:
Force HTTPS, enable CSRF protection, and escape all output -> Option D
Quick Check:
HTTPS + CSRF + escaping = best security [OK]
Hint: Combine HTTPS, CSRF, and escaping for strong security [OK]
Common Mistakes:
Ignoring CSRF protection importance
Thinking HTTPS alone is enough
Skipping output escaping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.