views:

212

answers:

2

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?

A: 

Here is the crucial line:

config.action_controller.session_store = :nil_session_store

See the whole post.

allesklar
Hmm well, but I actually need a session store. I just don't want Rails to send the Set-Cookie response header if it does not differ from the hash that the client sent with the corresponding request header.
hurikhan77
-1 The question was not to have no session store at all
hurikhan77
+1  A: 

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

casey
Yeah I already suspected that... Does "touching" in this sense also mean reading from the session hash? +1 for supporting my suspicion
hurikhan77
Reading is okay. Check out CGI::Session::CookieStore#close in actionpack/lib/action_controller/session/cookie_store.rb
casey