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.

:attr_accessible nil breaks AR session store

#2073

I'm not sure if this is a bug as such but it's a change that's arisen in edge but wasn't in 2.3 RC1 and earlier.

If you configure your session store as :active_record_store and also include

ActiveRecord::Base.send(:attr_accessible, nil)

in your initializers then sessions break.

Specifically, the session records in the database are stored with a blank "session_id" column and "session" in the controller is empty.

To reproduce, build a trivial Rails application (or use an existing one) with Active Record session store, add ActiveRecord::Base.send(:attr_accessible, nil) to the initializers and clear any existing sessions from the browser and db. On each request one more session records will be generated with an empty session_id column.

Reported by Peter Nash · February 25th, 2009 @ 03:41 PM

State: wontfix
Milestone: none
Assigned to: josh josh
Importance: none

Activity

  1. josh
  2. josh
    josh
    • State changed from new to wontfix

    I'm not sure this is a good idea in general. If you add ActiveRecord::Base.send(:attr_accessible, nil) to base no AR attributes will be able to be saved on any model.

    I would think the same issue would have existed in any version of Rails.

    February 25th, 2009 @ 06:34 PM

  3. Peter Nash
    Peter Nash

    The reason for adding ActiveRecord::Base.send(:attr_accessible, nil) is so that the default behaviour of all application model classes is to NOT allow updates to to attributes from params unless access is specifically allowed with :attr_accessible in each model. This way it relies on the programmer to explicitly declare which attributes can be updated from parameter hashes. I know that there are varying opinions about whether that's a good thing to enforce but in the past setting this did not break any of the core Rails classes. For background on why I prefer to "whitelist" attr_accessible see http://railscasts.com/episodes/26

    February 25th, 2009 @ 10:29 PM

  4. Peter Nash
    Peter Nash

    I meant to add that using ActiveRecord::Base.send(:attr_accessible, nil) to enforce the use of attr_accessible in models used to work fine in 2.1, 2.2 and 2.3RC1 - it's only on Edge that it breaks AR sessions. However, if anyone else is affected by this, an alternative strategy of auditing for the use of attr_accessible in models instead of enforcing it is detailed here http://blog.insoshi.com/2008/09/...

    February 26th, 2009 @ 12:01 AM

  5. keith_shetler (at hotmail)
    keith_shetler (at hotmail)

    This appears in 2.3.2. The work-around:

    ActiveRecord::Base.send(:attr_accessible, nil)
    ActiveRecord::Base.send(:attr_accessible, :session_id)

    This correctly allows they system to set the :session_id attribute.

    July 10th, 2009 @ 05:57 PM