Firebase 数据库、云函数、规则

Firebase Database, Cloud Functions, Rules

如何在名为 /functions-project-12345 的数据库中限制 write/lastmodified table 的访问仅限于云功能,read & write 对 table 的访问/chat 给大家

/functions-project-12345
    /lastmodified: 1234567890
    /chat
        /key-123456
            username: "Mat"
            text: "Hey Bob!"
        /key-123457
            username: "Bob"
            text: "Hey Mat"

您在此处看过第 Authenticate with limited privileges 章吗:https://firebase.google.com/docs/database/admin/start

这可能是您需要的。

基本上您需要使用 Admin SDK 并使用特定的 uid 和服务密钥对其进行初始化。否则(即,如果您通过触发该函数的事件的引用访问数据库),您的函数将与创建该事件的客户端具有相同的 uid(和相同的访问权限)。

Cloud Functions 运行 具有管理权限,这意味着它们绕过了数据库的安全规则。知道了这一点,就可以非常简单地保护数据库以满足您的要求:

{
  "rules": {
    ".write": false,
    "chat": {
      ".write": true
    }
  }
}