Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Ruby on Rails from sending the session header

How do I prevent Rails from always sending the session header (Set-Cookie). This is a security problem if the application also sends the Cache-Control: public header.

My application touches (but does not modify) the session hash in some/most actions. These pages display no private content so I want them to be cacheable - but Rails always sends the cookie header, no matter if the sent session hash is different from the previous or not.

What I want to achieve is to only send the hash if it is different from the one received from the client. How can you do that? And probably that fix should also go into official Rails release? What do you think?

like image 850
hurikhan77 Avatar asked Feb 27 '26 15:02

hurikhan77


2 Answers

Rails only adds the session cookie data to the Set-Cookie header if it has been touched. You might be setting things to the values that they already contain - it's not smart enough to check to see if the data is actually different.

edit My response is a little misleading. When you are using the cookie session store, a new cookie is set if the cookie value (after Marshaling) changes.

See actionpack/lib/action_controller/session/cookie_store.rb

like image 154
outcassed Avatar answered Mar 01 '26 04:03

outcassed


For Rails 3 then use this.

env['rack.session.options'][:skip] = true

or the equivalent

request.session_options[:skip] = true

You can find the documentation for it here http://doc.rubyists.com/rack/Rack/Session/Abstract/ID.html

like image 27
Darwin Avatar answered Mar 01 '26 05:03

Darwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!