Firebase 安全规则不适用于 CollectionGroup

Firebase security rule not working on CollectionGroup

我有这个安全规则代码

  match /{path=**}/favorited/{userUID} {
  allow create, update, read: if request.auth.uid == userUID;
} 

这是我收到的错误信息

W/Firestore( 6236): (21.3.0) [Firestore]: Listen for Query( collectionGroup=favorited where userUID == abcdefghijklmnopqrstuvwxyz and favorite == true order by -updatedAt, -name) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

我使用的查询是

  Firestore.instance.collectionGroup('favorited')
    .where('userUID', isEqualTo: user.uid)
    .where('favorite', isEqualTo: true)
    .orderBy('updatedAt', descending: true).snapshots()

我不知道 collectionGroup 的安全规则的哪一部分出了问题。 当没有安全规则时,查询代码工作正常。 我在这里错过了什么?

您的查询:

Firestore.instance.collectionGroup('favorited')
    .where('userUID', isEqualTo: user.uid)
    .where('favorite', isEqualTo: true)
    .orderBy('updatedAt', descending: true).snapshots()

...正在查找具有 字段 {userUID: value} 且值等于 user.uid 的任何文档(未知 ID),但您的安全规则:

match /{path=**}/favorited/{userUID} {
  allow create, update, read: if request.auth.uid == userUID;

正在专门寻找 Id 为 request.auth.uid 的文档。

请记住,安全规则是 而不是 过滤器,您是否创建了文档,以便每个具有 Id {value} 的文档也有一个字段 {userUID: value} ?

query 必须进行过滤;安全规则确保只有结果 ALL 通过规则的查询才会成功。如果查询 can 结果的 ID 与字段 {userUID: value} 不同,它将被拒绝。