Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn off CSRF protection in a rails app?

Tags:

The CSRF prevention built in to Rails is causing some problems for some automated load testing we are doing, and I want to turn it off for the duration of the process. How do I do this?

like image 824
Laurie Young Avatar asked Sep 26 '08 08:09

Laurie Young


People also ask

Can we turn off CSRF protection?

To disable CSRF protection on all routes. So navigate to app\Http\Middleware and open VerifyCsrfToken. php file. Then update the routes, which you want to disable CSRF protection.

How do I disable CSRF in application properties?

Disable using security configuration code The spring boot security application allows to configure the security details in a customized class that extends WebSecurityConfigurerAdapter class. The CSRF feature can be disabled using the code “ http. csrf(). disable ()”.

How does Rails protect against CSRF?

Briefly, Cross-Site Request Forgery (CSRF) is an attack that allows a malicious user to spoof legitimate requests to your server, masquerading as an authenticated user. Rails protects against this kind of attack by generating unique tokens and validating their authenticity with each submission.

Where is CSRF token stored in rails?

The real csrf token is stored in the session like so: session[:_csrf_token]. If it is does not exist already, it is generated using a Secure Random function, and stored base64 encoded. As it is binary data, the token is then base64 decoded before returning to the calling function.


2 Answers

I love simple questions with clear answers.

#I go in application.rb
self.allow_forgery_protection = false

If you want to do this for testing only you can move that into one of the environment files (obviously, you'll be touching Application then rather than self). You could also write something like:

#I still go in application.rb
self.allow_forgery_protection = false unless ENV["RAILS_ENV"] == "production"

See here for details. (Continuing Rails' wonderful tradition of having documentation of core features in 2 year old blog posts, which were distilled from commit logs.)

like image 80
Patrick McKenzie Avatar answered Oct 18 '22 05:10

Patrick McKenzie


In Rails 3, remove the protect_from_forgery command in app/controllers/application_controller.rb

like image 26
Jeff Dickey Avatar answered Oct 18 '22 05:10

Jeff Dickey