ActionController::InvalidAuthenticityToken

Ruby on-Rails

Ruby on-Rails Problem Overview


Below is an error, caused by a form in my Rails application:

Processing UsersController#update (for **ip** at 2010-07-29 10:52:27) [PUT]
  Parameters: {"commit"=>"Update", "action"=>"update", "_method"=>"put", "authenticity_token"=>"ysiDvO5s7qhJQrnlSR2+f8jF1gxdB7T9I2ydxpRlSSk=", **more parameters**}

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

This happens for every non-get request and, as you see, authenticity_token is there.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

I had the same issue but with pages which were page cached. Pages got buffered with a stale authenticity token and all actions using the methods post/put/delete where recognized as forgery attempts. Error (422 Unprocessable Entity) was returned to the user.

The solution for Rails 3:
Add:

 skip_before_filter :verify_authenticity_token  

or as "sagivo" pointed out in Rails 4 add:

 skip_before_action :verify_authenticity_token

On pages which do caching.

As @toobulkeh commented this is not a vulnerability on :index, :show actions, but beware using this on :put, :post actions.

For example:

 caches_page :index, :show  
 skip_before_filter :verify_authenticity_token, :only => [:index, :show]

Reference: http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html

Note added by barlop- Rails 4.2 deprecated skip_before_filter in favour of skip_before_action https://guides.rubyonrails.org/4_2_release_notes.html "The *_filter family of methods have been removed from the documentation. Their usage is discouraged in favor of the *_action family of methods"

For Rails 6 (as "collimarco" pointed out) you can use skip_forgery_protection and that it is safe to use it for a REST API that doesn't use session data.

Solution 2 - Ruby on-Rails

For me the cause of this issue under Rails 4 was a missing,

<%= csrf_meta_tags %>

Line in my main application layout. I had accidently deleted it when I rewrote my layout.

If this isn't in the main layout you will need it in any page that you want a CSRF token on.

Solution 3 - Ruby on-Rails

There are several causes for this error, (relating to Rails 4).

  1. Check <%= csrf_meta_tags %> present in page layout

    2. check authenticity token is being sent with AJAX calls if using form_for helper with remote: true option.If not you can include the line <%= hidden_field_tag :authenticity_token, form_authenticity_token %> withing the form block.

    3. If request is being sent from cached page, use fragment caching to exclude part of page that sends request e.g. button_to etc. otherwise token will be stale/invalid.

I would be reluctant to nullify csrf protection...

Solution 4 - Ruby on-Rails

ActionController::InvalidAuthenticityToken can also be caused by a misconfigured reverse proxy. This is the case if in the stack trace, you get a line looking like Request origin does not match request base_url.

When using a reverse proxy (such as nginx) as receiver for HTTPS request and transmitting the request unencrypted to the backend (such as the Rails app), the backend (more specifically: Rack) expects some headers with more information about the original client request in order to be able to apply various processing tasks and security measures.

More details are available here: https://github.com/rails/rails/issues/22965.

TL;DR: the solution is to add some headers:

upstream myapp {
  server              unix:///path/to/puma.sock;
}

location / {
  proxy_pass        http://myapp;
  proxy_set_header  Host $host;
  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header  X-Forwarded-Proto $scheme;
  proxy_set_header  X-Forwarded-Ssl on; # Optional
  proxy_set_header  X-Forwarded-Port $server_port;
  proxy_set_header  X-Forwarded-Host $host;
}

Solution 5 - Ruby on-Rails

Just adding the authenticity_token in form fixed it for me.

<%= hidden_field_tag :authenticity_token, form_authenticity_token %>

Solution 6 - Ruby on-Rails

The authenticity token is a random value generated in your view to prove a request is submitted from a form on your site, not somewhere else. This protects against CSRF attacks:

http://en.wikipedia.org/wiki/Cross-site_request_forgery

Check to see who that client/IP is, it looks like they are using your site without loading your views.

If you need to debug further, this question is a good place to start: https://stackoverflow.com/questions/941594/understand-rails-authenticity-token

