News for hackers. Stuff that matters.
Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by tptacek on Friday January 27, 2012 @06:16PM (#3520651)
I know i'm harping here, Michael, but:

Another step that isn’t strictly necessary at this stage—but is a really good idea nevertheless—is to tell Rails which attributes of the model are accessible, i.e., which attributes can be modified by outside users (such as users submitting requests with web browsers).

That's not what attr_accessible means --- the real meaning of attr_accessible is subtly different in a way that leads people to make mistakes. attr_accessible means attributes can be modified automatically based on user input. End-user web browser requests can modify things that aren't in attr_accessible --- you just have to write the code to let them.

The error this leads to is overly-permissive attr_accessible statements, because people fall into a trap of believing anything that might be changed as a direct result of a user request must be in the attr_accessible statement.

Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by mhartl on Friday January 27, 2012 @06:57PM (#3520757)
If this is 'harping', please keep doing it. Mass assignment/attr_accessible is an important and oft-neglected issue, and I appreciate your taking the time to help. I agree that readers could get the wrong idea by thinking they're better-protected than they really are. I've clarified the wording by adding "automatically" as a modifier:

i.e., which attributes can be modified automatically by outside users

The detailed meaning is then deferred to later in the tutorial, when the effects of attr_accessible are illustrated with concrete examples of mass assignment.

Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by dawgr on Saturday January 28, 2012 @07:46AM (#3521956)
Could you please include a small section that talks about how to integrate rails and backbone?
Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by mhartl on Saturday January 28, 2012 @04:38PM (#3523360)
I don't have any plans to make this part of the core tutorial, but it might be a good topic for a future standalone product.
Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by snprbob86 on Friday January 27, 2012 @06:59PM (#3520763)
`attr_accessible` and friends are a nightmare anyway. They push a controller-layer concern into the model-layer. It breaks the hash-initialization syntax that is so useful in irb, in unit tests, in background jobs, etc.

Instead, I've been using ActiveSupport's Hash#slice method in my controllers.

Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by tptacek on Friday January 27, 2012 @07:39PM (#3520879)
Declarative syntax for which attributes the model designer is comfortable exposing to mass assignment seems like a perfectly legitimate model concern. In what sense is attr_accessible different from, say, "validate"?
Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by snprbob86 on Friday January 27, 2012 @08:08PM (#3520967)
It mascarades and is documented as a security feature, but it's a superficial restriction. You could simply loop and send("#{key}=", value) instead of calling attributes=(hash).

In practice, all it does it prevent you from writing `MyModel.new(:foo => bar)` and force you to write `m = MyModel.new; m.foo = bar` This is simply annoying at the repl.

Validation applies always the same (or when some predicates are satisfied), but restriction on changing fields can vary by view or via permissions (ie. only admins can flip that bit!)

The security concern comes up when you bring "params" into the picture. You don't want to ever do: MyModel.new(params) for fear of params[:is_admin] == true.

A better solution would be to automatically mark the values in `params` as tainted and throw an error on mass-assignment of tainted values. This way you can still use mass assignment for non-user-input (like in tests or at the repl). You could have an `safe_attrs` to disable tainted value filtering for some known-safe attributes.

Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by tptacek on Friday January 27, 2012 @08:53PM (#3521074)
I see where you're coming from. In the past 10 years, I've learned to appreciate things that, while not solving fundamentally a specific security problem, act as a circuit breaker to keep people from mindlessly making the exact same mistake over and over again.

AR::Base#update_attributes is a bad interface, from a security perspective. But it's vital to the programmer experience of Rails development. There's no good fix for the problem; AR models don't even list their attributes, let alone express a coherent strategy for defending them. I'll take the little wins I can get.

Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by sc00ter on Saturday January 28, 2012 @08:47AM (#3522045)
"Mass assignment security is a feature that is aimed at dealing with form input. By placing it in the model, we end up making the implicit assumption that all ‘normal’ interaction with the model happens via a HTTP request. This assumption is incorrect and causes problems."

http://jonathanleighton.com/articles/2011/mass-assignment-se...

Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by danneu on Friday January 27, 2012 @08:08PM (#3520966)
You can still use attr_accessible in the controller-layer.

    class AccountsController < ApplicationController
      include ActiveModel::MassAssignmentSecurity

      attr_accessible :first_name, :last_name
      ...
http://api.rubyonrails.org/classes/ActiveModel/MassAssignmen... explicitly states: Note that using Hash#except or Hash#slice in place of attr_accessible to sanitize attributes won’t provide sufficient protection.

I agree with you, though. Model-level attr_accessible, if nothing else, is flatly annoying. I feel inconvenienced by a measure that's supposed to protect against malicious users. And I feel like the terrorists have already won. I'm no MVC guru, but it makes sense to me that the Controller would deal with what amounts to the params that are on their way to the model. Why should the model have to worry about mass assignment?

Re:Chapter 6 of the Rails Tutorial, 2nd Edition is out ("Mod... by snprbob86 on Friday January 27, 2012 @08:50PM (#3521069)
`except` is a bad idea because it's a black-list, but what's wrong with using `slice` as a white-list?

(Anyway, I just grepped our code and there is only ONE usage of slice in a controller anyway, the rest are explicit, non-mass assignments)