【问题标题】:Firebase security rule not working on CollectionGroupFirebase 安全规则不适用于 CollectionGroup
【发布时间】:2021-05-14 03:49:18
【问题描述】:

我有这个安全规则代码

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

这是我收到的错误消息

W/Firestore(6236):(21.3.0)[Firestore]:监听查询(collectionGroup=favorited where userUID == abcdefghijklmnopqrstuvwxyz and favorite == true order by -updatedAt,-name ) 失败:状态{code=PERMISSION_DENIED, description=缺少或权限不足。, cause=null}

我使用的查询是

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

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

【问题讨论】:

    标签: flutter google-cloud-firestore firebase-security


    【解决方案1】:

    您的查询:

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

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

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

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

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

    查询必须做过滤;安全规则确保只有结果 ALL 通过规则的查询才会成功。如果查询 可以 得到一个 Id 与字段 {userUID: value} 不同的结果,它将被拒绝。

    【讨论】:

    • 感谢您的回答!是的,我所有名为“favorited”的子集合下的文档都有字段“{userUID:value}”,我已经尝试使用“request.auth.uid == resource.data.userUID”而不是“request.auth”。 uid == userUID”。这是你的意思吗?但它也没有工作..
    • 不,我的意思是他们都有userUID的documentID吗?您所写的安全规则与文档中的 ANY 字段没有任何关系;它要求 documentID 本身等于 userUID。匹配表达式与集合和文档名称、NOT 字段相关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多