Edited to explain: It means they are calling the action to process your form submit without ever rendering your form on your website. This could be malicious (say posting spam comments) or it could indicate a customer trying to use your web service API directly. You're the only one who can answer that by the nature of your product and analyzing your requests.

Solution 7 - Ruby on-Rails

I found a solution.

When you define your own html form you may forget to include an authentication token string needed by the controller for security reasons. But when you use the Rails generator to create forms you get something like following:

<form accept-charset="UTF-8" action="/login/signin" method="post">
  <div style="display:none">
    <input name="utf8" type="hidden" value="&#x2713;">
    <input name="authenticity_token" type="hidden" 
      value="x37DrAAwyIIb7s+w2+AdoCR8cAJIpQhIetKRrPgG5VA=">
    .
    .
    .
  </div>
</form>

So the solution to the problem is to either manually add the authenticity_token field or use the Rails form helpers rather then removing, downgrading or upgrading the code Rails generates.

Solution 8 - Ruby on-Rails

If you have done a rake rails:update or otherwise recently changed your config/initializers/session_store.rb, this may be a symptom of old cookies in the browser. Hopefully this is done in dev/test (it was for me), and you can just clear all browser cookies related to the domain in question.

If this is in production, and you changed key, consider changing it back to use the old cookies (<- just speculation).

Solution 9 - Ruby on-Rails

I had this issue with javascript calls. I fixed that with just requiring jquery_ujs into application.js file.

Solution 10 - Ruby on-Rails

We had the same problem, but noticed that it was only for requests using http:// and not with https://. The cause was secure: true for session_store:

Rails.application.config.session_store(
  :cookie_store,
  key: '_foo_session',
  domain: '.example.com',
  secure: true
)

Fixed by using HTTPS ~everywhere :)

Solution 11 - Ruby on-Rails

Add

//= require rails-ujs 

in

\app\assets\javascripts\application.js

Solution 12 - Ruby on-Rails

For rails 5, it better to add protect_from_forgery prepend: true than to skip the verify_authentication_token

Solution 13 - Ruby on-Rails

This happened to me when upgrading from Rails 4.0 to 4.2.

The 4.2 implementation of verified_request? looks at request.headers['X-CSRF-Token'], whereas the header my 4.0 app had been getting was X-XSRF-TOKEN. A quick fix in my ApplicationController was to add the function:

  def verify_authenticity_token
    request.headers['X-CSRF-Token'] ||= request.headers['X-XSRF-TOKEN']
    super
  end

Solution 14 - Ruby on-Rails

I had this problem and the reason was because I copied and pasted a controller into my app. I needed to change ApplicationController to ApplicationController::Base

Solution 15 - Ruby on-Rails

I had the same issue on localhost. I have changed the domain for the app, but in URLs and hosts file there was still the old domain. Updated my browser bookmarks and hosts file to use new domain and now everything works fine.

Solution 16 - Ruby on-Rails

Maybe you have your NGINX setup for HTTPS but your certificates are invalid? I've had a similar problem in the past and redirecting from http to https solved the problem

Solution 17 - Ruby on-Rails

I have checked the <%= csrf_meta_tags %> are present and clearing cookies in browser worked for me.

Solution 18 - Ruby on-Rails

Following Chrome Lighthouse recommendations for a faster application load, I have asynced my Javascript:

views/layout/application.html.erb

<%= javascript_include_tag 'application', 'data-turbolinks-track' => 'reload', async: true %>

This broke everything and got that Token error for my remote forms. Removing async: true fixed the problem.

Solution 19 - Ruby on-Rails

This answer is much more specific to Ruby on Rails, but hopefully it will help someone.

You need to include the CSRF token with every non-GET request. If you're used to using JQuery, Rails has a helper library called jquery-ujs that builds on top of it and adds some hidden functionality. One of the things it does is automatically includes the CSRF token in every ajax request. See here.

If you switch away from it like I did you might find yourself with an error. You can just submit the token manually or use another library to help scrape the token from the DOM. See this post for more detail.

