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

On put, if you protect() every field, will wipe out entire collection #254

Open
brianhiss opened this issue Aug 22, 2013 · 3 comments
Open

Comments

@brianhiss
Copy link

I was adding protect() to a put method, to secure a collection. I started by adding all the fields and then backing out a few fields I wanted to change. I noticed when I had all the fields in put.js listed with protect(), it wiped out every row of data in that collection.

I have been testing it several times with all different field types (array, string, number, etc.) and seems to be consistent behavior.

I am going to dig for a solution, but wanted to give a heads up if someone else experienced this or already has a solution.

Check out: [Protect in the Deployd Docs](http://docs.deployd.com/docs/collections/adding-logic.md#s-On Put-3789)

@brianhiss
Copy link
Author

Found the solution, can't seem to submit a pull request.

Under lib/resources/collection/index.js, around line ~520, wrap this:

store.update({id: query.id}, item, function (err) {
   if(err) return done(err);
   item.id = id;

   done(null, item);

   if(session && session.emitToAll) session.emitToAll(collection.name + ':changed');
});

with this:

if (Object.keys(item).length === 0) {
            done(new Error("Not authorized."));
        } else {
            store.update({id: query.id}, item, function (err) {
                if(err) return done(err);
                item.id = id;

                done(null, item);

                if(session && session.emitToAll) session.emitToAll(collection.name + ':changed');
            });
        }
      }

@ritch
Copy link
Member

ritch commented Aug 22, 2013

A PR for this would be great. What was the issue when submitting one?

@sigbjornt
Copy link

It does not look like this problem is resolved (using 0.8.4).
May I suggest to reopen the issue?

Small usecase where the object lost all values except from the id:

id="";

dpd.mycollection.post({Name:'Test', theArray:[{'data':'testdata'}]}, function(result, error) {
id=result.id
});

dpd.mycollection.put(id,{Name:"updatedName", theArray:[{'data':'updated-testdata'}]});

The collection has 3 fields: id (string), Name (string) and theArray (array). The onPut event handler protects 2 fields

protect('theArray');
protect('Name')

@ericfong ericfong reopened this Jul 29, 2015
NicolasRitouet pushed a commit that referenced this issue Oct 18, 2016
* correct extreme uses of hide() and protect()

this addresses the problem of #254 where, if you hide or protect all fields of an object during a PUT operation, all the data of the record will be deleted.

* pass jshint requirement

* Update index.js
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

4 participants