Firestore 规则在尝试读取 Resource.data 值时无法尝试列出集合

Gee*_*Lad 6 firebase firebase-security google-cloud-firestore

简单场景:

  • 我有一个名为“foo”的集合
  • 它有一个 ID 为“bar”的文档
  • “bar”有一个名为“sample”的字符串字段,其值为“fail”

如果我在我的规则集中尝试这样的事情:

  service cloud.firestore {
    match /databases/{database}/documents {
      // Test rule
      match /foo/{id} {
        allow read: if resource.data.sample == 'fail';
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

当我在查询中指定文档 ID 时,它可以在“bar”上进行查询,但如果我尝试列出所有项目,查询将返回“缺少或权限不足”。我也尝试过allow read: if get(/databases/$(database)/documents/foo/$(id)).data.sample == 'fail';,得到了相同的结果。

集合中只有一条记录,当我引用数据 ID 时,我可以单独查询它。据我所知,为了查询正常工作,过滤条件需要匹配规则。在本例中,我没有应用任何过滤器,但应返回所有 (1) 条记录。

我很难找到解决方案。我见过的所有解决方案都说查询结果需要匹配规则,在这种情况下它们应该匹配。

Gee*_*Lad 1

我不知道发生了什么,但它现在似乎正在发挥作用。我没有改变任何东西来让它工作。我使用的测试代码通过 Web API 非常简单:

function test() { firebase.firestore().collection("foo").get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
    }); });} test();
Run Code Online (Sandbox Code Playgroud)