<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,6 @@
 == master
 
+* Add mass-assignment protection in the Message/MessageRecipient models
 * Change how the base module is included to prevent namespacing conflicts
 
 == 0.1.3 / 2008-09-07</diff>
      <filename>CHANGELOG.rdoc</filename>
    </modified>
    <modified>
      <diff>@@ -33,6 +33,10 @@ class Message &lt; ActiveRecord::Base
                         :sender_id,
                         :sender_type
   
+  attr_accessible :subject,
+                  :body,
+                  :to, :cc, :bcc
+  
   after_save :update_recipients
   
   named_scope :visible,</diff>
      <filename>app/models/message.rb</filename>
    </modified>
    <modified>
      <diff>@@ -31,6 +31,10 @@ class MessageRecipient &lt; ActiveRecord::Base
                         :receiver_id,
                         :receiver_type
   
+  attr_protected  :state,
+                  :position,
+                  :hidden_at
+  
   before_create :set_position
   before_destroy :reorder_positions
   </diff>
      <filename>app/models/message_recipient.rb</filename>
    </modified>
    <modified>
      <diff>@@ -13,12 +13,16 @@ module Factory
   def valid_attributes_for(model, attributes = {})
     name = model.to_s.underscore
     send(&quot;#{name}_attributes&quot;, attributes)
+    attributes.stringify_keys!
     attributes
   end
   
   # Build an unsaved record
   def new_record(model, *args)
-    model.new(valid_attributes_for(model, *args))
+    attributes = valid_attributes_for(model, *args)
+    record = model.new(attributes)
+    attributes.each {|attr, value| record.send(&quot;#{attr}=&quot;, value) if model.accessible_attributes &amp;&amp; !model.accessible_attributes.include?(attr) || model.protected_attributes &amp;&amp; model.protected_attributes.include?(attr)}
+    record
   end
   
   # Build and save/reload a record</diff>
      <filename>test/factory.rb</filename>
    </modified>
    <modified>
      <diff>@@ -75,6 +75,28 @@ class MesageRecipientTest &lt; Test::Unit::TestCase
     recipient = new_message_recipient(:position =&gt; nil)
     assert recipient.valid?
   end
+  
+  def test_should_protect_attributes_from_mass_assignment
+    recipient = MessageRecipient.new(
+      :id =&gt; 1,
+      :message_id =&gt; 1,
+      :receiver_id =&gt; 1,
+      :receiver_type =&gt; 'User',
+      :kind =&gt; 'bcc',
+      :position =&gt; 10,
+      :state =&gt; 'read',
+      :hidden_at =&gt; Time.now
+    )
+    
+    assert_nil recipient.id
+    assert_equal 1, recipient.message_id
+    assert_equal 1, recipient.receiver_id
+    assert_equal 'User', recipient.receiver_type
+    assert_equal 'bcc', recipient.kind
+    assert_nil recipient.position
+    assert_equal 'unread', recipient.state
+    assert_nil recipient.hidden_at
+  end
 end
 
 class MessageRecipientAfterBeingCreatedTest &lt; Test::Unit::TestCase</diff>
      <filename>test/unit/message_recipient_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -60,6 +60,32 @@ class MessageTest &lt; Test::Unit::TestCase
     message = new_message(:body =&gt; nil)
     assert message.valid?
   end
+  
+  def test_should_protect_attributes_from_mass_assignment
+    message = Message.new(
+      :id =&gt; 1,
+      :sender_id =&gt; 1,
+      :sender_type =&gt; 'User',
+      :subject =&gt; 'New features',
+      :body =&gt; 'Find out more!',
+      :to =&gt; [1, 2],
+      :cc =&gt; [3, 4],
+      :bcc =&gt; [5, 6],
+      :state =&gt; 'sent',
+      :hidden_at =&gt; Time.now
+    )
+    
+    assert_nil message.id
+    assert_nil message.sender_id
+    assert message.sender_type.blank?
+    assert_equal 'New features', message.subject
+    assert_equal 'Find out more!', message.body
+    assert_equal [1, 2], message.to
+    assert_equal [3, 4], message.cc
+    assert_equal [5, 6], message.bcc
+    assert_equal 'unsent', message.state
+    assert_nil message.hidden_at
+  end
 end
 
 class MessageBeforeBeingCreatedTest &lt; Test::Unit::TestCase</diff>
      <filename>test/unit/message_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>abbf596d1f266cd60e9b7d762269b84236cb27a6</id>
    </parent>
  </parents>
  <author>
    <name>Aaron Pfeifer</name>
    <email>aaron.pfeifer@gmail.com</email>
  </author>
  <url>http://github.com/pluginaweek/has_messages/commit/543f87a4b94b409d2aef13edd155d1e5a6d41d99</url>
  <id>543f87a4b94b409d2aef13edd155d1e5a6d41d99</id>
  <committed-date>2008-10-26T08:32:42-07:00</committed-date>
  <authored-date>2008-10-26T08:32:42-07:00</authored-date>
  <message>Add mass-assignment protection in the Message/MessageRecipient models
Update Factory helpers to handle protected attributes</message>
  <tree>fd42a54a355345fa511347a00097d1dde513eb0b</tree>
  <committer>
    <name>Aaron Pfeifer</name>
    <email>aaron.pfeifer@gmail.com</email>
  </committer>
</commit>
