Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing or insufficient permissions when writing to Firestore using field in access rules

I am getting an error when attempting to write to Firestore.

I am attempting to use a field containing the user uid for my security rule.

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

    match /messages/{document=**} {
      allow read: if resource.data.user_uid == request.auth.uid;
      allow write: if resource.data.user_uid == request.auth.uid;
    }
  }
}

If I have data in my Firestore database, the read rule works fine - but when I attempt to write I get: Error: Missing or insufficient permissions. Is there something I'm doing wrong with this write rule?

P.S. If I change my rule to this, I'm able to write to my database - but this isn't secure enough for my purposes:

 match /messages/{document=**} {
      allow read: if resource.data.user_uid == request.auth.uid;
      allow write: if request.auth != null;
    }
like image 941
Leo Farmer Avatar asked Oct 08 '17 20:10

Leo Farmer


People also ask

How do I set firestore database rules?

Use the Firebase console To set up and deploy your first set of rules, open the Rules tab in the Cloud Firestore section of the Firebase console. Write your rules in the online editor, then click Publish.

What counts as a write in firestore?

According to Firebase pricing, writes are defined as: You are charged for each document read, write, and delete that you perform with Cloud Firestore. Charges for writes and deletes are straightforward. For writes, each set or update operation counts as a single write. Meaning that one document created is one write.


Video Answer


3 Answers

resource.data refers to the data already stored, so your rule allowed users to only update data that already includes their user ID.

What you probably want to check is request.resource.data which is the new data coming in.

There's a rather extensive document about those fields and other security rules here: https://firebase.google.com/docs/firestore/security/rules-conditions

like image 157
Scarygami Avatar answered Oct 12 '22 05:10

Scarygami


This question and the accepted answer were very useful for me. I ended up using a slightly different set of rules which I'll share here incase someone else finds them useful.

Like the OP, I store a user ID (uid) with my resources. However, when I want to create a new resource, there's no uid yet. Using an example in the documentation I ended up with security rules that look like this:

service cloud.firestore {
  match /databases/{database}/documents {
    match /messages/{document=**} {
      allow read, update, delete: if request.auth.uid == resource.data.uid;
      allow create: if request.auth.uid != null;
    }
  }
}
like image 36
quicklikerabbit Avatar answered Oct 12 '22 05:10

quicklikerabbit


You can make your database available just for reading and not for writing:

  service cloud.firestore {
    match /databases/{database}/documents {
     match /{document=**} {
       allow read: if true;
       allow write: if false;
      }
   }
}
like image 4
Ssubrat Rrudra Avatar answered Oct 12 '22 04:10

Ssubrat Rrudra