Performance: CSRF protection
CSRF protection affects the security layer of web requests but has minimal direct impact on page load speed or rendering performance.
class ApplicationController < ActionController::Base protect_from_forgery with: :exception end <!-- Rails form helpers automatically include CSRF tokens --> <%= form_with url: "/submit" do |form| %> <%= form.text_field :data %> <%= form.submit "Send" %> <% end %>
class ApplicationController < ActionController::Base protect_from_forgery with: :null_session end <!-- Forms do not include CSRF tokens explicitly --> <form action="/submit" method="post"> <input type="text" name="data"> <button type="submit">Send</button> </form>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No CSRF token (insecure) | Minimal DOM nodes | 0 | 0 | [X] Bad |
| Rails form_with with CSRF token | One hidden input added | 0 | Negligible | [OK] Good |
protect_from_forgery with options like :exception to enable CSRF protection.protect_from_forgery with: :exception is valid Rails syntax to enable CSRF protection.<%= form_with url: '/posts' do |form| %> <%= form.text_field :title %> <%= form.submit 'Create' %> <% end %>
ActionController::InvalidAuthenticityToken error to block the request.ActionController::InvalidAuthenticityToken. Which fix will most likely solve the problem?form_with automatically includes the CSRF token, fixing the error.protect_from_forgery with: :null_session disables CSRF exceptions for APIs but keeps protection for forms, requiring separate API authentication.Pass the test and this pattern moves unknown