Firestore Security Rules breaking with update rule

2019-02-27 19:05发布

问题:

I posted a question about this yesterday but I'm creating a new one with more details. Firestore .setData is blocked by update rule not create

I've run the simulator and the rules work there. Also when I create the document and change setData in the swift code to update the code works. It appears to only fail when creating the document. But the catch is that when I remove the update rule or simply change it to allow update: if false; the setData (or seen as create by the rules) executes properly. I have no clue whats going on nor do I know of any tools for getting a better insight.

 match /users_real/{userID} {
    allow create: if true;
    allow read: if isOwner(userID);
    allow update: if (request.writeFields.size() == 1);

}

set data:

self.docRef.collection("users_real").document("adfadsf").setData(post) { (error) in

            if let error = error {
                print("He dead!: \(error.localizedDescription)")


            }
            else {
                print("it worked, for now")


            }
        }

回答1:

Firebase Support confirms that there is a bug related to the evaluation of request.writeFields.size(). No estimate was given of when it will be fixed.

The existence of the bug can be demonstrated with the following rules:

service cloud.firestore {
  match /databases/{database}/documents {

    match /cities/{city} {
      // This should always evaluate to true, but does not.
      allow create: if (request.writeFields.size() == 1) || (request.writeFields.size() != 1);
      allow update: if true;
    }
  }
}

Although the create rule should always evaluate to true, an attempt to create a city fails with Permission Denied. It seems that the problem with request.writeFields affects not only the rule in which it appears, but also other rules for the path. For the rules shown above, an attempt to update an existing city also fails with Permission Denied.