📖 Scenario: You are building a simple Rails web application that allows users to submit a contact form. To keep your app safe from malicious attacks, you need to add protection against Cross-Site Request Forgery (CSRF).
🎯 Goal: Learn how to enable and verify CSRF protection in a Rails controller and form, so your app only accepts form submissions from trusted sources.
📋 What You'll Learn
Create a Rails controller named ContactsController with a new and create action
Add CSRF protection by enabling protect_from_forgery in the controller
Create a simple HTML form in the new.html.erb view that includes the CSRF token
Verify that the create action only accepts requests with a valid CSRF token
💡 Why This Matters
🌍 Real World
CSRF protection is essential for any web app that accepts form submissions to prevent attackers from tricking users into submitting unwanted requests.
💼 Career
Understanding and implementing CSRF protection is a key skill for Rails developers to build secure web applications.
Progress0 / 4 steps
1
Create ContactsController with new and create actions
Create a Rails controller named ContactsController with two empty actions: new and create.
Ruby on Rails
Hint
Use def new and def create inside the controller class.
2
Enable CSRF protection in ContactsController
Add the line protect_from_forgery with: :exception inside the ContactsController class to enable CSRF protection.
Ruby on Rails
Hint
Place protect_from_forgery with: :exception inside the controller class but outside any method.
3
Create a form with CSRF token in new.html.erb
In the new.html.erb view file, create a form using form_with helper for the create action. Make sure the form includes the CSRF token automatically.
Ruby on Rails
Hint
Use form_with with url: contacts_path and method: :post. Rails adds the CSRF token automatically.
4
Verify CSRF token in create action
In the create action of ContactsController, add code to safely handle the form submission. The CSRF token will be verified automatically by Rails because of protect_from_forgery. Add a simple redirect to new after submission.
Ruby on Rails
Hint
You don't need to manually check the CSRF token. Just redirect after handling the form.
Practice
(1/5)
1. What is the main purpose of CSRF protection in a Rails application?
easy
A. To prevent unauthorized commands from being executed by malicious websites
B. To speed up database queries
C. To improve the user interface design
D. To encrypt user passwords
Solution
Step 1: Understand CSRF meaning
CSRF stands for Cross-Site Request Forgery, which is an attack where unauthorized commands are sent from a user that the web application trusts.
Step 2: Identify CSRF protection goal
The goal of CSRF protection is to stop these unauthorized commands from malicious websites.
Final Answer:
To prevent unauthorized commands from being executed by malicious websites -> Option A
4. You see this error in your Rails app: ActionController::InvalidAuthenticityToken. Which fix will most likely solve the problem?
medium
A. Ensure your forms include the CSRF token by using Rails form helpers
B. Remove the CSRF token from your forms
C. Add protect_from_forgery with: :exception in ApplicationController
D. Disable CSRF protection in config
Solution
Step 1: Identify cause of InvalidAuthenticityToken
This error happens when the CSRF token is missing or invalid in the form submission.
Step 2: Choose fix to include CSRF token
Using Rails form helpers like form_with automatically includes the CSRF token, fixing the error.
Final Answer:
Ensure your forms include the CSRF token by using Rails form helpers -> Option A
Quick Check:
Fix InvalidAuthenticityToken = include CSRF token [OK]
Hint: Use Rails form helpers to add CSRF token automatically [OK]
Common Mistakes:
Adding protect_from_forgery again when already present
Removing CSRF token which causes error
Disabling protection instead of fixing token
5. You want to allow JSON API requests in your Rails app without CSRF errors but keep protection for HTML forms. Which is the best way to do this?
hard
A. Manually add CSRF tokens to JSON requests
B. Disable CSRF protection globally
C. Use protect_from_forgery with: :null_session and handle API authentication separately
D. Use protect_from_forgery with: :exception and skip CSRF check for JSON requests
Solution
Step 1: Understand CSRF and API requests
CSRF tokens are mainly for browser forms; API JSON requests often don't use them and need different handling.
Step 2: Choose best Rails approach
Using protect_from_forgery with: :null_session disables CSRF exceptions for APIs but keeps protection for forms, requiring separate API authentication.
Final Answer:
Use protect_from_forgery with: :null_session and handle API authentication separately -> Option C
Quick Check:
API JSON + CSRF = null_session + separate auth [OK]
Hint: Use null_session for APIs, keep exception for forms [OK]
Common Mistakes:
Disabling CSRF protection globally
Trying to add CSRF tokens manually to JSON
Skipping CSRF only without alternative auth
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.