views:

362

answers:

2

I've recently switched from storing session data in a cookie to storing it in the database.

Now every POST request causes an ActionController::InvalidAuthenticityToken error. It only happens in Firefox, and only on my home machine (I tried it out at work today in the same version of FF and everything was fine). This leads me to believe that it's something do do with the cookie I deleted yesterday...

Here's the relevant part of environment.rb:

# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
# Make sure the secret is at least 30 characters and all random, 
# no regular words or you'll be exposed to dictionary attacks.
config.action_controller.session = {
:session_key => '_basillslam_session',
:secret      => '373ee5b69a4a31d3318485fs368c41fac6b797a1f5c35693b49bd34e8a96291b92dd577bd49de7aeea56c9ffa1af2d8386bafe857220cafacfa0028f01be357d78'
}

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with 'rake db:sessions:create')
config.action_controller.session_store = :active_record_store

In application.rb:

protect_from_forgery :secret => 'f1d54db45b47ec94a6a54b1e744fafa6'

Here's the part of the full trace where the error is thrown:

C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/request_forgery_protection.rb:79:in `verify_authenticity_token'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:469:in `send!'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:469:in `call'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:441:in `run'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:716:in `run_before_filters'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:695:in `call_filters'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in `perform_action_without_benchmark'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in `perform_action_without_caching'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:678:in `perform_action'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache'
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/query_cache.rb:8:in `cache'
[etc. ... ]

Has anybody experienced this before? Or does anybody know why this might be happening?

+3  A: 

The exact same thing happened to me when I switched one of my sites. Delete the Rails session cookie for that site in your home Firefox Browser.

It only happens if you had a pre-existing session that used the cookie store. So, hopefully, only you and your browser will ever see the issue. After you've deleted the cookie, you'll never see the error again.

With the session in the database and only a random key to map to that session in the browsers cookies, the session protection magic is no longer necessary.

Otto
Thanks Otto, I had a feeling it'd be something to do with old cookies. The thing is though, I'm pretty sure I've deleted all the cookies for this application (the machine has been turned off more than once, which would have cleared sessions, right?). Is there some specific step I'm missing out?
joecorcoran
Also, is it okay that the :session_key and :secret are being set both in environment.rb and application.rb? Is this causing confusion perhaps? I just made sure my cookies are deleted and I'm still getting the same error. Weird.
joecorcoran
Thanks for all your comments Otto. I had to resort to re-installing Firefox. A little annoying that deleting my session cookies wasn't fixing the problem, since that's almost definitely why the error was being thrown.
joecorcoran
+1  A: 

Thanks to Otto, I now know the reason why this happens. Although, I was able get around the problem by writing <%= token_tag %> within the form which is being POSTed. The token_tag puts a hidden field containing the authenticity token in the form and hence the ActionController::InvalidAuthenticityToken error vanishes.

Chirantan
Thanks, Chirantan, I'll give this a go tonight. It seems odd that I should have to make a change to the site to make it work on my home machine though! I'm pretty sure that Rails's form helpers include the necessary authenticity_token by default, right?
joecorcoran
Makes sense, turning off the cookie session store probably makes the form helpers not insert that anymore.
Otto