【问题标题】:Cloud Firestore Security Rules allow write only from Firebase functionCloud Firestore 安全规则只允许从 Firebase 函数写入
【发布时间】:2018-12-26 13:57:37
【问题描述】:

我真的希望能够通过仅允许 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>;
    }
  }
}

【问题讨论】:

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


    【解决方案1】:

    从 2021 年 2 月起,您可以申报

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

    【讨论】:

      【解决方案2】:

      所以,我使用这条规则:

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

      这是基于上面的答案。 它只是禁止从任何客户端应用程序访问,除了 Admin SDK。

      【讨论】:

        【解决方案3】:

        Cloud Functions for Firebase 代码通常使用 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;
            }
          }
        }
        

        【讨论】:

        • 非常感谢。正是我需要的。
        • 如果我只删除allow write: if false; 行怎么办?
        • 虽然这可能适用于 Cloud Functions for Firebase,但它似乎不适用于常规 Cloud Functions。
        猜你喜欢
        • 2021-12-11
        • 1970-01-01
        • 2019-11-02
        • 1970-01-01
        • 1970-01-01
        • 2020-07-10
        • 2019-04-16
        • 2020-11-27
        • 1970-01-01
        相关资源
        最近更新 更多