# Error: "ActionController::InvalidAuthenticityToken"

**URL:** <https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916>\
**Category:** Rails\
**Created:** [February 22, 2010, 5:01pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916 "2010-02-22T17:01:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 22, 2010, 5:01pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/1 "2010-02-22T17:01:35Z")

</div>

Hi there,

I have my first Rails app running and I regularly get the following  
“logged\_exception” error message:

“ActionController::InvalidAuthenticityToken”

Has anybody an idea what might cause this problem? Could it somehow be a  
“time out” error (like an “AuthenticityToken” which might expire after a  
certain time, or something along those lines)?

Any idea how that error could be prevented from occurring?

The “backtrace” always starts like this:

=================  
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action\_controller/request\_forgery\_protection.rb:79:in  
`verify\_authenticity\_token’

/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/callbacks.rb:178:in  
`send’

/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/callbacks.rb:178:in  
`evaluate\_method’

# /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/callbacks.rb:166:in `call’ …

Thanks for any help with this!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 22, 2010, 5:28pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/2 "2010-02-22T17:28:20Z")

</div>

The Authenticity Token is a value that is inserted in to forms (when  
using  
the form\_for helper) that is then checked when the submit request is  
sent.  
It helps prevent CSRF attacks.

What is likely happening is that you’re generating your own form and not  
including the token (which you can do by inserting a hidden field and  
using  
the authenticity\_token helper).

You can learn more about CSRF and Rails’ protection at:

> **[Cross-site request forgery](https://en.wikipedia.org/wiki/Cross-site_request_forgery)**
>
> Cross-site request forgery, also known as one-click attack or session riding and abbreviated as CSRF (sometimes pronounced sea-surf) or XSRF, is a type of malicious exploit of a website or web application where unauthorized commands are submitted from a user that the web application trusts. There are many ways in which a malicious website can transmit such commands; specially-crafted image tags, hidden forms, and JavaScript fetch or XMLHttpRequests, for example, can all work without the us The t...

> **[Securing Rails Applications — Ruby on Rails Guides](https://guides.rubyonrails.org/security.html#cross-site-request-forgery-csrf)**
>
> Securing Rails ApplicationsThis manual describes common security problems in web applications and how to avoid them with Rails.After reading this guide, you will know: All countermeasures that are highlighted. The concept of sessions in Rails, what...

Cheers,

Andy

–  
Andy J.  
[http://andyjeffries.co.uk/](http://andyjeffries.co.uk/)#rubyonrails #mysql #jquery  
Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS  
Company number: 5452840

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 22, 2010, 9:34pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/3 "2010-02-22T21:34:49Z")

</div>

Yeah, Andy is right. Why don’t you send us the code that generates these  
error requests? I have seen this error in some Ajax components like  
autocomplete. They create a form but do not send the token.

I post this into my blog:  
[http://blogdomario.wordpress.com/2009/05/29/autocomplete-versus-rails-2-x/](http://blogdomario.wordpress.com/2009/05/29/autocomplete-versus-rails-2-x/)

2010/2/22 Andy J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> [Cross-site request forgery - Wikipedia](http://en.wikipedia.org/wiki/Cross-site_request_forgery)  
> Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS
> 
> > “ActionController::InvalidAuthenticityToken”
> 
> –  
> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> To unsubscribe from this group, send email to  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
MÃ¡rio SÃ©rgio Coelho Marroquim

> **[blogdomario](https://blogdomario.wordpress.com)**

[http://www.muraldeideias.com.br](http://www.muraldeideias.com.br)

> **[Credishop](https://www.credishop.com.br)**
>
> Thoughts, stories and ideas.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 23, 2010, 6:24pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/4 "2010-02-23T18:24:57Z")

</div>

Thanks for your hints…

Well, the code is actually the following:

\<%= check\_box\_tag ‘applicationfile\_verified’,  
nil,  
applicationfile.verified,  
{ :onclick =\> “#{remote\_function(:url =\> {  
:controller =\>  
‘applicationfiles’,  
:action =\> ‘verify’,  
:id =\> applicationfile.id })}”  
} %\>

…which results in the following source…

And since the source includes…

```
" data:'authenticity_token=' + encodeURIComponent('xV3Ayw...9Nnjs=') 

```

"

…it would mean the Authenticity Token is there and OK, right?

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 23, 2010, 12:43pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/5 "2010-02-23T12:43:54Z")

</div>

It could also be caused by users with cookies disabled in the browser,  
incorrect protect\_from\_forgery settings, or caching of authenticity  
tokens.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 14, 2010, 3:45pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/6 "2010-05-14T15:45:02Z")

</div>

Have you cached your view by any chance? That would mean that the  
authenticity\_token in the view is stored in a cached file and not really  
dynamic?

Cheers,  
Aditya

Tom Ha wrote:

> Hi there,
> 
> I have my first Rails app running and I regularly get the following  
> “logged\_exception” error message:
> 
> “ActionController::InvalidAuthenticityToken”
> 
> Has anybody an idea what might cause this problem? Could it somehow be a  
> “time out” error (like an “AuthenticityToken” which might expire after a  
> certain time, or something along those lines)?
> 
> Any idea how that error could be prevented from occurring?
> 
> The “backtrace” always starts like this:
> 
> =================  
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action\_controller/request\_forgery\_protection.rb:79:in  
> `verify\_authenticity\_token’
> 
> /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/callbacks.rb:178:in  
> `send’
> 
> /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/callbacks.rb:178:in  
> `evaluate\_method’
> 
> # /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/callbacks.rb:166:in `call’ …
> 
> Thanks for any help with this!  
> Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 23, 2010, 10:37pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/7 "2010-02-23T22:37:23Z")

</div>

Seems fine to me!

2010/2/23 Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> ```
> :action => 'verify',
> 
> ```
> 
> ## type=“checkbox”\>
> 
> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> To unsubscribe from this group, send email to  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
MÃ¡rio SÃ©rgio Coelho Marroquim

> **[blogdomario](https://blogdomario.wordpress.com)**

[http://www.muraldeideias.com.br](http://www.muraldeideias.com.br)

> **[Credishop](https://www.credishop.com.br)**
>
> Thoughts, stories and ideas.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 14, 2010, 6:09pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/8 "2010-05-14T18:09:10Z")

</div>

Only sometimes…

Maybe due to bots…?

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 14, 2010, 5:53pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/9 "2010-05-14T17:53:49Z")

</div>

Does it always do it or only sometimes?

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 14, 2010, 7:26pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/10 "2010-05-14T19:26:59Z")

</div>

On 14 May 2010, at 18:09, Tom Ha wrote:

> Only sometimes…
> 
> Maybe due to bots…?

We’ve had it happen on random occasions while using the RESTful  
authentication plugin after upgrading Rails. I remember reading a  
ticket issue somewhere about it and iirc it has to do with something  
funky in reset\_session. But since we had the need for Rack-based  
authentication as well as some other features, we switched to Devise  
and have had no error reports ever since.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 14, 2010, 7:45pm UTC](https://www.ruby-forum.com/t/error-actioncontroller-invalidauthenticitytoken/183916/11 "2010-05-14T19:45:53Z")

</div>

I can confirm that I use the RESTful authentication plugin, too.

Thanks for your input!
