Firebase 规则拒绝集合组查询

Fre*_*nga 4 firebase firebase-security google-cloud-firestore

有用户、项目作为顶级集合,任务作为项目的子集合,下面是安全规则,我在允许集合组查询任务时遇到问题。即使我删除对createdBy的检查,似乎也不起作用,当我为任务资源运行模拟器时,我的规则有效

match /projects/{projectID} {
  allow read, delete, update: if request.auth.uid == resource.data.createdBy;
  allow create: if request.auth != null;
}

match /users/{userID} {
    allow read, delete, update: if request.auth.uid == userID;
  allow create: if request.auth != null;
}

match /projects/{projectID}/tasks/{taskID} {
  allow read, delete, update: if request.auth.uid == resource.data.createdBy;
  allow create: if request.auth != null;
}
Run Code Online (Sandbox Code Playgroud)

这是我的集合组查询

_firestore
    .collectionGroup('tasks')
    .where('dueDate', isEqualTo: DateTimeHelper.today)
    .where('createdBy', isEqualTo: user.id)
    .snapshots()
    .map((list) => list.documents.map((doc) {
          String projectId = doc.reference.parent().parent().documentID;
          String taskId = doc.documentID;
          return Task.fromDocument(doc, taskId, projectId);
        }).toList());
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 8

您的任何规则都不适用于集合组查询。您应该查看有关集合组规则的文档。从该页面:

在安全规则中,您必须通过为集合组编写规则来明确允许集合组查询:

  1. 确保rules_version = '2';是规则集的第一行。集合组查询需要安全规则版本 2 的新递归通配符 {name=**} 行为。
  2. 使用 match /{path=**}/[COLLECTION_ID]/{doc} 为您的集合组编写规则。

所以你的规则看起来更像是这样的:

rules_version = '2';  // at the very top of your rules

match /{path=**}/tasks/{taskID} {
  allow read, delete, update: if request.auth.uid == resource.data.createdBy;
  allow create: if request.auth != null;
}
Run Code Online (Sandbox Code Playgroud)