What is the primary purpose of the protect_from_forgery method in a Rails controller?
Think about how Rails protects forms and non-GET requests from unauthorized actions.
The protect_from_forgery method helps prevent CSRF attacks by requiring a valid authenticity token in forms and AJAX requests. This token ensures that the request comes from the legitimate user.
Given the following controller code snippet, what will happen if a user tries to submit a form with an extra parameter admin: true?
def user_params params.require(:user).permit(:name, :email, :password) end
Consider how permit controls which parameters are allowed for mass assignment.
Strong parameters only allow the specified attributes (:name, :email, :password) to be assigned. Extra parameters like admin are filtered out and ignored, preventing unauthorized mass assignment.
Which of the following Rails session store configurations correctly enhances security by preventing session fixation and ensuring cookie confidentiality?
Look for options that enable secure cookies and restrict JavaScript access.
Setting secure: true ensures cookies are sent only over HTTPS. httponly: true prevents JavaScript access to cookies, reducing XSS risks. same_site: :strict helps prevent CSRF by restricting cross-site requests.
Consider the following Rails code snippet in a controller:
def search
@users = User.where("name = '#{params[:name]}'")
endWhat is the main security issue with this code?
Think about how user input is used inside the SQL query string.
Directly inserting user input into a SQL query string without sanitization allows attackers to inject malicious SQL code. Rails provides parameterized queries to avoid this risk.
Given the following Rails configuration in config/application.rb:
config.action_dispatch.default_headers = {
'Content-Security-Policy' => "default-src 'self'; img-src https://trusted.com; script-src 'none'"
}What will be the effect on the browser when loading this app?
Review how Content Security Policy directives control resource loading.
The default-src 'self' directive restricts all resources to the same origin by default. The img-src https://trusted.com allows images from that domain. The script-src 'none' blocks all scripts from loading, enhancing security.