Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off CSRF token in rails 3

I have a rails app that serves some APIs to an iPhone application. I want to be able to simply post on a resource without minding on get the correct CSRF token. I tried some methods that I see here in stackoverflow but it seems they no longer work on rails 3.

Thank you for helping me.

like image 921
Simone D'Amico Avatar asked Apr 14 '11 20:04

Simone D'Amico


People also ask

How do I turn off CSRF?

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.

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.

What is CSRF token in rails?

Rails CSRF TokenThe server generates these tokens, links them to the user session, and stores them in the database. This token is then injected into any form presented to the client as a hidden field. When the client correctly submits the form for validation, it passes the token back to the server.

How does rails verify CSRF token?

When a user makes a POST request, the CSRF token from the HTML gets sent with that request. Rails compares the token from the page with the token from the session cookie to ensure they match.


1 Answers

In the controller where you want to disable CSRF the check:

skip_before_action :verify_authenticity_token 

Or to disable it for everything except a few methods:

skip_before_action :verify_authenticity_token, :except => [:update, :create] 

Or to disable only specified methods:

skip_before_action :verify_authenticity_token, :only => [:custom_auth, :update] 

More info: RoR Request Forgery Protection

like image 199
Mike Lewis Avatar answered Oct 05 '22 10:10

Mike Lewis