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.

mem_cache_store_test has flawed test

#3238

So I was poking around mem_cache_store_test and noticed that test_prevents_session_fixation passes in both these cases:

  def test_prevents_session_fixation
    with_test_route_set do
      get '/get_session_value'
      assert_response :success
      assert_equal 'foo: nil', response.body
      session_id = cookies['_session_id']
      
      reset!
      
      get '/set_session_value', :_session_id => session_id
      assert_response :success
      assert_equal nil, cookies['_session_id']
    end
  end

AND

  def test_prevents_session_fixation
    with_test_route_set do
      get '/get_session_value'
      assert_response :success
      assert_equal 'foo: nil', response.body
      session_id = cookies['_session_id']
      
      reset!
      
      get '/set_session_value'
      assert_response :success
      assert_equal nil, cookies['_session_id']
    end
  end

(notice the missing :_session_id => session_id in the second)... This just seems to suggest that reset! is doing something to prevent session fixation. From my experience, MemCacheStore allows session fixation (i.e. the client can set their own session_id if they want to).

If I'm mistaken, can anyone explain how this test is actually showing that MemCacheStore's implementation prevents session-fixation?

Thanks!

Reported by Aaron Gibralter · September 20th, 2009 @ 08:07 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Aaron Gibralter
    Aaron Gibralter
    • Tag changed from action_controller, bug, memcachestore, session-fixation, sessions to action_controller, bug, memcachestore, patch, session-fixation, sessions

    Alright, I've created a patch for this and ticket #3134 MemCacheStore allows for arbitrary client generated session_ids?.

    September 20th, 2009 @ 08:51 PM

  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from action_controller, bug, memcachestore, patch, session-fixation, sessions to action_controller, bug, bugmash, memcachestore, patch, session-fixation, sessions

    September 25th, 2009 @ 11:57 AM

  3. Blue Box Chris
    Blue Box Chris

    Unable to run this on a fresh check out.

    error: actionpack/lib/action_controller/session/mem_cache_store.rb: does not exist in index
    error: actionpack/test/controller/session/mem_cache_store_test.rb: does not exist in index
    Patch failed at 0001.
    When you have resolved this problem run "git-am --resolved".
    If you would prefer to skip this patch, instead run

    September 27th, 2009 @ 12:13 AM

  4. Elad Meidar
    Elad Meidar

    @Aaron: Correct me if i'm wrong, but if assert_equal nil, cookies['_session_id'] is passing on both cases, it means that the session_id is not affected by sending a param named _session_id and remains as nil... not sure i see the problem or exactly what is so disturbing around here, care to elaborate some more?

    -1 on the patch though, applies on 2-3-stable only and not on master, iv'e added a patch for master too.

    September 27th, 2009 @ 12:54 AM

  5. Blue Box Stephen
    Blue Box Stephen

    +1 on Elad's patch. applies and passes here.

    September 27th, 2009 @ 02:50 AM

  6. John Pignata
    John Pignata

    +1 verified patch applies and tests pass

    September 27th, 2009 @ 03:19 AM

  7. Aaron Gibralter
    Aaron Gibralter

    Yeah sorry, forgot to say that the patch was for 2-3-stable. The master branch has a new actionpack directory structure... Is there a way to submit a patch for multiple branches? Or should patches always be for master?

    @Elad: Ah, so it's just showing that cookie_only is being enforced... kk I get it.

    September 28th, 2009 @ 04:42 AM

  8. Aaron Gibralter
    Aaron Gibralter

    @Elad: Actually, why does assert_equal nil, cookies['session_id'] pass? The test seems like it's trying to show that the session store ignores the URL param :session_id => session_id; however, shouldn't get '/set_session_value' set a cookie regardless of the params you pass in??

    I do think the test is broken because it seems that reset! is causing the get '/set_session_value' to not set a cookie... See these examples:

    # THIS PASSES:
    def test_prevents_session_fixation
      with_test_route_set do
        reset!
        get '/set_session_value'
        assert_response :success
        assert_equal nil, cookies['_session_id']
      end
    end
    # THIS DOESN'T PASS:
    def test_prevents_session_fixation
      with_test_route_set do
        get '/set_session_value'
        assert_response :success
        assert_equal nil, cookies['_session_id']
      end
    end
    

    October 1st, 2009 @ 07:23 PM

  9. Rizwan Reza
    Rizwan Reza
    • Tag changed from action_controller, bug, bugmash, memcachestore, patch, session-fixation, sessions to action_controller, bug, memcachestore, patch, session-fixation, sessions

    February 12th, 2010 @ 12:46 PM

  10. Lucas Willett
    Lucas Willett
    • Importance changed from to

    The patch from @elad no longer applies cleanly to 2-3-stable or master. Given that this issue has existed for some 13 months now, is this issue still relevant?

    October 15th, 2010 @ 03:17 AM

  11. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:21 PM

  12. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:21 PM

  13. Aaron Gibralter
    Aaron Gibralter

    So I just took another look at this.

    def test_prevents_session_fixation
      with_test_route_set do
        get '/get_session_value'
        assert_response :success
        assert_equal 'foo: nil', response.body
        session_id = cookies['_session_id']
    
        reset!
    
        get '/set_session_value', :_session_id => session_id
        assert_response :success
        assert_not_equal session_id, cookies['_session_id']
      end
    end
    

    Basically, Rails's session fixation protection is that it doesn't accept a session_id as a parameter (by default -- if you pass the session option cookie_only: false session fixation can be a problem). What I was confused about in this test is the part before reset!, but now I guess it makes sense: it's just generating a "valid" session_id.

    Please close this ticket.

    February 14th, 2011 @ 02:50 PM