Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing access based on mapped key

It's a simple and common use case of security rule, but cannot make it work. I have a document orgs/fooOrg on my Firestore(not RTDB), and it contains an object

{
  "members": {
    "fooUser": true
  }
}

and the rule applied is

service cloud.firestore {
  match /databases/{database}/documents {
    match /orgs/{orgId} {
      allow read: if "fooUser" in resource.data.members;
    }
  }
}

I expect all the document in orgs collection should be able to be read; however, the server says Error: Missing or insufficient permissions as a result of running

firebase.firestore().doc('orgs/fooOrg').get()

on a browser (using v4.5.0 and v4.5.1). Even

allow read: if resource.data.members["fooUser"] == true;

fails, too. What went wrong?

In my understanding, this should work according to this document https://firebase.google.com/docs/firestore/security/secure-data#evaluating_documents_currently_in_the_database

I believe that it was working like a week ago. All the sudden, my working code started to generate the error, so I wrote this MCVE and tested on several different projects.

In addition, I found similar issues below, but a bit different from them, so not sure if it's the same reason (a bug on Firestore)

Firestore security rules based on map values (My case, even getting a simple document fails)

Firestore read rules with self condition (This case uses a value of a map. My case, a key is used)

like image 769
Ray Sakai Avatar asked Mar 19 '26 20:03

Ray Sakai


1 Answers

Now seems that the issue is solved without changing code. No announcement, but seems that something is fixed by Firestore side.

like image 114
Ray Sakai Avatar answered Mar 24 '26 18:03

Ray Sakai