Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails always resend the same cookie with active_record sessions

#2485

Hi,

I see that a rails application with active_record session storage always resend the same cookie to the web browser for each request. When using cookies session storage, one cookie is sent at the first connection but reloading the page (or going to another page) does not trigger the resending of the cookie.

Step to reproduce : 1) create an empty rails application 2) configure connection to database (if needed) 3) create a controller with a view 4) run WEBrick and use a browser (where you need to ack each cookie) => you get one cookie the first time you load the view page => you do not get a cookie any more if you reload the page (or if you go to another page) 5) enable ActiveRecord session: rake db:sessions:create rake db:migrate and edit config/environment.rb to enable it 6) run WEBrick and use a browser (where you need to ack each cookie) => you get the same cookie each time you load or reload a page !

I tested this behavior with rails 2.1 and 2.2 (not tested with rails 2.3) Can someone tell me how to avoid the resending of the same cookie with active_record session storage ?

Regards,

Vincent

Reported by Vincent Danjean · April 12th, 2009 @ 07:20 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: none

Activity

  1. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)
    • State changed from new to verified
    • Tag set to 2.3.6, 3.0, actionpack, sessions
    • Assigned user set to Prem Sichanugrist (sikachu)

    +1 on both 3.0 and 2.3.5. Will provide a patch soon.

    Also, I found this behavior in every session_store. Global patching will be fine

    January 20th, 2010 @ 04:06 PM

  2. donncha
    donncha

    Possibly??

    module ActionController #:nodoc:
      module Cookies
    
      protected
        # Returns the cookie container, which operates as described above.
        def cookies
    -      @cookies ||= CookieJar.new(self)
    +      CookieJar.new(self)
        end
      end
    end
    

    February 8th, 2010 @ 03:22 AM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  4. Rizwan Reza
    Rizwan Reza
    • Tag changed from 2.3.6, 3.0, actionpack, sessions to 2.3.6, 3.0, actionpack, bugmash, sessions
    • State changed from verified to open

    May 16th, 2010 @ 05:23 PM

  5. Rohit Arondekar
    Rohit Arondekar

    Any updates here? Can somebody please try and verify if this is still an issue on master.

    June 24th, 2010 @ 11:50 AM

  6. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)

    Hi,

    I can verify that it still apply to current master. I was working on it, but didn't finish it.

    Will come back and provide patch for it soon.

    June 24th, 2010 @ 02:35 PM

  7. José Valim
    José Valim

    What always resending the cookie is an issue?

    June 24th, 2010 @ 08:07 PM

  8. José Valim
    José Valim

    Sorry! For ActiveRecord store, since the cookie only contains the session_id, it does not make sense to send the same cookie every time. Yes, this should be fixed!

    June 24th, 2010 @ 08:11 PM

  9. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)

    Ok, so I've patched the abstract_store to check that the session_id value has been changed or not. If not, then it won't call the []= of cookie jar, which was resulted in sending the cookie back to user again.

    Noted that I also checked that the expired is not set. If it does, it will always resend the session_id cookie regardless of whether it's already set or not, to extend the expire time.

    I've attach the patch that cleanly apply on master. Patch for 2-3-stable hopefully will come soon.

    June 24th, 2010 @ 08:45 PM

  10. José Valim
    José Valim
    • Milestone cleared.
    • Assigned user changed from Prem Sichanugrist (sikachu) to José Valim

    June 24th, 2010 @ 08:54 PM

  11. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)
    • Tag changed from 2.3.6, 3.0, actionpack, bugmash, sessions to 2.3.x, 3.0, actionpack, bugmash, sessions

    It isn't quite hard to backport, so here it is

    June 24th, 2010 @ 09:24 PM

  12. Repository
    Repository
    • State changed from open to resolved

    (from [617e94658ddc3f71c42fc4f8c2346f87ff106bbe]) Make sure that Rails doesn't resent session_id cookie over and over again if it's already there [#2485 Rails always resend the same cookie with active_record sessions state:resolved]

    This apply to only Active Record store and Memcached store, as they both store only the session_id, which will be unchanged, in the cookie.

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/617e94658ddc3f71c42fc4f8c2346f...

    June 25th, 2010 @ 08:43 AM

  13. Repository
    Repository

    (from [f8f365346ec55ef388eaa43e30bd7629c375d274]) Make sure that Rails doesn't resent session_id cookie over and over again if it's already there [#2485 Rails always resend the same cookie with active_record sessions state:resolved]

    This apply to only Active Record store and Memcached store, as they both store only the session_id, which will be unchanged, in the cookie.

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/f8f365346ec55ef388eaa43e30bd76...

    June 25th, 2010 @ 08:49 AM

  14. jsman
    jsman
    • Importance changed from to

    This commit broke the ActiveRecord session store in 2.3.9. If there is currently no cookie with a session id present at the client side, no new cookie is ever created and every request will generate a new session id (but no cookie).

    September 8th, 2010 @ 01:16 AM

  15. Jeremy Kemper