Cloud Firestore安全规则仅允许从Firebase功能进行写入

rea*_*roo 21 firebase google-cloud-functions google-cloud-firestore firebase-security-rules

我真的希望能够通过仅允许firebase函数写入特定集合来保护我的firestore db ......我将如何去做?看那里的文档我找不到任何可能说明你怎么做的东西.例如,我正在寻找类似的东西:

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>;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 39

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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢。正是我需要的。 (2认同)
  • 如果我只删除`allow write:if false;'行怎么办? (2认同)
  • 虽然这可能适用于 Firebase 的 Cloud Functions,但它似乎不适用于常规的 Cloud Functions。 (2认同)