From the monthly archives:

May 2009

Ruby on Rails : Danger! Mass Assignment

by neil on May 28, 2009

Please note this blog has been moved to blog.platform45.com

You should not create or update records directly from parameters!

 
     @user = User.new(params[:user])
 

This is mass assignment. It is creating a new user object from all the attributes assigned to :user

 <% form_for @user do |f| %>
 <%= f.text_field :name %>
 <%= f.text_field :email %>
 ...

All the attributes from this parameters hash with be be used in creating a new user model. If your user table contains an admin field. The ‘attacker’ can submit a post, setting admin = true.

 <% form for @user do |f| %>
 <%= f.text_field :name %>
 <%= f.text_field :email %>
 <%= f.text_field :admin, :value => true %>
 ...

How can we prevent this ?

We need to only allow “safe” attributes from being mass assigned.

You might think it’s easier to specify which attributes should be restricted, but remember foreign keys are also susceptible to mass assignment.

So we are rather going to set the attributes that are allowed, rather than restricted. The rest we will have to assign individually. In this example you would add to your model:

 
    attr_accessible :name, email
 

You should explicitly set which attributes are allowed in every model. If you’re lazy and forgetful like most of us. Try the plugin audit-mass-assignment It allows you to run:

 rake audit:mass_assignment

It will fail on every model that does not include attr_accessible.

{ 0 comments }

Will some women never change?

by Sarah Pietersen on May 19, 2009

Please note this blog has been moved to blog.platform45.com

On Saturday I attended a women’s “business networking” conference in Century City. I must admit I thoroughly enjoyed myself although it was not quite what I expected.

I expected to be welcomed and surrounded by strong successful women. I envisioned a room filled with self-starting entrepreneurs, ambitious and inspiring business women that stood against the female stereotype. Many by now are thinking “Hmmm… the female stereotype you say – touchy subject!” And yes it is a taboo subject for some. But I’m going to take advantage of the fact that this is a blog, which allows me to right my own opinions without fear of judgment. [click to continue...]

{ 4 comments }