0
0
Ruby on Railsframework~5 mins

CSRF protection in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does CSRF stand for and what problem does it solve?
CSRF stands for Cross-Site Request Forgery. It protects web applications by preventing unauthorized commands from being transmitted from a user that the web app trusts.
Click to reveal answer
beginner
How does Rails protect against CSRF attacks by default?
Rails includes a CSRF token in forms and verifies this token on the server for each non-GET request. If the token is missing or invalid, Rails rejects the request.
Click to reveal answer
intermediate
What is the purpose of the protect_from_forgery method in Rails?
The protect_from_forgery method enables CSRF protection in Rails controllers by checking the CSRF token on requests that change data.
Click to reveal answer
intermediate
Why should CSRF tokens be unique per user session?
Unique tokens per session ensure that attackers cannot guess or reuse tokens from other users, making CSRF attacks much harder to perform.
Click to reveal answer
advanced
How can you disable CSRF protection for specific actions in a Rails controller?
You can disable CSRF protection for specific actions by using skip_before_action :verify_authenticity_token, only: [:action_name] in the controller.
Click to reveal answer
What does Rails check to protect against CSRF attacks?
AUser's browser type
BUser's IP address
CA secret token included in forms and headers
DThe request URL
Which HTTP methods does Rails typically protect with CSRF tokens?
APOST, PUT, PATCH, DELETE
BAll HTTP methods
COPTIONS only
DGET and HEAD
What happens if a CSRF token is missing or invalid in a Rails request?
AThe user is redirected to the homepage
BThe request is processed normally
CRails logs a warning but processes the request
DRails raises an exception and rejects the request
How can you include a CSRF token in an HTML form in Rails views?
AManually add a hidden input with the token
BUse Rails form helpers like <code>form_with</code> or <code>form_for</code>
CAdd a meta tag in the head
DNo need to include it in forms
When might you want to disable CSRF protection in a Rails controller?
AFor API endpoints that use other authentication methods
BFor all user-facing forms
CFor GET requests
DNever disable CSRF protection
Explain how Rails uses CSRF tokens to protect web applications from attacks.
Think about what Rails adds to forms and checks on the server.
You got /4 concepts.
    Describe how you can manage CSRF protection in a Rails controller, including enabling and disabling it.
    Consider controller methods that control CSRF checks.
    You got /3 concepts.