0
0
Ruby on Railsframework~20 mins

CSRF protection in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSRF Protection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Rails form is submitted without a CSRF token?

Consider a Rails application with default CSRF protection enabled. What will happen if a form is submitted without including the CSRF token?

AThe server will reject the request with an <strong>ActionController::InvalidAuthenticityToken</strong> error.
BThe server will accept the request and process it normally without any error.
CThe server will redirect the user to the login page automatically.
DThe server will log a warning but still process the request.
Attempts:
2 left
💡 Hint

Think about what Rails does to protect against forged requests.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly includes the CSRF token in a Rails form?

In Rails views, which code snippet correctly ensures the CSRF token is included in the form?

A<code>&lt;form&gt;&lt;input type='hidden' name='csrf_token' value='&lt;%= csrf_token %&gt;'&gt;&lt;/form&gt;</code>
B<code>&lt;form&gt;&lt;%= csrf_meta_tags %&gt;&lt;/form&gt;</code>
C<code>&lt;%= form_with model: @post do |form| %&gt;&lt;/form&gt;</code>
D<code>&lt;form&gt;&lt;input type='hidden' name='authenticity_token' value='&lt;%= form_authenticity_token %&gt;'&gt;&lt;/form&gt;</code>
Attempts:
2 left
💡 Hint

Look for the correct hidden input name and method to get the token.

🔧 Debug
advanced
2:00remaining
Why does disabling CSRF protection cause a security risk?

A developer disables CSRF protection in a Rails controller using skip_before_action :verify_authenticity_token. What is the main security risk introduced?

AThe application will crash when receiving POST requests.
BAttackers can trick users into submitting unwanted requests on their behalf, potentially changing data without consent.
CUsers will be logged out automatically after each request.
DThe server will reject all AJAX requests.
Attempts:
2 left
💡 Hint

Think about what CSRF protection prevents.

🧠 Conceptual
advanced
2:00remaining
How does Rails verify the CSRF token on incoming requests?

Which statement best describes how Rails verifies the CSRF token on POST, PATCH, PUT, or DELETE requests?

ARails verifies the token by decoding a JWT sent in the request headers.
BRails checks if the token matches a value stored in a cookie named <code>csrf_token</code>.
CRails compares the token submitted in the form data or headers with the token stored in the user session.
DRails does not verify tokens on non-GET requests by default.
Attempts:
2 left
💡 Hint

Consider where Rails stores the token and how it compares it.

state_output
expert
2:00remaining
What is the value of session[:_csrf_token] after a valid form submission?

Given a Rails app with default CSRF protection, after a user submits a valid form with a correct CSRF token, what happens to session[:_csrf_token]?

AIt remains the same as before the request, preserving the token for future requests.
BIt is cleared and set to <code>nil</code> after each valid request.
CIt changes to a new random token after each valid form submission.
DIt stores the submitted token value from the form for the current request only.
Attempts:
2 left
💡 Hint

Think about token reuse and session persistence.