fireward icon indicating copy to clipboard operation
fireward copied to clipboard

Setting read and write requests on the same line causes errors

Open matthewiiv opened this issue 5 years ago • 2 comments

Setting read and write rules on the same line causes resource is undefined error on get requests since no resource is provided in the request.

firestore.ward

match /users/{user_id} is User {
  allow read, list, create: if true;
  allow update: if request.auth.uid == user_id
}

firestore.rules

match /users/{user_id} {
      function is______PathType(data, prev) {
        return is____User(data, prev!=null ? prev : null);
      }
      allow read, list, create: if is______PathType(request.resource.data, resource==null ? null : resource.data) && (true);
      allow update: if is______PathType(request.resource.data, resource==null ? null : resource.data) && (request.auth.uid == user_id);
    }

This can be fixed by putting read and write rules on separate lines: firestore.ward

match /users/{user_id} is User {
  allow read, list: if true;
  allow create: if true;
  allow update: if request.auth.uid == user_id
}

firestore.rules

match /users/{user_id} {
      function is______PathType(data, prev) {
        return is____User(data, prev!=null ? prev : null);
      }
      allow read, list: if true;
      allow create: if is______PathType(request.resource.data, resource==null ? null : resource.data) && (true);
      allow update: if is______PathType(request.resource.data, resource==null ? null : resource.data) && (request.auth.uid == user_id);
    }

matthewiiv avatar Jan 26 '21 12:01 matthewiiv

Thanks for reporting the issue @matthewiiv. Others have also run into the issue (#23). So far, the solution I have in mind is to throw a compile error for read and write operations on the same allow directive.

bijoutrouvaille avatar Feb 08 '21 22:02 bijoutrouvaille

Ah interesting. I didn't realise it would also cause issues in firestore too.

I think throwing a compile error is a perfectly reasonable solution here.

Thanks for the respone

matthewiiv avatar Feb 08 '21 22:02 matthewiiv