我想创建 Firebase Firestore 规则,以便只有经过验证的用户才能读取和写入成员集合。但我好像写不出这么火爆的规则。
我写了这些 Firestore 规则:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
match /users/{userId} {
allow create: if request.auth.uid != null;
allow read: if request.auth.uid == userId;
}
match /members/{document=**} {
allow read, write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.verified == true;
}
}
}
但它们似乎不起作用。即使我尝试这样做,这也是最令人担忧的部分:
match /members/{document=**} {
allow read: if false;
}
我仍然可以读取应用程序中的数据。我什至可以更新 Firebase Web UI 中的文档,它们也会在我的 React-admin 应用程序中更新。
您的
match /{document=**}
匹配语句与您的 match /members/{document=**}
语句重叠。
并且与
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
任何经过身份验证的用户都可以读取和写入您的整个 Firestore 数据库。
有关重叠匹配语句的更多详细信息,请参阅doc:
一个文档可能会匹配多个匹配语句。 当多个允许表达式匹配一个请求时, 如果满足任何条件,则允许访问。
一般来说不建议保留默认的
match /{document=**}
声明。您应该为每个集合/子集合编写专用的集合或规则,并删除 match /{document=**}
语句和规则。