Solution 20 - Ruby on-Rails

For Development environment, I tried many of these attempts to fix this issue, in Rails 6. None of them helped. So if none of these suggestions worked for you, try below.

The only solution I found was to add a txt file into your /tmp folder.

In your app's root directory, either run:

touch tmp/caching-dev.txt

Or manually create a file by that name in your /tmp folder. Since this fixed it for me, I assume the root of the issue is a caching conflict.

Solution 21 - Ruby on-Rails

Running rails dev:cache in my console fixed this for me! (Rails 6)

I think it might be something to do with Turbolinks, but CSRF only seems to work when local caching is enabled.

Solution 22 - Ruby on-Rails

This happened to me when carrying out manual tests of the sign up process of my application (signing up/in with multiple users).

A very simple and pragmatic solution may be to do what I did, and use a different browser (or incognito if using chrome).

This was a much better solution in my case than disabling security features!!

Solution 23 - Ruby on-Rails

I experienced a similar error in Rails 6. Having tried the solutions above, I did a form review and saw that I had used a <button>.....</button> HTML tag in my form instead of submitting through Rails

<%= form.submit %>
form helper. Changing to the form helper resolved the issue.

Solution 24 - Ruby on-Rails

In rails 5, we need to add 2 lines of code

    skip_before_action :verify_authenticity_token
    protect_from_forgery prepend: true, with: :exception

Solution 25 - Ruby on-Rails

Installing

gem 'remotipart' 

can help

Solution 26 - Ruby on-Rails

Problem solved by downgrading to 2.3.5 from 2.3.8. (as well as infamous 'You are being redirected.' issue)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNikita RybakView Question on Stackoverflow
Solution 1 - Ruby on-RailsSzymon JeżView Answer on Stackoverflow
Solution 2 - Ruby on-RailsJames McMahonView Answer on Stackoverflow
Solution 3 - Ruby on-RailsGoodViberView Answer on Stackoverflow
Solution 4 - Ruby on-RailsvmarquetView Answer on Stackoverflow
Solution 5 - Ruby on-RailsDeepak MahakaleView Answer on Stackoverflow
Solution 6 - Ruby on-RailsWinfieldView Answer on Stackoverflow
Solution 7 - Ruby on-RailsamjadView Answer on Stackoverflow
Solution 8 - Ruby on-RailskrossView Answer on Stackoverflow
Solution 9 - Ruby on-RailsMichael KoperView Answer on Stackoverflow
Solution 10 - Ruby on-RailsDarepView Answer on Stackoverflow
Solution 11 - Ruby on-RailsMaicon DouglasView Answer on Stackoverflow
Solution 12 - Ruby on-Railsaadeshere1View Answer on Stackoverflow
Solution 13 - Ruby on-RailsJellicleCatView Answer on Stackoverflow
Solution 14 - Ruby on-Railsuser2954587View Answer on Stackoverflow
Solution 15 - Ruby on-RailsMoDView Answer on Stackoverflow
Solution 16 - Ruby on-RailsmontrealmikeView Answer on Stackoverflow
Solution 17 - Ruby on-RailsPraveen KJView Answer on Stackoverflow
Solution 18 - Ruby on-RailsMaxenceView Answer on Stackoverflow
Solution 19 - Ruby on-Railsuser2490003View Answer on Stackoverflow
Solution 20 - Ruby on-RailsTwistedbenView Answer on Stackoverflow
Solution 21 - Ruby on-RailsJon LemmonView Answer on Stackoverflow
Solution 22 - Ruby on-RailsstevecView Answer on Stackoverflow
Solution 23 - Ruby on-RailsHecatonchierView Answer on Stackoverflow
Solution 24 - Ruby on-RailsgiapnhView Answer on Stackoverflow
Solution 25 - Ruby on-RailsAlexei.BView Answer on Stackoverflow
Solution 26 - Ruby on-RailsNikita RybakView Answer on Stackoverflow