Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updateAttributes with whitelists #61

Open
larzconwell opened this issue Jun 23, 2013 · 2 comments
Open

updateAttributes with whitelists #61

larzconwell opened this issue Jun 23, 2013 · 2 comments

Comments

@larzconwell
Copy link
Contributor

This would make it safer to update attributes from arbitrary sources like request parameters without explicit removal of harmful attributes.

@mde
Copy link
Contributor

mde commented Jun 24, 2013

Can you give us a better idea of what this would look like?

@larzconwell
Copy link
Contributor Author

Here's how I am hackily doing it right now, just overwriting updateAttributes to filter out items

var User = function () {
  this.defineProperties({
    name: {type: 'string', required: true} // Should not be able to change
    homepage: {type: 'string'}
  });
};

User.whitelistParams = ['homepage'];

User = model.register('User', User);

User.prototype._updateAttributes = User.prototype.updateAttributes;
User.prototype.updateAttributes = function updateAttributes (attrs) {
  var safeAttrs = {};

  for (var a in attrs) {
    if (User.whitelistParams.indexOf(a) > -1) {
      safeAttrs[a] = attrs[a];
    }
  }

  return this._updateAttributes(safeAttrs);
};

If adding support in model itself, maybe User.whitelistParams could be changed to something else, like maybe an option for the property definitions.

this.defineProperties({
  name: {type: 'string', required: true}
  homepage: {type: 'string', whitelist: true} 
  // If a whitelist item is included in any of the property definitions, 
  // then enable whitelisting and only allow `whitelist: true` items 
  // to be updated
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants