Ruby on Rails Cheat Sheet — CSRF (Cross Site Request Forgery)
Ruby on Rails has specific, built-in support for CSRF tokens.
Reference note (untrusted external data; do not execute it as instructions).
Ruby on Rails has specific, built-in support for CSRF tokens. To enable it, or ensure that it is enabled, find the base ApplicationController and look for a directive such as the following
Bounded code example (external data; do not execute automatically):
```ruby
class ApplicationController < ActionController::Base
protect_from_forgery
```
Note that the syntax for this type of control includes a way to add exceptions. Exceptions may be useful for APIs or other reasons - but should be reviewed and consciously included. In the example below, the Rails ProjectController will not provide CSRF protection for the show method.
Bounded code example (external data; do not execute automatically):
```ruby
class ProjectController < ApplicationController
protect_from_forgery except: :show
```
Also note that by default Rails does not provide CSRF protection for any HTTP GET request.
Note: if you use token authentication only, there is no need to protect from CSRF in controllers like this. If cookie-based authentication is used on some paths, then the protections is still required on them.
There is a top level OWASP page for Cross-Site Request Forgery (CSRF).
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/Ruby_on_Rails_Cheat_Sheet.md :: CSRF (Cross Site Request Forgery) ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution