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.

Mass Assignment Security Refactoring

#3178

This patch provides a rewrite that is hopefully more understandable and modular than the existing code. This should allow developers and plugin authors to experiment a bit more without introducing massive security holes. E.g., it's pretty trivial to move responsibility over to a controller by including ActiveRecord::MassAssignmentSecurity and adding a couple helper methods. This is my first contribution attempt so hopefully I didn't mess up too much ;)

Reported by Eric Chapweske · September 9th, 2009 @ 10:15 PM

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

Activity

  1. Eric Chapweske
    Eric Chapweske
    • Tag changed from patch to activerecord, patch

    September 9th, 2009 @ 10:16 PM

  2. Michael Koziarski
    Michael Koziarski
    • Milestone cleared.
    • Assigned user set to Michael Koziarski

    Hey Eric,

    This looks like a really awesome refactoring.

    I especially like the way they're tested independently of active record's own attributes= method. Could you also include an example in the documentation for the sanitizer how you'd suggest using it in a controller.

    CCing jeremy pratik and josh as I think this is good to go but would like to get more eyes on it.

    Great work

    September 14th, 2009 @ 12:05 AM

  3. Eric Chapweske
    Eric Chapweske

    Thanks for the feedback! I've added the example documentation and also updated the implementation so that it can be extended for context-sensitive sanitization, as shown in the example in mass_assignment_security.rb. I would like to find a cleaner solution for this, but I think this approach requires the fewest API changes.

    September 29th, 2009 @ 07:49 PM

  4. Eric Chapweske
    Eric Chapweske

    Round 3. Hmmm, refactored the refactoring and expanded the documentation a bit. This simplifies the implementation significantly. Don't plan to make any other changes unless requested.

    Also added mass_assignment_security/context.rb. I'd be happy to move it to a plugin, although I think it's pretty useful, and it's optional to include it.

    Also rewrote multi-parameter assignment, the ticket is at #3317 Multi-parameter assignment refactoring

    October 5th, 2009 @ 12:30 AM

  5. Eric Chapweske
  6. Eric Chapweske
    Eric Chapweske

    Any chance of this making it in the next release? Hoping to utilize this in the near future.

    Thanks!
    -Eric

    January 12th, 2010 @ 08:18 PM

  7. Rizwan Reza
    Rizwan Reza
    • Tag changed from activerecord, patch to activerecord, bugmash, patch

    The patch doesn't apply cleanly anymore.

    January 16th, 2010 @ 08:53 AM

  8. Eric Chapweske
  9. Eric Chapweske
    Eric Chapweske

    Oh, this patch also modifies the documentation. Removed advise about using Hash.slice, and replaced it with this:

    # Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of +attr_accessible+ 
    # to sanitize attributes won't provide sufficient protection.
    

    Edited by Rohit Arondekar for formating.

    January 29th, 2010 @ 03:37 AM

  10. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to open

    This is nice and it works for me. The code style is different from the rest of Rails though: extra spaces padding class declarations, no indentation after protected/private. It's nit-picking, I know, but let's keep it uniform.

    January 29th, 2010 @ 05:47 PM

  11. Eric Chapweske
    Eric Chapweske

    Woops. Adding indentation for private/protected and no spaces between modules and classes.

    January 30th, 2010 @ 01:56 AM

  12. Eric Chapweske
    Eric Chapweske

    Bleh, scratch that -- this patch has a bug in it -- but I expect the style is correct :)

    January 30th, 2010 @ 02:10 AM

  13. Rizwan Reza
    Rizwan Reza
    • Tag changed from activerecord, bugmash, patch to activerecord, patch

    February 12th, 2010 @ 12:46 PM

  14. joshsz
    joshsz

    FWIW, this patch is no longer applying cleanly to master:

    $ git apply /tmp/mass_assign_clean.diff -v
    (...)
    Checking patch activerecord/lib/active_record/base.rb...
    error: while searching for:
        extend ActiveModel::Naming
        extend QueryCache::ClassMethods
        extend ActiveSupport::Benchmarkable
    
        include Validations
        include Locking::Optimistic, Locking::Pessimistic
    
    error: patch failed: activerecord/lib/active_record/base.rb:2378
    error: activerecord/lib/active_record/base.rb: patch does not apply
    (...)
    

    April 24th, 2010 @ 08:49 PM

  15. Rohit Arondekar
    Rohit Arondekar
    • Importance changed from to Medium

    Any updates to this ticket? Eric did you manage to fix that bug?

    July 1st, 2010 @ 10:33 AM

  16. José Valim
    José Valim
    • Assigned user changed from Michael Koziarski to José Valim
    • State changed from open to resolved

    Hoooray, applied!

    July 8th, 2010 @ 06:16 PM

  17. Jeremy Kemper