Cloud Firestore安全规则只允许来自Firebase函数的写入

40

我希望能够通过允许Firebase函数写入特定集合来保护我的Firestore数据库...我该如何实现这一点?查看他们的文档,我没有找到任何可能说明如何做到这一点的内容。例如,我正在寻找像这样的东西:

service cloud.firestore {
  match /databases/{database}/documents {

    // Match any document in the 'cities' collection
    match /cities/{city} {
      allow read;
      allow write: if <from firebase function>;
    }
  }
}
3个回答

80

一般来说,使用 Firebase 的 Cloud Functions 代码会使用 Firebase Admin SDK 访问其他 Firebase 产品。无论权限如何设置,Admin SDK 都将拥有对 Firestore 的完全读写访问权限。你既不能明确允许也不能拒绝访问 Admin SDK,这也意味着你不能明确允许或拒绝访问 Cloud Functions。

如果你只希望后端读写数据库的某些部分而不包括移动客户端应用程序,请完全拒绝所有客户端的访问,并让 Admin SDK 完成其工作。

service cloud.firestore {
  match /databases/{database}/documents {

    // Match any document in the 'cities' collection
    match /cities/{city} {
      allow read: if false;
      allow write: if false;
    }
  }
}

3
非常感谢,这正是我所需要的。 - realroo
2
如果我只是删除 allow write: if false; 这一行呢? - user25
2
虽然这可能适用于Firebase的云函数,但似乎不适用于常规的云函数。 - Blueriver

4

所以,我使用这条规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read: if false;
      allow write: if false;
    }
  }
}

基于上述答案。它只禁止除管理员SDK外的任何客户端应用程序访问。


0

截至2021年2月,你只需声明即可。

{
  "rules": {
    ".read": false,
    ".write": false
  }
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接