【问题标题】:How to Make Secure Cloud Firestore Security Rules如何制定安全的 Cloud Firestore 安全规则
【发布时间】:2021-04-21 02:30:46
【问题描述】:

所以我试图弄清楚云存储规则是如何工作的,并制作了一个简单的应用程序来学习如何编写安全规则。该应用程序基本上向所有用户显示新闻,并允许用户在无需获得用户身份验证的情况下向管理员分享建议(无需注册)。所以我想如果我将allow write: if true; 用于包含用户建议的文档,它将是安全的,但我收到来自firebase 的警告,即我的firestore 云规则不安全。所以我认为这不安全,因为也许任何人都可以重写/删除users_suggestion 文档中的整个数据。我仍然需要在不获取用户身份验证的情况下实现安全规则,如果您知道如何请帮助

这就是规则的样子:

service cloud.firestore {
  match /databases/{database}/documents {
    
    match /users_suggestion/{documents}{
    allow read: if request.auth != null
    allow write: if true;
    }
    match /news/{documents}{
    allow read;
    allow write: if request.auth != null
    }
  }
}

集合是:users_suggestionnews

【问题讨论】:

  • 安全规则因应用程序而异。在/users_suggestion/{documents} 的这条规则中,read 允许经过身份验证的用户,然后写入更高级别的操作,为此用户也应该通过身份验证。
  • 希望你推荐了这个不错的 firebase 文档 - firebase.google.com/docs/firestore/security/rules-structure
  • @MuthuThavamani 是否可以在没有身份验证的情况下只允许对一个文档或字段进行有限写入?
  • @MuthuThavamani 我刚刚阅读完您分享的文档,它非常有用,似乎不可能有安全规则并允许在不进行身份验证的情况下写入。谢谢你的帮助
  • 很高兴,您对规则有所了解。在我的答案中更新了一些!

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


【解决方案1】:

安全规则因应用程序而异。

在您的 /users_suggestion/{documents} 已验证用户规则中已允许读取,然后 write 是更高级别的操作,因此用户也应通过验证。

Firebase 提供了非常灵活的安全规则来保护 Cloud Firestore、实时数据库和 Cloud Storage 中的数据。

除了仅检查用户身份验证外,您还应使用安全条件提供窄访问。

  • 基于角色的访问
  • 基于属性的访问
  • 公共和私人混合访问

参考资料:

  1. https://firebase.google.com/docs/firestore/security/rules-structure
  2. https://firebase.google.com/docs/rules/basics

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2018-04-05
    • 2018-10-19
    相关资源
    最近更新 更多